423,850 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,850 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
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()                                                      
  7.    {            
  8.          printf("==========©©Andrei78©©==========");
  9.          printf("=======©All Rights Reserved©====");
  10.          printf("===========©05-06-2012©=========");                                                                       
  11.        int count,x,u=1;                                           
  12.        float n,y;                                             
  13.  
  14.        printf("Enter the values of x and n : ");               
  15.        cin>>x>>n;               
  16.        y = 1;                                                
  17.        count = 1;        
  18. do
  19.  {                                                       
  20.             y = y*x;                                           
  21.             count++;           
  22.  
  23.        }        
  24. while ( count <= n);                                        
  25.  
  26.        cout<<y;
  27.        if(n=0){
  28.             cout<<"1";
  29.             }  
  30.  
  31.        system("PAUSE");
  32.     return EXIT_SUCCESS;
  33. }
  34.  
Jun 5 '12 #1

✓ answered by weaknessforcats

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.

Share this Question
Share on Google+
10 Replies


weaknessforcats
Expert Mod 5K+
P: 9,196
This code:
Expand|Select|Wrap|Line Numbers
  1.            y = y*x;                                           
  2.               count++;           
  3.  
  4.          }        
  5.   while ( count <= n);   
  6.  
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
  1. if(n=0){
  2.               cout<<"1";
  3.               } 
  4.  
assigns 0 to n, which will always be false. Probably you meant:

Expand|Select|Wrap|Line Numbers
  1. if(n==0){
  2.               cout<<"1";
  3.               } 
  4.  
Jun 5 '12 #2

Rabbit
Expert Mod 10K+
P: 12,279
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
  1. while ( count <= n);
with
Expand|Select|Wrap|Line Numbers
  1. 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

weaknessforcats
Expert Mod 5K+
P: 9,196
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
  1.  while(n<0){
and then i put
Expand|Select|Wrap|Line Numbers
  1.  y = y*x;
  2.       count--;
  3.       cout<<y;       
  4.  
but the output doesn't stop.I am working for like 6 hours at this project.Can u give me a hint?
Jun 5 '12 #8

weaknessforcats
Expert Mod 5K+
P: 9,196
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
  1.    while(count<=-1*n){
or
Expand|Select|Wrap|Line Numbers
  1.    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

Post your reply

Sign in to post your reply or Sign up for a free account.