Thread: While loop for sum of cubes

  1. #1
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32

    Question While loop for sum of cubes

    I feel like I'm so close. I have to write a sum of cubes beginning with 1 to whatever # "n". I am restricted to a while loop. I just completed a similar sum of squares using a for loop and I got it fairly quickly.

    Can anyone suggest something?

    Code:
    #include <stdio.h>
    #include <math.h>
    
    main ()
    {
    /* Declare variables */
      int i, n, sumcubes = 1;
    
    /* Set for loop conditions */
      while (i <= n) 
      {
      sumcubes += (i*i*i);
      printf("Enter n: \n");
      scanf("%i", &n);
    
      printf ("The sum of the cubes from 1..%d is %d\n", n, 
          sumcubes); 
    
      }
    
     system("pause");
     exit(0); 
    
    }

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I think you should ask the user for the number of loops before the while() loop.

    Also, i and n aren't set in the boundaries of the while() loop. This leads to undefined behaviour!

    Code:
    int i=0, n;
    
    printf("Enter the loop#: ");
    scanf("%d", &n);
    
    while (i<=n){ ... }
    Unless you're doing something different

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    In addition, you really ought to declare the main() function as returning an int.
    See here for further info.
    I think you can put a signature here.

  4. #4
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    Thank you all for your suggestions.

    twomers. . .are you saying I should set the i and n variables within the loop or out of it?
    i =1 not zero at the beginning per the equation I have so I cannot set i=0.
    I do not want to ask how many times the user wants to loop.
    I want the program to increment by one from the #1 until the arbitrary # n and take all the cubes and add them up so once i increments by one until it reaches "n" it will turn false and exit the while loop.

    Thank you too, Angoid.

    This still does not work.
    Code:
      while (i <= n) 
      {
      printf("Enter n: \n");
      scanf("%i", &n);
      ++i;
      sumcubes += (i*i*i);
      printf ("The sum of the cubes from 1..%d is %d\n", n, 
          sumcubes); 
    
      }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8
    Quote Originally Posted by ralphwiggam
    Thank you all for your suggestions.

    twomers. . .are you saying I should set the i and n variables within the loop or out of it?
    i =1 not zero at the beginning per the equation I have so I cannot set i=0.
    I do not want to ask how many times the user wants to loop.
    I want the program to increment by one from the #1 until the arbitrary # n and take all the cubes and add them up so once i increments by one until it reaches "n" it will turn false and exit the while loop.

    Thank you too, Angoid.

    This still does not work.
    Code:
      while (i <= n) 
      {
      printf("Enter n: \n");
      scanf("%i", &n);
      ++i;
      sumcubes += (i*i*i);
      printf ("The sum of the cubes from 1..%d is %d\n", n, 
          sumcubes); 
    
      }
    I think he meant that you aren't initliazing i and n before the loop so those variables could contain anything. You never set what i is so it will not contain 1 like you want it to.

    Can you post a sample of what you want the input/output of your program to be?

    Is this what you want your program to do?:

    Enter n:
    1
    The sum of the cubes from 1..1 is 1

    Enter n:
    2
    The sum of the cubes from 1..2 is 9

    Enter n:
    3
    The sum of the cubes from 1..3 is 36

    Enter n:
    4
    The sum of the cubes from 1..4 is 100

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    printf("Enter n: \n");
      scanf("%i", &n);
    while (i <= n) 
      {
      sumcubes += (i*i*i);
      
      }
    printf ("The sum of the cubes from 1..%d is %d\n", n, 
          sumcubes);
    you are almost there. Just need to place your bit of code above while loop and a bit below the while loop

    ssharish2005

  7. #7
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32

    Tried suggestions

    LineOFire
    That is exactly what I want my program to do.
    I would put down exactly what u did but that is a waste of time, u nailed it.
    When I set i down just as 1 it does not work as well as the code that I have below.

    ssharish2005
    I did exactly what u suggested but now it executes even worse than before. It only asks for "n" and does nothing with it.
    Back when I had my "printf" and "scanf" functions within the while loop it at least took n and cubed the n and added 1 displaying the last printf I had in the loop with this code (ie this code gets me closer than the other variations I have to the output I want the sum of cubes over the range 1-"n"):

    Code:
      int i, n, sumcubes = 1;
    
    /* Set for loop conditions */
      while (i <= n) 
      {
      printf("Enter n: \n");
      scanf("%i", &n);
      ++i;
      sumcubes += (i*i*i);
      printf ("The sum of the cubes from 1..%d is %d\n", n, 
          sumcubes); 
    
      }
    Thank you for your suggestions

    PS I went to twomers site to thank him/her but my browser said it was down
    Last edited by ralphwiggam; 09-28-2006 at 04:28 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are first using n uninitialized. Read a value for it ahead of time; use a do-while loop, or initialize the variable first.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    printf("Enter a value for n\n?");
        scanf("%d",&n);
        
        while(i++<n)
            sum+=(i*i*i);
            
        printf("The total sum is %d\n",sum);
    ssharish2005

  10. #10
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    ssharish2005 . . .I tried your latest suggestion but it asks for a number & does nothing with it.

    Code:
      int i = 1, n = 1, sumcubes = 1;
    
      printf("Enter n: \n");
      scanf("%i", &n);
        while (i++<n) 
      {
      sumcubes += (i*i*i);
      printf ("The sum of the cubes from 1..%d is %d\n", n, 
          sumcubes); 
      }
    Oops I accidentally hit the post reply button too early but I can tell you

    quzah I tried it your way & I guess it's better now but still does not yield to my desire (no double meaning)
    Code:
    {
    /* Declare variables */
      int i = 1, n = 1, sumcubes = 1;
    
    /* Set for loop conditions */
      while (i <= n) 
      {
      printf("Enter n: \n");
      scanf("%i", &n);
      ++i;
      sumcubes += (i*i*i);
      printf ("The sum of the cubes from 1..%d is %d\n", n, 
          sumcubes); 
      }
    Last edited by ralphwiggam; 09-28-2006 at 04:54 PM.

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include<stdio.h>
    
    int main()
    {
        int i=0,n;
        int sum=0;
        
        printf("Enter a value for n\n?");
        scanf("%d",&n);
        
        while(i++<n)
            sum+=(i*i*i);
            
        printf("The total sum is %d\n",sum);
        
        getchar();
        return 0;
    }
    /*my output
    Enter a value for n
    ?3
    The total sum is 36
    */
    This is how u are suppose to use that bit of code. If it ask for some value enter some value see what sort of output it gives you. The value which it was asking you is for the variable n. Look at my code, u will know what it is , after calcualtion things it prints of the total sum. If u place the printf fucntion inside the while it will show you the each iteeration sum results. If u want to debug the program u can do that.

    ssharish2005
    Last edited by ssharish2005; 09-28-2006 at 05:07 PM.

  12. #12
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    ssharish2005
    I cut and pasted the whole code & it executes and asks for n, but once I designate a # it closes down. I am using Bloodshed software C++ version 4.9.9.2 but I save everything as .c files before I compile and run them.
    I am sorry to try your patience.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    FAQ


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    ssharish2005
    That was the key. It works! I really do appreciate it.
    Thank you too quzah.
    Thanks to everyone!
    I am learning a lot, thanks.
    Last edited by ralphwiggam; 09-29-2006 at 02:33 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM