Thread: Power Loop help!

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    Power Loop help!

    I am trying to get this outcome:
    1 4 9 16 25 36 49 64 81 100 121 144

    Here is what I have so far:
    Code:
        int i;
        int x=i * i;
         
        while (i<=12)
        {printf("%d ", x);
        i+=1;
        }
    And this puts out:
    1 1 1 1 1 1 1 1 1 1 1 1 1

    I know that I am close.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Well its printing all 1's because you are not settin the value of 'x' inside the loop. Try putting it in and see what happens. Its not goign to be right, nut it will be closer.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    I did that but I still got
    1111111111111

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    No you wouldent, or at least not if you set it as i*i each loop.

    In your current prog 'i' is undefined when you set x as well.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    Well, did that and I am finally coming up with a different outcome, but it is still not it.
    Here is my new code:
    Code:
    int i=1;
     
        for (i =1; i < 12; i +=i^2)   
        {printf("&#37;2d ", i);
    }
    And that outcome is:
    1 4 10

    So I am getting closer.

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Ok, why not look at the numbers again:

    1 4 9 16 25 36
    +3 +5 +7 +9 +11

    See a pattern?

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    Yes, but how to I tell it to start at 3 then add two each time?

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How about just putting x = i * i; in the loop in which i is incremented?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    I already tried that and it didn't work.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by hilow55321 View Post
    I already tried that and it didn't work.
    I don't see it in your code anywhere, and we keep suggesting it because it does work.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    Here is the code that I am currently working on:
    Code:
     int i;
        int x= i * i;
        for (i =1; i <= 144; i +=x)   
        {printf("&#37;2d ", i);
    }

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    What part of "in the loop" is messing you up?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    so do I make another for statement for do I put x = i*i in the original for statement?

  14. #14
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Try:

    Code:
        int i = 1, x;
         
        while (i<=12)
        {
            x = i*i;
            printf("&#37;d ", x);
            i+=1;
        }

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    See I knew I was close.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM