Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Implementing an average counter for an array

  1. Mar 10, 2006 #1
    i need to get this array to calculate the numbers that are less than the average and greater than or equal to the average.

    first of all, i need to figure out how to make a counter to count the average of the array! i can't find the code in the textbook

    here's what i have so far:
    Code (Text):
    #include <stdio.h>
    #define MAXARR 20

            arr[MAXARR], i, counter, value, no_of_elements, max,sum;
        /* User is requested to enter values from keyboard until -1 is entered  */
        counter = 0;
        printf("Do not entere more than %d values\n\n",MAXARR);
        printf("Enter value (-1 to quit): ");
        scanf("%d", &value);
        while (value != -1)
            printf("Please enter value (-1 to quit):   ");
            scanf ("%d", value);
            counter = counter + 1;


        /* Output routine.  The number of elements entered is displayed         */
        no_of_elements = counter;
        printf("%d values were entered\n",no_of_elements);
        printf("these values are: \n");
            printf("%d  ",arr[i]);
  2. jcsd
  3. Mar 10, 2006 #2
    Code (Text):

    int sum=0;

    for(int count=0; count<counter; count++)
       sum += arr[count];

    double avg = sum/counter;
    There's probably more efficient ways, but this is the easiest one I know of. From there, its a matter of another loop through the whole array and a simple if-else statement.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook