0

I'm trying to make a C++ program where first the user declare a 2d array, I'm using this:

#include <iostream>
#include <ctime> 
using namespace std;

int main() {
    int i,o; 
    cout << "Ingress rows: ";
    cin >> i; 
    cout << "Ingress columns: "; 
    cin >> o;
    int array1[i][o];
}

So, what I want to do is a function which receives those values and fill the 2d array with random numbers, here is as far as I get (after trying and trying for hours):

void fill_array(int (*arr)[], size_t fila, size_t col) {
    srand(time(NULL));
    for (size_t i = 0; i < fila; i++) {
        for (size_t > j = 0; j < col; j++) {
           arr[i][j] = rand() % 10 + 20;
        }
    }
}

I constantly get the errors:

error: cannot convert 'int ()[o]' to 'int' for argument '1' to 'void fill_array(int, size_t, size_t)' fill_array(array1,i,o);

Any kind of help is more than welcome. Thanks.

1
  • 1
    Please format your question. The editor has a live preview and all the help tooltips you need. Also include the complete and exact error message so we can actually help you. Commented May 2, 2018 at 14:25

1 Answer 1

2

Use vectors. Dynamic arrays int array1[i][o]; are not documented in C++ standard.

...
#include <vector>

void fill_array(std::vector<std::vector<int>> &arr) {
    srand(time(NULL));
    for (size_t i = 0; i < arr.size(); i++) {
        for (size_t j = 0; j < arr[i].size(); j++) {
            arr[i][j] = rand() % 10 + 20;
        }
    }
}

int main() {
    ...
    std::vector<std::vector<int>> array1(i, std::vector<int>(o));
    fill_array(array1);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.