Sample – example of pass-by-reference
by Nideesh C on February 8, 2011 · 0 comments
in C++
#include <iostream.h>
#include <stdlib.h>
int exp (int,int);
void readnums (int&, int&);
void main () {
int b, e;
readnums(b,e);
cout << b << ” to the ” << e << ” = ” << exp(b,e) << endl;
}
void readnums (int& b, int& e) {
int correctInput;
cout << “Enter the base and the exponent: “;
cin >> b >> e;
if (!cin) {
cout << “Disaster! Terminating program.” << endl;
exit(-1);
}
correctInput = (b >= 0) && (e >= 0);
while (!correctInput) {
cout << “Something wrong! Try again …” << endl;
cout << “Re-enter base and exponent: “;
cin >> b >> e;
if (!cin) {
cout << “Disaster! Terminating program.” << endl;
exit(-1);
}
correctInput = (b >= 0) && (e >= 0);
}
}
int exp (int b, int e) {
int result;
result = 1;
while (e != 0) {
result = result * b;
e = e – 1;
}
return(result);
}
Not Satisfied ? Just search & get the result
Related posts:
- Sample – exponentiation using multiplication
- Sample – understanding call-by-value and call-by-reference
- Sample – factorial
- Sample program (multiplication using addition)
- Sample – Fibonacci numbers
Tagged as:
c programming examples,
c programming reference,
c tutorial,
c++ program examples,
C++ Programming,
example of pass-by-reference,
learning c++,
pass-by-reference,
programming languages,
programming languages c,
sample c++ programs
Me, freelance system administrator having the qualification of Diploma in Electronics & Tele-communication + MCSE + CCNA + CST + 5 years of experience in IT field.
If you like This post, you can follow TheOnlineTutorials on Twitter.
Contact me Via email: support@theonlinetutorials.com
Subscribe to feed via Feed or EMAIL to receive instant updates.
Legal Disclaimer:All information found on the site is without any implied warranty of fitness for any purpose or use whatsoever. Content author/site administrator is not responsible for any loss occurred due to mistakes in this tutorial. Use this tutorial website at your own risk.