Passing Arrays

I'm creating a program for doing different mathematics with matrices. One of the stipulations is that we are not allowed to use pointers, but in order to account for this we made the number of columns constant for both of the two matrices we are working with. This means that we have to return a matrix from a function. I have tried pretty much everything and all Google points me to is pointers. Help??? Thanks in advance.





#include <cstdlib>
#include <string>
#include <iostream.h>
#include <math.h>
#include <fstream.h>
#include <iomanip.h>
#include <ctype.h>
using namespace std;
const int column(3);
void MatrixSize(int &r1, int &r2);
void MatrixInput(int a1[][column], int r1, int a2[][column], int r2);
void printMatrix(int a1[][column], int r1);
int Add(int a1[][column], int r1, int a2[][column]);

int main ()
{
int ra(0), rb(0);
MatrixSize(ra, rb);

int A[ra][column];
int B[rb][column];
MatrixInput(A, ra, B, rb);
Add(A,ra,B);

return 0;
}

void MatrixSize(int &r1, int &r2)
{
cout<<"Input the number of rows of the first Matrix"<<endl;
cin>>r1;
cout<<"Input the number of rows of the second Matrix"<<endl;
cin>>r2;
}

void MatrixInput(int a1[][column], int r1, int a2[][column], int r2)
{
int value(0);
cout<<"Insert values for each cell in the Matrices."<<endl;
cout<<"For the First Matrix:"<<endl;
for(int r=0; r<r1; r++)
{
for(int c=0; c<column; c++)
{
cout<<"Value for row "<<r<<"column "<<c<<":"<<endl;
cin>>value;
a1[r][c]=value;
}
}

cout<<"For the Second Matrix:"<<endl;
for(int r(0); r<r2; r++)
{
for(int c(0); c<column; c++)
{
cout<<"Value for row "<<r<<"column "<<c<<":"<<endl;
cin>>value;
a2[r][c]=value;
}
}

}

Add(int a1[][column], int r1, int a2[][column])
{
int sum[r1][column];
for(int r(0); r<r1; r++)
{
for(int c(0); c<column; c++)
{
sum[r][c]=a1[r][c]+a2[r][c];
}
}

return sum;
}
May I suggest using an object. So define your object, then define some access functions. Then pass the object around by reference using the "&" reference symbol.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Matrix {
  int M[30][40];
  int & operator()(int i, int j) { return M[i][j]; }
}
int add( Matrix & m ) {
  ... // ADD STUFF
}
int main() {
  Matrix m;
  ...
  int r = add(m);
  ...
  return 0;
}

Topic archived. No new replies allowed.