C Program calculates the sum & average of an array

In the previous tutorial, we have  seen the array definition, declaration, initialization and access array values. Now we use the array to solve real-life math problems.

C Program calculates the sum & average of an array:

In this program, you can able to calculate sum & average of an array. Including classified positive and negative sum.

#include <stdio.h>
void main()
{
int array[10];
int i, num, negative_sum = 0, positive_sum = 0;
float total = 0.0, average;
clrscr();
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]); //Inseting value into array
}
printf("Input array elements \n");
for (i = 0; i < num; i++)
{
printf("%+3d\n", array[i]);
}
/* Summation starts here*/
for (i = 0; i < num; i++)
{
if (array[i] < 0)
{
negative_sum = negative_sum + array[i]; // Calculate nevative sum
}
else if (array[i] > 0)
{
positive_sum = positive_sum + array[i]; // Calculate positive sum
}
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);
getch();
}
#include <stdio.h> void main() { int array[10]; int i, num, negative_sum = 0, positive_sum = 0; float total = 0.0, average; clrscr(); 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]); //Inseting value into array } printf("Input array elements \n"); for (i = 0; i < num; i++) { printf("%+3d\n", array[i]); } /* Summation starts here*/ for (i = 0; i < num; i++) { if (array[i] < 0) { negative_sum = negative_sum + array[i]; // Calculate nevative sum } else if (array[i] > 0) { positive_sum = positive_sum + array[i]; // Calculate positive sum } 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); getch(); }
#include <stdio.h>

void main() 
{
    int array[10];
    int i, num, negative_sum = 0, positive_sum = 0;
    float total = 0.0, average;
    clrscr();
    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]);           //Inseting value into array
    }
    printf("Input array elements \n");
    for (i = 0; i < num; i++)
    {
         printf("%+3d\n", array[i]);
    }

   /* Summation starts here*/

    for (i = 0; i < num; i++) 
    {
        if (array[i] < 0) 
        {
            negative_sum = negative_sum + array[i];    // Calculate nevative sum
        } 
        else if (array[i] > 0) 
        {
            positive_sum = positive_sum + array[i];    // Calculate positive sum
        } 
        
        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);
    getch();
}

Output:

sum avg array

C Program to Calculate the Sum of the Array Elements using Pointer:

A pointer is an object, whose value refers to another value stored elsewhere in the computer memory using its memory address. in this program, we use a pointer *ptr (pointer is declared by using * sign) which store initial address of numArray[].

#include<stdio.h>
#include<conio.h>
void main()
{
int numArray[10];
int i, sum = 0;
int *ptr;
printf("\nEnter 10 elements : ");
for (i = 0; i < 10; i++)
scanf("%d", &numArray[i]); //Accept the 10 elements from the user in the array.
ptr = numArray; // a=&a[0] We are storing the address of the array into the pointer.
for (i = 0; i < 10; i++)
{
sum = sum + *ptr;
ptr++;
}
printf("The sum of array elements : %d", sum);
}
#include<stdio.h> #include<conio.h> void main() { int numArray[10]; int i, sum = 0; int *ptr; printf("\nEnter 10 elements : "); for (i = 0; i < 10; i++) scanf("%d", &numArray[i]); //Accept the 10 elements from the user in the array. ptr = numArray; // a=&a[0] We are storing the address of the array into the pointer. for (i = 0; i < 10; i++) { sum = sum + *ptr; ptr++; } printf("The sum of array elements : %d", sum); }
#include<stdio.h>
#include<conio.h>

void main() 
{
   int numArray[10];
   int i, sum = 0;
   int *ptr;

   printf("\nEnter 10 elements : ");

   for (i = 0; i < 10; i++)
   scanf("%d", &numArray[i]);   //Accept the 10 elements from the user in the array.

   ptr = numArray; // a=&a[0] We are storing the address of the array into the pointer.

   for (i = 0; i < 10; i++)
   {
     sum = sum + *ptr;
     ptr++;
   }

 printf("The sum of array elements : %d", sum);
}

Output:

sum avg array1

C Program to Calculate Sum of all Elements of an Array using Pointers as Arguments:

One of the best things about pointers is that they allow functions to alter variables outside of their own scope. By passing a pointer to a function you can allow that function to read and write to the data stored in that variable. In this program, we use a pointer (*ptr) to catch an array address which passes through a function argument (addnum()).

#include <stdio.h>
void main() {
int array[5] = { 100, 300, 800, 400, 1000 } ;
int sum;
clrscr();
int addnum(int *ptr);
sum = addnum(array);
printf("Sum of all array elements = %d\n", sum);
getch();
}
int addnum(int *ptr) {
int index, total = 0;
for (index = 0; index < 5; index++) {
total += *(ptr + index);
}
return(total);
}
#include <stdio.h> void main() { int array[5] = { 100, 300, 800, 400, 1000 } ; int sum; clrscr(); int addnum(int *ptr); sum = addnum(array); printf("Sum of all array elements = %d\n", sum); getch(); } int addnum(int *ptr) { int index, total = 0; for (index = 0; index < 5; index++) { total += *(ptr + index); } return(total); }
#include <stdio.h>
void main() {
 int array[5] = { 100, 300, 800, 400, 1000 } ;

 int sum;
 clrscr();

 int addnum(int *ptr);

 sum = addnum(array);
 printf("Sum of all array elements = %d\n", sum);
 getch();
}
int addnum(int *ptr) {
 int index, total = 0;
 for (index = 0; index < 5; index++) {
 total += *(ptr + index);
 }
 return(total);
}

Output:

sum avg array2

 

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

One Pingback

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.