Thread: help with running sum

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    27

    help with running sum

    Hello guys

    as you see I'm suppose to do what the question below wants but I got this far and my brain stopped working ... can a pro just give me a hit or hints of what I'm supposed to do next as I read my notes but ..it's no good






    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main() {
    
    
    
    
            
            int rtotal;
            int i;
    
    
    
    
            for(i=0;i<1;i++){
                printf("enter a number : ");
                scanf("%d", &rtotal);
                printf("sum %d\n",rtotal);
    
    
    
    
    }
    
    
    
    
    
    
    
    
        return main();
    }
    help with running sum-lab6q1-jpg

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I would suggest you start with two variables called 'sum' and 'number'.

    Somewhere, number gets the input.

    Somewhere, sum and number are added together.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    27
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main() {
    
    
    
    
    
    
            int number;
            int sum=0;
    
    
    
    
    
    
                printf("enter a number : ");
                scanf("%d", &number);
    
    
                sum+=number;
                printf("sum %d\n",sum);
    
    
    
    
    
    
    
    
        return main();
    }
    like this ? but it does not add each number with the previous input just like the question wants ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well so far, so good.

    > return main();
    Now you need a loop around some part of the code.
    Note: calling main recursively is NOT how you make a loop. Read your book on the 'while' statement.

    Think about the "option to exit at any stage", how would you do this?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    27
    I see , because if the condition is not true then the while loop exits , and it gets to return 0; so the program terminates imma try this now and get back to you
    cheers bro

  6. #6
    Registered User
    Join Date
    Feb 2014
    Posts
    27
    omg bro It works now , thank you so much ... now I understand the while loop more , I love you so much haha

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. got this running but...
    By hubris in forum C++ Programming
    Replies: 4
    Last Post: 03-25-2011, 11:43 AM
  2. Replies: 13
    Last Post: 12-09-2008, 11:09 AM
  3. Running
    By cgod in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2004, 11:40 PM
  4. Running *.bat with C
    By johnc in forum C Programming
    Replies: 20
    Last Post: 07-16-2002, 03:56 PM
  5. About running MS-DOS...
    By master5001 in forum Windows Programming
    Replies: 12
    Last Post: 10-10-2001, 02:21 PM