Thread: The sum of the numbers entered in a loop

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    11

    The sum of the numbers entered in a loop

    I'm trying to get the total sum of numbers entered in the loop to be printed out but also I have it so it counts how many times a number is entered in. I just can't figure out how to store each number written in. here's my code....

    Code:
    #include <stdio.h>
    
    int main (void)
    {
        //Declare Variables
        int neg_number;
        int counter;
        int sum;
        double average;
        double x;
        
        counter = 0; //Set count to 0 to count how many times user enters a number in the loop
        sum = 0; // Set sum to 0 to add up the total numbers entered in the program
        do {
            
            
            counter ++;
            printf("Please enter a negative number: ");
            scanf("%lf", &x);
            scanf("%i", &neg_number);
            sum = x+1;
            
            
            }
        while (neg_number >= 0);
        
            
        printf("The sum of the numbers you entered is %i ", sum);
        printf("You inputed %i numbers before figuring out how to put in a negative number\n", counter);

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Arrays in C - Cprogramming.com

    If you limit the range of numbers allowed, you can store the numbers entered in an array. Or you can store the count of the numbers, or both. Anything beyond a small range of numbers and you'll need some other data structure to hold them (like a linked list or something).


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

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    4
    You could implement something similar to this, to check for reoccurrences of an entered number. Perhaps use a second array to store occurrences of entered numbers. First entry would increment to 1 and then a loop such as this would check to see if that number has already been entered, if so, return to the matching element and increment again, else, assign 1.

    Code:
    EAF1[10]; /*Example Array of Floats*/
    EAF2[10];
    cntr2;
    
    /*more code here*/
    
    for (cntr2=0; cntr2<counter; cntr2++)
    {
         if (EAF1[cntr2]==EAF1[counter])
         {
              EAF2[cntr2]++;
         }
         else
         {
              EAF2[counter]=1;
         }
    }
    
    /*more code here...*/
    Seems also your |||do...while||| might be safe as a |||for||| and |||counter||| and |||sum||| could be initialized in the declarations.

    Code:
    int counter=0;
    int sum=0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 04-08-2010, 06:36 PM
  2. Replies: 4
    Last Post: 03-08-2010, 07:15 PM
  3. Finding the 3 largest numbers of the five entered
    By Bkgrant in forum C Programming
    Replies: 11
    Last Post: 02-13-2009, 01:08 PM
  4. Not allowing numbers to be entered.
    By Andy123 in forum C Programming
    Replies: 18
    Last Post: 03-17-2006, 05:47 AM
  5. minimum numbers to be entered into an array
    By volk in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2003, 06:19 PM