C: Fix the overrun

This program goes past the end of its array. Fix it.

What to build

A corrected copy of the starter code.

Requirements

  • Find and fix the bugs. There are two.
  • Read n numbers, and print them doubled, separated by single spaces.

Input

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

Output

One line.

Constraints

n is from 1 to 10.

Example

Input:

3
1 2 3

Output:

2 4 6

Why it works

An array of size n has indexes 0 to n - 1. In real C, writing one element too many silently changes whatever lies next in memory.

Examples

Example 1 · Input
3
1 2 3
Expected output
2 4 6
Example 2 · Input
1
5
Expected output
10

Hints

Hint 1

Which index does the loop reach on its last round?

Hint 2

Look at both loops, and at the size of the array.

Solution

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

int main(void) {
    int n, a[10];
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    for (int i = 0; i < n; i++) {
        printf(i ? " %d" : "%d", a[i] * 2);
    }
    printf("\n");
    return 0;
}

Lesson: Bugs C will not catch →