C Program to find sum & average of positive and negative numbers in an array


January 9, 2023, Learn eTutorial
2153

What is an Array in C?

An Array is a collection of elements of the same data type stored in a sequence of memory locations that can be accessed by the index variable.

An Array will be declared as data type array_name [size], which is "int array[23]". it uses sequential memory locations, so the lowest address (starting address) also called the base address, that has the first element, and the highest address has the last element of the array.

How to calculate the average and sum of positive and negative numbers using C?

In this C program, we are adding the user input into an Array, and so we can access the elements by incrementing the array index by one. Now we display the numbers using another for loop. After that, open another for loop to check whether each element is Positive, Negative, or Zero.

  • If it is Positive, it gets added to the positive sum variable.
  • If it is Negative, it gets added to the negative sum variable.
  • else it is zero.

Then we calculate the Total sum by adding the numbers to the Total. Finally, we calculate the Average by dividing the Total by the number of elements and display the result separately.

ALGORITHM

STEP 1: Include the Header files into the C program to use the built-in functions.

STEP 2: Declare and Initialize the variables and the Array.

STEP 3: Accept the number of terms needed from the user using printf and scanf.

STEP 4: Open 'for loop' to accept numbers and add that to Array.

STEP 5: Display the elements using another 'for loop' .

STEP 6: Open a for loop from '0' to the 'number of terms' to check each element.

STEP 7: Using an 'if' condition check if the number is less than zero, add that to Negative Sum.

STEP 8: Else if the number is greater than zero, add that to the Positive Sum.

STEP 9: Else add the element is zero and nothing to be done.

STEP 10: Add the Array element with the Total Sum in each iteration of the 'for loop'.

STEP 11: Calculate the 'Average' by dividing the Total Sum by the total number of elements.

STEP 12: print the Positive Sum, Negative Sum, and Average using printf in C programming language.


This program uses the below concepts in C programming, we recommend reading these topics to get a better understanding.

C Source Code

                                          #include <stdio.h>
#define MAXSIZE 10               /* defines array size 10 */

void main() 
{
   int array[MAXSIZE];
   int i, N, negsum = 0, posum = 0;
   float total = 0.0, averg;
   printf("Enter the value of N\n");
   scanf("%d", & N);
   printf("Enter %d numbers (-ve, +ve and zero)\n", N);            /* enter the user input into the array we defined */
   for (i = 0; i < N; i++) 
   {
      scanf("%d", & array[i]);
      fflush(stdin);
   }
   printf("Input array elements\n");
   for (i = 0; i < N; i++) 
   {
      printf("%+3d\n", array[i]);            /* prints the values inside of the array using 3 positions*/
   }

  /* Summing  begins */

  for (i = 0; i < N; i++) 
  {
      if (array[i] < 0) 
      {
         negsum = negsum + array[i];                 /* if number is negative it gets added to negative sum */
      }
     else if (array[i] > 0) 
     {
        posum = posum + array[i];                      /* if number is positive it gets added to positive sum */
     }
    else if (array[i] == 0) 
    {
        ;
    }
     total = total + array[i];
   }
   averg = total / N;                  /* calculating average */
   printf("\nSum of all negative numbers    = %d\n", negsum);           /* displays the output */
   printf("Sum of all positive numbers    = %d\n", posum);
   printf("\nAverage of all input numbers   = %.2f\n", averg);
} 
                                      

OUTPUT

Enter the value of N
5

Enter 5 numbers (-ve, +ve and zero)
5
-3
0
-7
6

Input array elements
+5
-3
+0
-7
+6
Sum of all negative numbers    = -10

Sum of all positive numbers    = 11

Average of all input numbers   = 0.20