• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Yet another C++ Question...

pac1085

Diamond Member
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.

 
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...🙂
 
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?
 
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 🙂.


 
\\ 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);
}
 
Cool, Heres what I got (It works...but I havent set it up to let the user input the values 😛)

#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);
}
 
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;
}
 
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. 🙂
 
Back
Top