Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 918
Complexicator Avatar asked Sep 06 '25 23:09

Complexicator


1 Answers

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();
        }     
    }
}
like image 79
Barmar Avatar answered Sep 09 '25 20:09

Barmar