448,825 Members | 1,625 Online Need help? Post your question and get tips & solutions from a community of 448,825 IT Pros & Developers. It's quick & easy.

# Output wrong..

 P: 16 This is my code and i dont know why when i enter let's say x=3 and n=0 it gives 3. Expand|Select|Wrap|Line Numbers #include  #include    using namespace std;   int main()                                                          {                      printf("==========©©Andrei78©©==========");          printf("=======©All Rights Reserved©====");          printf("===========©05-06-2012©=========");                                                                               int count,x,u=1;                                                   float n,y;                                                       printf("Enter the values of x and n : ");                       cin>>x>>n;                       y = 1;                                                        count = 1;         do  {                                                                    y = y*x;                                                        count++;                     }         while ( count <= n);                                                  cout<

First, before you get to your loop check to see if n is 0. If it is, skip the loop and display a 1. Anything to the 0th power is 1.

Second, if n is positive set your loop control variable to n.

Third, if n is negative, set your loop control variable to
-1 *n. So -3 will end up as 3 in the loop control variable.

Fourth, save the number in x.

Fifth, inside the loop calculate y = y * x.

Sixth, after you exit the loop check if n -s negative. If it is, your answer is 1/y. Otherwise, your answer is y.

Seventh, as to spending a ton of time, we once were where you are now. My story is that a barber once asked me what I did for a living. I said I was a C++ programmer. He said that sounded hard. I asked how long it took to become a barber. He said about 300 hours of class time plus 1800 hours of on-the-job haircutting. I thought, hmm 2100 hours. Full tme for year. Now I tell my students C++ programming is as easy as cutting hair -- after you spend 2100 hours practicing it. Good luck.

10 Replies

 Expert Mod 5K+ P: 9,197 This code: Expand|Select|Wrap|Line Numbers            y = y*x;                                                          count++;                       }           while ( count <= n);      When n is 0 and count is 1 then count<=n is really 1<=0, which is false so you drop out of the loop. This time x=3 and y=1 so y*n is 1*3 which is 3. You display 3. Beyond that, this code: Expand|Select|Wrap|Line Numbers if(n=0){               cout<<"1";               }    assigns 0 to n, which will always be false. Probably you meant: Expand|Select|Wrap|Line Numbers if(n==0){               cout<<"1";               }    Jun 5 '12 #2

 Expert Mod 10K+ P: 12,405 That's because a do while loop is guaranteed to run at least once. Use a while loop instead. Jun 5 '12 #3

 P: 16 Got it.My bad i readed while and while but it was do-while and while sorry Jun 5 '12 #4

 P: 16 Ok,i solved with positive and 0 powers.But now i am trying to do with negative power.I want it to show : x=2 n=-1 y=0.5 i changed the code Expand|Select|Wrap|Line Numbers while ( count <= n); with Expand|Select|Wrap|Line Numbers while ( count >= n); but nothing happens.And when i am trying to put it with if(){} it gives the same number with the x(the base) with every number i enter Jun 5 '12 #5

 100+ P: 131 You wouldn't need to change much; add a var to check whether or not the power is negative. If it is, multiply the power by -1 and use division instead of multiplication with the loop. Always check against div by 0. Jun 5 '12 #6

 Expert Mod 5K+ P: 9,197 Remember, a negative power is calculated differetly from a positive power. You will need different logic inside your loop. Plus, the loop control for -1 power needs to be +1 and the loop control for +1 power needs to be +1. I suggest you have a separate variable for your loop control. Jun 5 '12 #7

 P: 16 Ok so i put Expand|Select|Wrap|Line Numbers  while(n<0){ and then i put Expand|Select|Wrap|Line Numbers  y = y*x;       count--;       cout<

 Expert Mod 5K+ P: 9,197 First, before you get to your loop check to see if n is 0. If it is, skip the loop and display a 1. Anything to the 0th power is 1. Second, if n is positive set your loop control variable to n. Third, if n is negative, set your loop control variable to -1 *n. So -3 will end up as 3 in the loop control variable. Fourth, save the number in x. Fifth, inside the loop calculate y = y * x. Sixth, after you exit the loop check if n -s negative. If it is, your answer is 1/y. Otherwise, your answer is y. Seventh, as to spending a ton of time, we once were where you are now. My story is that a barber once asked me what I did for a living. I said I was a C++ programmer. He said that sounded hard. I asked how long it took to become a barber. He said about 300 hours of class time plus 1800 hours of on-the-job haircutting. I thought, hmm 2100 hours. Full tme for year. Now I tell my students C++ programming is as easy as cutting hair -- after you spend 2100 hours practicing it. Good luck. Jun 5 '12 #9

 P: 16 Expand|Select|Wrap|Line Numbers    while(count<=-1*n){ or Expand|Select|Wrap|Line Numbers    while(n<0){ and then what? how do i save a number.. Jun 6 '12 #10

 100+ P: 332 @weaknessforcats Nice Seventh. :) Jun 7 '12 #11 