Lets write a C program to calculate Sum and Average of N numbers using Arrays and using macros and for loop.
Related Read:
Calculate Sum and Average of N Numbers without using Arrays: C Program
Page Contents
Formula To Calculate Sum and Average
int a[5] = {2, 4, 6, 5, 9};
sum = 2 + 4 + 6 + 5 + 9;
average = sum / 5.0;
Result
sum = 26;
average = 5.2;
Important Note:
Look at the formula for calculating average. If you divide any number by integer number, it’ll only return integer value and discard the digits after decimal point. So make sure to divide the number by floating point value. To convert integer to float, make use of typecasting syntax.
Typecasting
int N = 5;
sum = 2 + 4 + 6 + 5 + 9;
average = sum / (float)N;
Since N is integer type variable, dividing any number by N would give us integer data. For some input it’ll result in wrong result. To fix it, we make use of typecasting and cast the type of N to float using above syntax.
Example: Expected Output
Enter 5 integer numbers
5
2
6
4
3
Sum of 5 numbers: 20
Average of 5 numbers: 4.000000
Video Tutorial: Calculate Sum and Average of N Numbers using Arrays: C Program
[youtube https://www.youtube.com/watch?v=K5Q4i7jyrAo]
Source Code: Calculate Sum and Average of N Numbers using Arrays: C Program
Method 1
#include<stdio.h> #define N 5 int main() { int a[N], i, sum = 0; float avg; printf("Enter %d integer numbers\n", N); for(i = 0; i < N; i++) scanf("%d", &a[i]); for(i = 0; i < N; i++) { sum = sum + a[i]; } avg = sum / (float)N; printf("\nSum of %d numbers: %d\n", N, sum); printf("\nAverage of %d numbers: %f\n", N, avg); return 0; }
Output 1:
Enter 5 integer numbers
2
5
6
8
10
Sum of 5 numbers: 31
Average of 5 numbers: 6.200000
Output 2:
Enter 5 integer numbers
1
2
3
4
5
Sum of 5 numbers: 15
Average of 5 numbers: 3.000000
Logic To Calculate Sum and Average of Array Elements
We ask the user to input N integer numbers. N is a macro and we’ve assigned 5 to it. Integer numbers input by the user is stored inside array variable a[N]. N being the size of the array.
SUM
We use for loop and iterate N times to fetch all the array elements. Variable i is assign a initial value of 0 and for loop iterates until i < N. Inside for loop we add the value of individual array element(a[i]) to previous value of variable sum. Once i < N condition is false, control exits for loop.
AVERAGE
Outside for loop we calculate average by using the formula:
average = sum / (float)N;
Once sum and average are calculated we output the result on to the console window.
Source Code: Calculate Sum and Average of N Numbers using Arrays: C Program
Method 2: Improved Source Code
#include<stdio.h> #define N 5 int main() { int a[N], i, sum = 0; float avg; printf("Enter %d integer numbers\n", N); for(i = 0; i < N; i++) { scanf("%d", &a[i]); sum = sum + a[i]; } avg = sum / (float)N; printf("\nSum of %d numbers: %d\n", N, sum); printf("\nAverage of %d numbers: %f\n", N, avg); return 0; }
You could even calculate sum inside the for loop which is used to accept input from the user. Whenever user inputs a number it is immediately added to the previous value of variable sum.
Advantages of using above source code
We don’t iterate through the array to fetch individual elements of the array and then add it to calculate sum. We calculate sum inside first for loop itself which is used to accept input by user. This method is faster and cheaper on resource usage.
Important Notes:
1. Make use of macros to assign size of array.
2. Make sure to declare variable avg(to calculate average) as float or double.
3. Make sure to divide variable sum by floating point or double type value and not by integer. In order to convert a integer variable or macro, make use of typecasting.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert