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.
Key Architecture Takeaways
- ›Point 1: Core computer science and engineering computing concept: C Program calculates the sum & average of an array.
- ›Point 2: Detailed trace of memory representation, algorithmic complexity, and control structures.
- ›Point 3: Includes portable ISO C (C17/C23) code examples verified with modern GCC and Clang.
Compiler & Toolchain Modernization
While legacy educational environments used 16-bit DOSBox Turbo C++, all code presented here adheres to standard ANSI/ISO C standards. You can compile these programs cleanly on modern Linux, Windows (MSYS2/WSL), or macOS using:
> gcc -Wall -Wextra -O2 program.c -o program
> ./program
> ```
---
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.c #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:
[](https://www.technhit.in/wp-content/uploads/2016/08/sum-avg-array.png)
## 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[].c #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:
[](https://www.technhit.in/wp-content/uploads/2016/08/sum-avg-array1.png)
## 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 <i class="EMPHASIS">and write</i> 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()).c #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:

