Zygodactyl546
Zygodactyl546

Reputation: 21

i can't get my simple program to pass a 2d array to a function

ive tried several ways but i can't get the function "Matrixinputs" to accept the 2d array by reference. "Matrixinputs" is going to change the inputs of the array to what ever the user chooses. i am a beginner but i think it has something to do with the fact that i'm trying to pass a dynamic array whose parameters are defined by the user, but that just a guess. please help, i errors get like these

matrix2.C:15:11: error: invalid operands of types ‘int [(((sizetype)(((ssizetype)a) + -1)) + 1)][(((sizetype)(((ssizetype)b) + -1)) + 1)]’ and ‘int [(((sizetype)(((ssizetype)c) + -1)) + 1)][(((sizetype)(((ssizetype)d) + -1)) + 1)]’ to binary ‘operator*’
  cour<<m1*m2;

or

$ g++ matrix.C -omatrixs -lm
matrix.C: In function ‘int main()’:
matrix.C:16:25: error: expression list treated as compound expression in initializer [-fpermissive]

and here is my code

#include <iostream>
#include <cmath>
using namespace std;
//Prototypes
double Matrixsettings(int&, int&, int&, int&);
int Matrixinputs(int&, int &);

int main()
{
    int a=2, b=2, c=2, d=2;
    cout<<  Matrixsettings( a,b,c,d);
    cout<< a<<b<<c<<d;
    int m1 [a] [b], m2 [c] [d];
    cout<<m1 [a] [b]<< m2 [c] [d];
    int Matrixinputs(m1 [a] [b],m2 [c] [d]);
return 0;
}

double Matrixsettings( int &a, int &b,  int &c, int &d)
{ 
    cout<< "how many rows in the first matrix: ";
    cin>> a;
    cout<< "how many columns in the first matrix: ";
    cin>> b;
    cout<< "how many rows in the second matrix: ";
    cin>> c;
    cout<< "how many columns in the second matrix: ";
    cin>> d;
    return 0;
}

int Matrixinputs(m1& [a] [b],m2& [c] [d]);
{
//this function will have a loop with cout and cin line defining each input of the matrix like array array
}

Upvotes: 0

Views: 118

Answers (2)

BLUEPIXY
BLUEPIXY

Reputation: 40145

It is convenient to use a vector, but an example of using new.

#include <iostream>

using namespace std;

template <typename T>
T **make_2d(int r, int c){
    T** array_2d = new T*[r];
    for(int i=0;i<r;++i)
        array_2d[i] = new T[c];
    return array_2d;
}

template <typename T>
void drop_2d(T** matrix, int r){
    for(int i=0;i<r;++i)
        delete[] matrix[i];
    delete[] matrix;
}

typedef int** Matrix;

void MatrixInputs(Matrix &m, int r, int c){
    for(int i = 0;i < r; ++i){
        for(int j = 0; j < c;++j){
            cout << "[" << i << "][" << j << "] >";
            cin >> m[i][j];
        }
    }
}

int main(){
    int a,b;
    cin >> a;
    cin >> b;

    Matrix m = make_2d<int>(a, b);

    MatrixInputs(m, a, b);

    //print
    for(int i=0;i<a;++i){
        cout << "[ " ;
        for(int j=0;j<b;++j){
            cout << m[i][j] << " ";
        }
        cout << "]" << endl;
    }
    drop_2d(m, a);
}

Upvotes: 0

SliceSort
SliceSort

Reputation: 375

void Matrixinputs(int* matrix, int row, int column);
{
//this function will have a loop with cout and cin line defining each input of the matrix like array array

for (int i=0; i<row; i++)
{
    for (int j=0; j<column; j++)
    {
        cin >> matrix[i*row+column];
    }
}
}

Outside this function, manually alocate memory to the matrix pointer.

Upvotes: 1

Related Questions