Yet another C++ Question...

pac1085

Diamond Member
Jun 27, 2000
3,456
0
76
Heres what the problem says to do:

Write a function PowerOf() that calculates and returns the value of x^n. (Hint: A for loop can be used to calculate the power.) Use the function header:
double PowerOf(double x, int n)
/*Returns x to the ntn power
n assumed >=0 */

My question is what do I put in the for loop it says to use to calculate the power? Im not very good in math questions like this, so any help would be greatly appreciated.

 

MC

Platinum Member
Feb 23, 2000
2,747
0
0
PowerOf(double x, int n){
If(x==1)
return x;
double result = 1; //Use to store result of x^n
else {
for(int i=1; i<n; i++){
result=result*x;
}
return result;
}

Hope this will help...:)
 

pac1085

Diamond Member
Jun 27, 2000
3,456
0
76
Thanks for the replys, MingChia, I had to change your code around a little bit, or I would get an error saying result wasnt declared or something. I still have a problem though...when I input PowerOf(2,10) it should display 1024, but it only displays 512...any clue why?
 

BowDown

Banned
Jun 2, 2000
2,197
0
0
I write 'C' but it's the same here:

double PowerOf (double x, int n)

{
int counter;
double total;

if (n == 1)
return (x);
else
for (total = x, counter = 1; counter < n; total *= x, counter++)
;
return (total);
}

It works I have tried it out :).


 

BowDown

Banned
Jun 2, 2000
2,197
0
0
\\ Here's the whole thing if you want it :).

\\ I set it to only print to the hundreths position ;)

#include <stdio.h>

void get_num (double *, int *);
double PowerOf (double, int);
void print_out (double);

int main (void)

{
int x;
double y;
double total;

get_num (&amp;y, &amp;x);

total = PowerOf (y, x);

print_out (total);

return (0);
}

void get_num (double *y, int *x)

{
char ch;

printf(&quot;Please enter the number to be changed: &quot;);
scanf(&quot;%lf%c&quot;, y, &amp;ch);

printf(&quot;\nPlease enter the power to be raised to: &quot;);
scanf(&quot;%d%c&quot;, x, &amp;ch);
}


double PowerOf (double x, int n)

{
int counter;
double total;

if (n == 1)
return (x);
else
for (total = x, counter = 1; counter < n; total *= x, counter++)
;
return (total);
}

void print_out (double total)

{
printf(&quot;\nYour results is %.2f!&quot;, total);
}
 

Mark R

Diamond Member
Oct 9, 1999
8,513
16
81
You need to include:

if (n==0) return 1;

The instructions state than n>=0.
 

pac1085

Diamond Member
Jun 27, 2000
3,456
0
76
Cool, Heres what I got (It works...but I havent set it up to let the user input the values :p)

#include <iostream.h>
#include <conio.h>


double PowerOf (double x, int n);

int main()
{


cout << PowerOf(2,10) << &quot;\n&quot;;

cout << &quot;\nPress any key to continue\n&quot;;
getch();
return 0;
}

double PowerOf (double x, int n)

{
int counter;
double total;

if (n == 1)
return (x);
else
for (total = x, counter = 1; counter < n; total *= x, counter++);
return (total);
}
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
and just for the hell of it here's another version ...


#include <iostream.h>

double powerof(double x, int n)
{
double rvalue = x;
while(--n) rvalue *=x;
return rvalue;
}

int main( )
{
double _base;
int _exp;

cout << &quot;Enter base value : &quot;;
cin >> _base;
cout << &quot;Enter exponent value : &quot;;
cin >> _exp;

if(_exp >= 0)
switch(_exp)
{
case 0:
cout << endl << _base << &quot; raised to 0 : 1&quot; << endl; break;
default:
cout << endl << _base << &quot; raised to &quot; << _exp << &quot; : &quot; << powerof(_base, _exp) << endl; break;
}
else
cout << &quot;Illegal exponent value&quot; << endl;
}
 

BowDown

Banned
Jun 2, 2000
2,197
0
0
Hehe... Cin and Cout; all greak to me :).

Yes I did forget the if (n==0). Noticed that after, but figured she was smart enough to catch that. :)