please check mine :)

question: write your implementation of the function double power(double x,int y);
that returns the value X^y for int y. make sure that your function works correctly for both +ve & -ve values of y.


my answer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <conio>
#include <math>
double power(double x,int y)
int main ()

{
   double r;
   r = pow ( x , y );
   return ( r );

}

int main ()

{
   double z , a ;
   int b;

   cout << "enter value a = " << a <<endl;
   cout << "enter value b = " << b <<endl;

   z = pow ( a , b ) ;

   cout << "result = " << z <<endl;

   getch ();
}


im using Borland c++,i cant run it as it has an error which i cant detect,,
plus, i m weak in function.

Last edited on
- You didn't even write your own implementation of the power function.

- You've only called the default power function, and you are missing a semicolon on what looks like a prototype.

- Moreover, you need to use code tags(the "< >" button) for code.
closed account (3qX21hU5)
1) Please use codetags when posting code to the forum.

2) You are missing a semicolon after your function prototype double power(double x,int y) which by the way you don't need since your power() function is defined before int main(). You would only need a prototype if it was defined AFTER main().

3) You have 2 int main() delete the one right after your prototype for power(). I believe you meant this to be a prototype also but int main() doesn't need a prototype and can only be declared ONCE.

4)
1
2
cout << "enter value a = " << a <<endl;
cout << "enter value b = " << b <<endl;


You are asking them to enter a value but never give them the chance to enter it. You just print the value in a and b (Which is undefined behavior since you have not yet defined what value is in either of them).

Do something like this
1
2
cout << "Enter value a = ";
cin >> a;

That is how you ask the user for input and store it in the variable a.

5) The biggest one of all you don't even write a function to calculate the power! You are just using the pow() function and returning the result. Make sure you make your own function for power or you will fail on the grading.

Also your function definition is wrong. A definition for a function should look like this.
1
2
3
4
double power(double x, double y)
{
    // Your code goes here
}


6) You created your power() function (Which does nothing really) but you don't even use it. You instead use the pow() function.

7) Watch how you format your code. Be consistent.
Last edited on
implementation?
like this? r++?

and which code should i place the tag?
- Basically, the assignment is telling you to make your own version of the "pow" function. Your own, meaning you write it yourself.

- you use the code tags as such (without the spaces):
[ code ]
code here
[ / code]

- Listen to what Zero said as well.
oh.....
thank u very much for ur help!

i need to push much effort to understand this subject....
(==")

thx guys!! :)
Topic archived. No new replies allowed.