Thread: Raising the power of an interger

  1. #31
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by sourpatchkid View Post
    Even with those changes i'm getting errors.
    Post your new edited code; please also post any errors and warning you got during compiling!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  2. #32
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    You edited it after i posted ;p

  3. #33
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Go back to the math. If PWR == 0, then you want to generate 1, if PWR == 1, then you want to generate 1 · 2, if PWR == 2, then you want to generate 1 · 2 · 2 , and so on.

  4. #34
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    int loop_counter = 2, PWR = 0; 
    printf("enter an interger:\n ");
    scanf("%d", &PWR);
    for (loop_counter = 0; loop_counter <; loop_counter++)
    {
    loop_counter*=PWR;
    }
    system("pause");
    return 0;
    
    
    
    
    
    
    }
    What would be the condition to stop it? loop_counter < PWR?

  5. #35
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    hat would be the condition to stop it? loop_counter < PWR?
    Well, that would be the condition to keep going - but you have the right code, as the loop will stop when the condition becomes false.

    Also, keep "loop_counter" separate - don't use it in the calculation. Put the result of the multiplication in a separate variable (perhaps called "result").

  6. #36
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    "Go back to the math. If PWR == 0, then you want to generate 1, if PWR == 1, then you want to generate 1 · 2, if PWR == 2, then you want to generate 1 · 2 · 2 , and so on."

    PWR is a variable for the user inputted exponent. loop_counter is the variable i want to multiply.

  7. #37
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    So should i multiply result to PWR?

  8. #38
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    int loop_counter = 2, PWR = 0, result = 0; 
    printf("enter an interger:\n ");
    scanf("%d", &PWR);
    for (loop_counter = 0; loop_counter < PWR; loop_counter++)
    {
    result*=PWR;
    }
    system("pause");
    return 0;
    
    
    
    
    
    
    }

  9. #39
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    OOOOOOOOOOOOOOOOOH! I know what you mean by return now lol

  10. #40
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    int loop_counter = 2, PWR = 0, result = 0, new_counter = 2; 
    printf("enter an interger:\n ");
    scanf("%d", &PWR);
    for (loop_counter = 0; loop_counter < PWR; loop_counter++)
    {
    result = new_counter*=PWR;
    }
    printf("%d\n", result);
    system("pause");
    return 0;
    
    
    
    
    
    
    }
    How does this look?

  11. #41
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    (loop_counter = 0;
    I think you will find that 0 is NOT the best value to use.
    You changed the code enough to get to work with 0; but, the code is ugly to me.

    Tim S.
    Last edited by stahta01; 04-26-2013 at 07:27 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  12. #42
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Whenever i input something though it's outputting wrong numbers.

    I'm VERY new to coding. I just learned about arithmetic expressions and looping this week in class.

  13. #43
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    I would have thought that loop_counter++ would of added 1 until it reached the value of PWR. But it's going beyond that.

  14. #44
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by sourpatchkid View Post
    I would have thought that loop_counter++ would of added 1 until it reached the value of PWR. But it's going beyond that.
    That is not the problem. Your for starting at loop counter at 0, and checking for < PWR is OK. You need to initialize result to 1 before starting the loop. Then looking at your loop, what it's currently doing is factorial, not powers of 2, your loop is doing 1 · 2 · 3 · 4 ... . So how can you fix your loop so it's doing 1 · 2 · 2 · 2 ... ?

  15. #45
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sourpatchkid
    I just learned about arithmetic expressions and looping this week in class.
    Did your teacher cover bitwise operations too?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Raising an Integer to a power
    By aileentas in forum C Programming
    Replies: 14
    Last Post: 09-19-2012, 03:58 AM
  2. Raising to a power
    By teelnaw in forum C Programming
    Replies: 6
    Last Post: 11-07-2010, 01:35 AM
  3. Help with checking, raising to the power of 2
    By philippe in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 07:30 PM
  4. raising to a power
    By next_to_nothing in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2002, 04:00 AM
  5. Raising to a power
    By Nate2430 in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2001, 04:44 PM