Thread: Summing Entries In A "for" Loop

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    6

    Post Summing Entries In A "for" Loop

    Basically, say i wanted to sum all the values enter for x. First, i ask the user how many x's there are, then create a "for" loop to ask for x1, x2, x3, .. xn. Then i want to know the cumulative total of x. How would i do this?

    This is the sample code i have made right now inside of my main function:

    Code:
    int N;
    int I;
    int X;
    
    
    //This is where i ask for how many x's there are//
    
    
        printf("What is N?: ");
        scanf("%d", &N);
      
    //This is the for loop//
    
        for(I=1;I<=N;I++){
            printf("the number is %d, what is X?: ", I);
            scanf("%d", &X);
        }
        
        printf("sum= %d", X);
        
        return 0;
    When it runs, it looks like this:
    What is N? (lets say 3)
    the number is 1, what is x?(lets say 2)
    the number is 2, what is x? ''
    the number is 3, what is x? ''

    the sum is: (it should say 6)


    Sorry if this is confusing, I will try my best to further clarify things, just ask.
    Last edited by Al3x_Payn3; 09-26-2014 at 11:12 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Your program will not compute the sum of anything. It will display the last number you enter. I suggest adding a line of code after the scanf that is inside the loop, adding X to another variable, that contains the total. You should also use more descriptive names for your variables. At first glance, it's impossible to know what N, I, and X are used for in your program. Changing N to inputCount would make more sense. You can use the same concept to rename your other variables.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    6
    Okay, how does this look?

    Code:
    int countTotal;
    int countingIndex;
    int xValue;
    int sum=0;
    
    
    	
    	printf("What is the total number of X's?: ");
    	scanf("%d", &countTotal);
    	
    	for(countingIndex=1; countingIndex<=countTotal;countingIndex++){
    		printf("For X%d, what is the value of X?: ", countingIndex);
    		scanf("%d", &xValue);
    		sum = sum+xValue;
    	}
    	
    	printf("The total sum of X's = %d",sum);
    	
    	return 0;
    I ran it and it actually works so i'm guessing my statement inside the for loop to find the sum is valid. Is there a better way to write this?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Al3x_Payn3 View Post
    Is there a better way to write this?
    Not really. This is a more-or-less ideal implementation. You could use the += operator on line 14, but that wouldn't really improve anything. It would just be another way of doing it, not better than the way you're already doing it.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    6
    sweet, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need "if","for loop",&"else" source codes
    By dn_angel_07 in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2009, 10:01 PM
  2. Problem with ">", "<" in a for loop
    By Isolda_ in forum C Programming
    Replies: 6
    Last Post: 09-11-2007, 01:56 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  5. max number of "entries" in a struct
    By Thantos in forum C Programming
    Replies: 3
    Last Post: 12-05-2001, 09:28 AM

Tags for this Thread