Errors in the Given Program

C++ language (Article) (Program)

11

Program:

int numbers[5];
for (int i = 0; i <= 5; i++) {
    cin >> riumbers[i];
}

Output:


                                        

Explanation:

Identified Errors

  1. Array index out of range

    • Loop condition should be i < 5, not i <= 5

  2. Variable name misspelled

    • riumbers[i] should be numbers[i]

CORRECTED VERSION:


#include <iostream>
using namespace std;

int main() {
    int numbers[5];

    for (int i = 0; i < 5; i++) {
        cin >> numbers[i];
    }

    cout << "First element is: " << numbers[0] << endl;

    return 0;
}