2

I am trying to pass a 2d array to a function in c++. The problem is that its dimension is not universal constant. I take the dimension as an input from the user and then try to pass the array. Here is what i am doind:

/*
 * boy.cpp
 *
 *  Created on: 05-Oct-2014
 *      Author: pranjal
 */
#include<iostream>
#include<cstdlib>
using namespace std;


class Queue{
private:
    int array[1000];
    int front=0,rear=0;
public:
    void enqueue(int data){
        if(front!=(rear+1)%1000){
            array[rear++]=data;
        }
    }
    int dequeue(){
        return array[front++];
    }
    bool isEmpty(){
        if(front==rear)
            return true;
        else
            return false;
    }
};

class Graph{
public:
    void input(int matrix[][],int num_h){ //this is where I am passing the matrix
        int distance;
        char ans;

        for(int i=0;i<num_h;i++){
            for(int j=0;j<num_h;j++)
                matrix[i][j]=0;
        }
        for(int i=0;i<num_h;i++){
            for(int j=i+1;j<num_h;j++){
                cout<<"Is there route between houses "<<i<<" and "<<j<<": ";
                cin>>ans;
                if(ans=='y'){
                    cout<<"Enter the distance: ";
                    cin>>distance;
                    matrix[i][j]=matrix[j][i]=distance;
                }
            }
        }
        cout<<"The matrix is: \n";
        for(int i=0;i<num_h;i++){
            cout<<"\n";
            for(int j=0;j<num_h;j++)
                cout<<matrix[i][j]<<"\t";
        }
    }
};

int main(){
    Graph g;
    int num_h;
    cout<<"Enter the number of houses: ";
    cin>>num_h;
    int matrix[num_h][num_h];
    g.input(matrix,num_h); //this is where I get an error saying
                           // Invalid arguments ' Candidates are: void input(int (*)[], 
                           // int) '
    return 0;
}

Help much appreciated. Thank you.

Pranjal
  • 621
  • 1
  • 9
  • 23
  • possible duplicate of [Passing multidimensional arrays as function arguments in C](http://stackoverflow.com/questions/4051/passing-multidimensional-arrays-as-function-arguments-in-c) – dandan78 Oct 06 '14 at 05:17
  • You're already using non-standard extensions with that VLA declaration of `matrix` in `main()`. If you want the same in your parameter list, `num_h` is going to have to come along for the ride. – WhozCraig Oct 06 '14 at 05:17
  • @WhozCraig, sorry but I am not getting you – Pranjal Oct 06 '14 at 05:39
  • @dandan78, ya we can do that one. But I don't want it to be so complicated. Already I am working on a graph problem wherein I have to use BFS and Prims algorithms. Doing what you suggested will kill my time – Pranjal Oct 06 '14 at 05:41
  • @Pranjal What I mean is the caller side is already using a non-standard VLA extension (likely you're using gcc or some such) when declaring `int matrix[num_h][num_h];`. You could continue that usage by simply declaring input as `input(int num_h, int matrix[][num_h])`. For standard compliance a different, non-VLA solution would be in-order (such as a vector of vectors or simple array of vectors). – WhozCraig Oct 06 '14 at 13:45

2 Answers2

6

Problems in your code:

Problem 1

void input(int matrix[][],int num_h){

is not valid C++. In a multi-dimensional array, all but the first dimension must be constants. A valid declaration would be:

// Define a constant at the start of the file.
const int MATRIX_SIZE 200;


void input(int matrix[][MATRIX_SIZE],int num_h){

Problem 2

int matrix[num_h][num_h];

is not valid C++. VLA are not supported in C++.

Suggested Solution

Use std::vector<std::vector<int>> to capture the 2D array.

Change

void input(int matrix[][],int num_h){

to

void input(std::vector<std::vector<int>>& matrix){
// You can get the size by calling matrix.size()
// There is no need to pass num_h as an argument.

Change the calling code to:

int main(){
    Graph g;
    int num_h;
    cout<<"Enter the number of houses: ";
    cin>>num_h;

    // Construct a 2D array of size num_h x num_h using std::vector
    std::vector<std::vector<int>> matrix(num_h, std::vector<int>(num_h));

    g.input(matrix);
    return 0;
}
R Sahu
  • 200,579
  • 13
  • 144
  • 260
  • That was the best solution. Thank you very much – Pranjal Oct 06 '14 at 05:50
  • Can you please help me with one more thing? How should I return that matrix from the input function ? – Pranjal Oct 06 '14 at 06:15
  • @Pranjal, you don't need to. If you pass it by reference, as I have suggested in my answer, the changes you make in `input` will be visible in `main`. – R Sahu Oct 06 '14 at 06:17
  • Great! I just tried it and it really works. Thanks for that. But I still wanted to know how to return that array. Maybe helpful for me in future – Pranjal Oct 06 '14 at 06:20
  • @Pranjal, you can change the return type to `std::vector>&` and use `return matrix;` in the function. – R Sahu Oct 06 '14 at 06:21
-1

Instead of passing the entire matrix, pass a pointer to the matrix. To do this effectively, you need to either treat the matrix as 2D, but manage it as a vector, or use a vector of vectors.

Considering the first case, you would:

int matrix[num_h * num_h];

and

g.input(matrix,num_h)

to

void input(int *matrix,int num_h)

and access the elements using

matrix[i * num_h + j] = 0;
Joao
  • 587
  • 1
  • 4
  • 14