Output wrong..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew V
    New Member
    • May 2012
    • 16

    Output wrong..

    This is my code and i dont know why when i enter let's say x=3 and n=0 it gives 3.


    Code:
    #include <cstdlib>
    #include <iostream>
    
    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<<y;
           if(n=0){
                cout<<"1";
                }  
       
           system("PAUSE");
        return EXIT_SUCCESS;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:
    Code:
               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:
    Code:
    if(n=0){
                  cout<<"1";
                  }
    assigns 0 to n, which will always be false. Probably you meant:

    Code:
    if(n==0){
                  cout<<"1";
                  }

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      That's because a do while loop is guaranteed to run at least once. Use a while loop instead.

      Comment

      • Andrew V
        New Member
        • May 2012
        • 16

        #4
        Got it.My bad i readed while and while but it was do-while and while sorry

        Comment

        • Andrew V
          New Member
          • May 2012
          • 16

          #5
          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
          Code:
          while ( count <= n);
          with
          Code:
          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

          Comment

          • divideby0
            New Member
            • May 2012
            • 131

            #6
            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.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              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.

              Comment

              • Andrew V
                New Member
                • May 2012
                • 16

                #8
                Ok so i put
                Code:
                 while(n<0){
                and then i put
                Code:
                 y = y*x;
                      count--;
                      cout<<y;
                but the output doesn't stop.I am working for like 6 hours at this project.Can u give me a hint?

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  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.

                  Comment

                  • Andrew V
                    New Member
                    • May 2012
                    • 16

                    #10
                    Code:
                       while(count<=-1*n){
                    or
                    Code:
                       while(n<0){
                    and then what?
                    how do i save a number..

                    Comment

                    • Mariostg
                      Contributor
                      • Sep 2010
                      • 332

                      #11
                      @weaknessforcat s
                      Nice Seventh. :)

                      Comment

                      Working...