Q:

Array - runtime error: index out of bounds for type 'int [*]'

The user can enter two integer number n, k: k < n.
Now one must enter n arbitrary integer numbers which i want to store in an array.
#include <iostream>
using namespace std;

int main(){
    int n, k;
    int scores[n];  
    cin >> n >> k;
    
    for (int i = 0; i < n; i ++){
        cin >> scores[i];
    }   
}

For n = 5 , k = 3 I get the following error message after entering the third random number
untitled.cpp:13:18: runtime error: index 3 out of bounds for type 'int [*]'

How can scores[3] be out of bounds if the total array "scores" consists of 5 elements?

A:

Before reading data, n is not initialized and have indeterminate value.
Usages of n must be after reading.
#include <iostream>
using namespace std;

int main(){
    int n, k;  
    cin >> n >> k;
    int scores[n]; // move this
    
    for (int i = 0; i < n; i ++){
        cin >> scores[i];
    }   
}

Also note that variable-length array like this is not in the standard of C++. Consider using std::vector instead.
#include <iostream>
#include <vector>
using namespace std;

int main(){
    int n, k;  
    cin >> n >> k;
    vector<int> scores(n);
    
    for (int i = 0; i < n; i ++){
        cin >> scores[i];
    }   
}

