Complexicator Complexicator - 3 months ago 11
C++ Question

Initializing a 2D array with random numbers

I am trying to initialize my array in a function with random values, so I can (later) sort it.

static int a[10][100000]; //declared in int main()

void init(int [10][100000]) {
for (int i = 0; i <10; i++){
a[i] = rand();
for(int k = 0; k < 100000; k++){
a[k] = rand();
}
}
}


Any help would be appreciated

Answer

First, you got the variable wrong. The argument is arr, not a.

a[i] = rand() makes no sense. a[i] is a whole row, you can't assign a number to it. To access an element of a 2-d array, use two subscripts.

void init(int arr[10][100000]) {
    for (int i = 0; i <10; i++){
        for(int k = 0; k < 100000; k++){
            arr[i][k] = rand();
        }     
    }
}