Sum and Average of an Array: flowchart, algorithm and program
This program reads n numbers into an array, adds them up in a running total, and prints the sum and the average. The average is the sum divided by n, calculated as a float so the decimal part is kept.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Set sum = 0.
- Step 3: Read n.
- Step 4: Set i = 0.
- Step 5: If i < n, go to step 6; otherwise go to step 9.
- Step 6: Read a[i].
- Step 7: Set sum = sum + a[i].
- Step 8: Set i = i + 1. Go to step 5.
- Step 9: Set avg = (float)sum / n.
- Step 10: Print sum.
- Step 11: Print avg.
- Step 12: Stop.
Pseudocode
BEGIN
sum ← 0
READ n
i ← 0
WHILE i < n DO
READ a[i]
sum ← sum + a[i]
i ← i + 1
END WHILE
avg ← (float)sum / n
PRINT sum
PRINT avg
END
Program in C, Python and Java
Sum and Average of an Array program in C
#include <stdio.h>
int main() {
int n, i;
int a[100];
int sum = 0;
float avg;
printf("Enter n: ");
scanf("%d", &n);
printf("Enter %d numbers: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
sum = sum + a[i];
}
avg = (float)sum / n;
printf("Sum = %d\n", sum);
printf("Average = %.2f\n", avg);
return 0;
}Sum and Average of an Array program in Python
n = int(input("Enter n: "))
a = list(map(int, input("Enter " + str(n) + " numbers: ").split()))
sum = 0
for i in range(n):
sum = sum + a[i]
avg = sum / n
print("Sum =", sum)
print("Average = {:.2f}".format(avg))Sum and Average of an Array program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
int[] a = new int[n];
int sum = 0;
System.out.print("Enter " + n + " numbers: ");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
sum = sum + a[i];
}
float avg = (float) sum / n;
System.out.println("Sum = " + sum);
System.out.printf("Average = %.2f%n", avg);
}
}
Sample input: n = 5, numbers = 10 20 30 40 50
Output: Enter n: Enter 5 numbers: Sum = 150
Average = 30.00
How it works
- Read n, then start sum at 0 so the running total begins clean.
- The for loop runs i from 0 to n-1. Each pass reads one number into a[i] and adds it to sum with sum = sum + a[i].
- After the loop, sum holds the total of all n numbers. For 10 20 30 40 50 it grows 10, 30, 60, 100, 150.
- avg = (float)sum / n converts sum to float before dividing, so the division keeps decimals instead of cutting them off.
- printf prints sum as an integer with %d and avg with %.2f, which shows two digits after the decimal point.
Dry run (trace table)
| Step | Line | Box | sum | n | i | a[0] | a[1] | a[2] | a[3] | a[4] | avg | Output |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 3 | Start | ||||||||||
| 2 | 6 | sum = 0 | 0 | |||||||||
| 3 | 10 | Read n | 0 | 5 | Enter n: | |||||||
| 4 | 13 | i = 0 | 0 | 5 | 0 | Enter 5 numbers: | ||||||
| 5 | 13 | i < n? | 0 | 5 | 0 | |||||||
| 6 | 14 | Read a[i] | 0 | 5 | 0 | 10 | ||||||
| 7 | 15 | sum = sum + a[i] | 10 | 5 | 0 | 10 | ||||||
| 8 | 13 | i = i + 1 | 10 | 5 | 1 | 10 | ||||||
| 9 | 13 | i < n? | 10 | 5 | 1 | |||||||
| 10 | 14 | Read a[i] | 10 | 5 | 1 | 20 | ||||||
| 11 | 15 | sum = sum + a[i] | 30 | 5 | 1 | 20 | ||||||
| 12 | 13 | i = i + 1 | 30 | 5 | 2 | |||||||
| 13 | 13 | i < n? | 30 | 5 | 2 | |||||||
| 14 | 14 | Read a[i] | 30 | 5 | 2 | 30 | ||||||
| 15 | 15 | sum = sum + a[i] | 60 | 5 | 2 | 30 | ||||||
| 16 | 13 | i = i + 1 | 60 | 5 | 3 | |||||||
| 17 | 13 | i < n? | 60 | 5 | 3 | |||||||
| 18 | 14 | Read a[i] | 60 | 5 | 3 | 40 | ||||||
| 19 | 15 | sum = sum + a[i] | 100 | 5 | 3 | 40 | ||||||
| 20 | 13 | i = i + 1 | 100 | 5 | 4 |
Time and space complexity
Time: O(n) · Space: O(n). The loop runs n times doing constant work each time, and the array stores n elements.
Common mistakes
- Not setting sum = 0 at the start. A local variable holds a garbage value, so the total comes out wrong.
- Writing avg = sum / n without the (float) cast. Both are int, so integer division drops the decimal part (for example 7 / 2 gives 3, not 3.5).
- Entering n larger than 100. The array a[100] only has 100 slots, so extra values write outside it. Also, dividing by n when n is 0 is invalid.
Viva questions
Q1. Why is sum initialised to 0 before the loop?
Because sum is a running total and 0 is the neutral starting value for addition. If it were left uninitialised, it would hold a garbage value and the final sum would be wrong.
Q2. What does the loop on line 13 do, and why does it run from i = 0 to i < n?
It reads one number per iteration into a[i] and adds it to sum. Array indexes start at 0, so n elements occupy indexes 0 to n-1, which is why the condition is i < n and not i <= n.
Q3. Why is there a cast (float)sum on line 18? What would happen without it?
Both sum and n are int, so sum / n would be integer division and drop the decimal part, e.g. 7/2 would give 3. Casting sum to float forces floating-point division, so 7/2 gives 3.5.
Q4. Why do we use &a[i] in scanf but just sum in printf?
scanf needs the address of the variable so it can store the input there, hence the &. printf only needs the value to display, so no address is required.
Q5. What happens if the user enters n = 0? What if n is negative or greater than 100?
With n = 0 the loop never runs and sum stays 0, but avg = 0.0 / 0 gives NaN (division by zero in float). A negative n also skips the loop and gives a meaningless average. If n is greater than 100, scanf writes past the end of the array a[100], which is undefined behaviour or a crash.
Q6. Is the array actually needed in this program? Could you get the same result without it?
No, it isn't strictly needed. Sum is accumulated as each number is read, so you could use a single variable for the input and avoid storing all values. The array would only be necessary if you wanted to use the numbers again later, e.g. to print them or find the maximum.
Q7. What is the time and space complexity of this program?
Time is O(n) because there is a single loop that runs n times with constant work in each iteration. Space is O(n) for the array (fixed at 100 here), though O(1) would suffice if you didn't store the elements.
Q8. What would change if the numbers could be decimals like 2.5 and 3.7?
You would declare the array and sum as float (or double), and use %f in scanf and printf instead of %d. The cast on line 18 would no longer be needed, since the sum would already be floating-point.
Frequently asked questions
Why do we cast sum to float before dividing?
If sum and n are both int, C does integer division and throws away the fraction. Casting sum to float makes the division use decimals, so 7 / 2 gives 3.5.
Do we need an array to find the sum and average?
Not strictly, since you could add each number as you read it. The array is useful when you also want to use the numbers again later, such as finding the largest or printing them.
What does %.2f mean in printf?
It prints a float with exactly two digits after the decimal point. So 30 is shown as 30.00.