C
C
chaikovskyiii2021-12-02 14:01:22
C++ / C#
chaikovskyiii, 2021-12-02 14:01:22

How to pass an array to a C++ function (writes no matching function to call)?

void average (int *a){

}
int task3(){
    int start, rowCount, colCount;
    cout << "Enter 1 for inputting numbers from keyboard, or 2 for using random numbers: ";
    cin >> start;
    cout << "Enter the number of rows and columns" << endl << "Rows: ";
    cin >> rowCount;
    cout << "Columns: ";
    cin >> colCount;
    int a[rowCount][colCount];
    if (start == 1 ){
        for (int i=0; i<rowCount; i++)
            for (int j=0; j<colCount; j++)
            {cout << "a[" << i << "][" << j << "]="; cin >> a[i][j];}
    }
    else if (start == 2) {
        int Low = -100;
        int High = 100;
        srand((unsigned) time(NULL));
        for (int i = 0; i < rowCount; i++){
            for (int j = 0; j < colCount; j++)
                a[i][j] = Low + rand() % (High - Low + 1);}
        average(a);

    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adamos, 2021-12-02
@chaikovskyiii

Using the Crosses, it is worth using their conveniences.

std::vector< std::vector< int > > a(rowCount, std::vector< int >(colCount, 0));
void average (std::vector< std::vector< int > > &a){

So you won't run into misses in array indexes, for example. If you can.
And in your code, int[][], of course, cannot spontaneously turn into int*

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question