Complexicator
Complexicator

Reputation: 71

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

Upvotes: 4

Views: 5642

Answers (1)

Barmar
Barmar

Reputation: 782105

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();
        }     
    }
}

Upvotes: 5

Related Questions