Thread: Raising the power of an interger

  1. #16
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by sourpatchkid View Post
    "Indeed. And we offer our help here to assist others in learning. Therefore, you are more likely to get hints that will enable you to solve the problem yourself, than a spoon-fed answer.

    Here's the sites homework policy, fyi.

    Can you write a "for()" loop? Let's start there."

    Indeed i can.

    Code:
    For(loop_counter = 0, loop_counter < 10, loop_counter++)
    {
    printf("loop %d", loop_counter);
    
    }
    Can you write one that will compile without errors?

    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. #17
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Sorry for dragging this out painfully, but it's not always easy to explain something without just blurting out the solution, which would rob you of a learning experience. Thanks for being patient =)

  3. #18
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    " initialization, condition, and the iterative change"
    Should i have the initialization at 2 since i'm using it as my initial value? What should my condition be to stop it when necessary?

  4. #19
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    "Sorry for dragging this out painfully, but it's not always easy to explain something without just blurting out the solution, which would rob you of a learning experience. Thanks for being patient =)"
    No thank you! I need help and you're actually helping me.

  5. #20
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    "Can you write one that will compile without errors?"
    The only thing i see that i messed up on was capitalizing "for".

  6. #21
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It's probably better to use a separate variable just for the counter. Although there are a few different ways to approach this, I'd recommend starting the counter at zero (initialization) and counting up to 'n'. (Well, one less than 'n', since you'd be starting from zero and not one.)

    This would require three variables: a counter for the loop, the maximum number of times to loop (based on the exponent), and the running result.

  7. #22
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by sourpatchkid View Post
    "Can you write one that will compile without errors?"
    The only thing i see that i messed up on was capitalizing "for".
    Look again.
    For, While and Do While Loops in C - Cprogramming.com

    Edit: In C, punctuation matters more than in English.

    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

  8. #23
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The only thing i see that i messed up on was capitalizing "for".
    You also separated the "for()" arguments with commas instead of semi-colons.

  9. #24
    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 = PWR; loop_counter++)
    {
    loop_counter*PWR;
    }
    system("pause");
    return 0;
    
    
    
    
    
    
    }
    how about this?

  10. #25
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    loop_counter*PWR;
    If you have a good Compiler with warnings up high; you should get a message saying this line has no effect.

    I suggest comparing "*=" with "*" in what they do.
    Also, compare "==" with "=".

    Edit3: http://en.wikipedia.org/wiki/Operato..._C%2B%2B#Table

    Tim S.
    Last edited by stahta01; 04-26-2013 at 06:30 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

  11. #26
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Well then... What am i doing wrong? All you're telling me is that i'm wrong and then not helping me. I'm not having you guys do my homework, i'm trying to problem solve.

  12. #27
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    In addition to what Tim mentioned:

    - "scanf()" requires a pointer to a variable (i.e. "&PWR")
    - The middle argument of the "for()" dictates when the loop ends - when that condition is no longer true, the loop terminates. You don't have a condition there, you have an assignment.
    - I mentioned to keep your loop/counter variable separate from any variables used in calculations

  13. #28
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by sourpatchkid View Post
    Well then... What am i doing wrong? All you're telling me is that i'm wrong and then not helping me. I'm not having you guys do my homework, i'm trying to problem solve.
    Re-read my prior message I edited at least twice giving you hints.

    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

  14. #29
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Even with those changes i'm getting errors.

  15. #30
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Post the updated code!

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