Implementing an average counter for an array

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 3K views
clook
Messages
32
Reaction score
0
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:
#include <stdio.h>
#define MAXARR 20

main()
{
	int
		arr[MAXARR], i, counter, value, no_of_elements, max,sum;
    /*----------------------------------------------------------------------*/
    /* User is requested to enter values from keyboard until -1 is entered  */
    /*----------------------------------------------------------------------*/
    printf("\f");
	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)
	{
		arr[counter]=value;
		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");
    for(i=0;i<no_of_elements;i=i+1)
		printf("%d  ",arr[i]);
 
Physics news on Phys.org
Code:
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.