C: Fix the total

This program should add up some numbers, but it has a bug.

What to build

A corrected copy of the starter code.

Requirements

  • Find the bug and fix it.
  • Print the sum of the numbers.

Input

The first line has n. The second line has n whole numbers.

Output

One number.

Constraints

n is from 1 to 100.

Example

Input:

3
1 2 3

Output:

6

Why it works

A local variable holds leftover bytes until it is given a value. On a real computer this program might even seem to work, which is what makes the bug so dangerous.

Examples

Example 1 · Input
3
1 2 3
Expected output
6
Example 2 · Input
1
9
Expected output
9

Hints

Hint 1

Run the starter and read what StepVioz says.

Hint 2

Which variable is read before it has a value?

Solution

Show one correct solution (try it yourself first)
#include <stdio.h>

int main(void) {
    int n, total = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int x;
        scanf("%d", &x);
        total += x;
    }
    printf("%d\n", total);
    return 0;
}

Lesson: Bugs C will not catch →