Initialize the sum
Before traversal, sum is 0.
C programming · Lesson
Move the index from left to right and observe how a loop visits every element exactly once.
The loop uses i as an index. Each iteration reads v[i], updates state, and then moves to the next cell.
i = 0 → v[i] → update sum → i++ → repeat until i = 5
The flowchart keeps algorithm control visible; the Array Lab below makes cells, indices, comparisons, candidates and swaps visible.
Indices stay visible at all times. Sorting traces highlight comparisons, the current candidate, swaps and the region already sorted.
Before traversal, sum is 0.
Add 4: the sum becomes 4.
Add 7: the sum becomes 11.
Add 2: the sum becomes 13.
Add 9: the sum becomes 22.
Add 1: the sum becomes 23.
The traversal visited every element exactly once.
Run C executes only the example shown on the left, using stdin on the right. The coding challenge below has a separate editor and test button.
1#include <stdio.h>
2
3int main(void)
4{
5 int v[5] = {4, 7, 2, 9, 1};
6 int somma = 0;
7
8 for (int i = 0; i < 5; i = i + 1)
9 {
10 somma = somma + v[i];
11 }
12
13 printf("%d\n", somma);
14 return 0;
15}
Read five integers into an array and print their sum using a loop.
This is a different program from lab 1. Complete the starter code: Check all test cases compiles this editor and tests every input, including boundary cases.
What is the sum of {4, 7, 2, 9, 1}?
Challenge 1 / 5 · Understanding
Progress: 0 / 5 · correct: 0
ConceptStep separates what is required from what is recommended or useful as reinforcement. Only required prerequisites block your progress.
Relationships: solid line = required · dashed line = recommended · dotted line = reinforcement.