This is C Program to calculate the sum & average of an array.

This program will declare an array and then add all the array elements and finds the average of the array.

1. Create an array of fixed size (maximum capacity), lets say 10.

2. Take n, a variable which stores the number of elements of the array, less than maximum capacity of array.

3. Iterate via for loop to take array elements as input, and print them.

4. Iterate via for loop to access each element of array to get sum (sum of positive integers and negative integers are separate) and overall sum to calculate average.

5. Sum of positive and negative numbers are shown.

6. Average is calculated by dividing overall sum to the number of elements in array.

Here is source code of the C program to calculate the sum & average of an array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

`/*`

`* C program to read N integers into an array A and`

`* a) Find the sum of negative numbers`

`* b) Find the sum of positive numbers`

`* c) Find the average of all numbers`

`* Display the results with suitable headings`

`*/`

`#include <stdio.h>`

`#define MAXSIZE 10`

void main()

`{`

int array[MAXSIZE];

int i, num, negative_sum = 0, positive_sum = 0;

float total = 0.0, average;

printf ("Enter the value of N \n");

scanf("%d", &num);

printf("Enter %d numbers (-ve, +ve and zero) \n", num);

for (i = 0; i < num; i++)

`{`

scanf("%d", &array[i]);

`}`

printf("Input array elements \n");

for (i = 0; i < num; i++)

`{`

printf("%+3d\n", array[i]);

`}`

`/* Summation starts */`

for (i = 0; i < num; i++)

`{`

if (array[i] < 0)

`{`

negative_sum = negative_sum + array[i];

`}`

else if (array[i] > 0)

`{`

positive_sum = positive_sum + array[i];

`}`

else if (array[i] == 0)

`{`

`;`

`}`

total = total + array[i] ;

`}`

average = total / num;

printf("\n Sum of all negative numbers = %d\n", negative_sum);

printf("Sum of all positive numbers = %d\n", positive_sum);

printf("\n Average of all input numbers = %.2f\n", average);

`}`

1. Create an array of integer of some certain maximum capacity (10, in this case).

2. From users, take a number N as input, which will indicate the number of elements in the array (N <= maximum capacity).

3. Iterating through for loops (from [0 to N) ), take integers as input from user and print them. These input are the elements of the array.

4. Now, start summation by iterating through all the elements and adding positive and negative number separately to print as well as combined to calculate average.

5. To calculate average, the overall sum (sum of positive + negative integers) is divided by total number of elements in the array.

6. Print the average calculated.

Enter the value of N 10 Enter 10 numbers (-ve, +ve and zero) -8 9 -100 -80 90 45 -23 -1 0 16 Input array elements -8 +9 -100 -80 +90 +45 -23 -1 +0 +16 Sum of all negative numbers = -212 Sum of all positive numbers = 160 Average of all input numbers = -5.20

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

Here’s the list of Best Reference Books in C Programming, Data Structures and Algorithms.