HI guys i am a beginner c++ programmer so hopefully this one will be easy for the gurus.
i am trying to write a class to do matrix multiplication and then use it. In the class are 5 functions: one to define the dimensions of the matrix
two to enter the two matrices
one to do the multiplication
one to show the result

Ifi run the code it reads in the matrices and successfully does the multiplication (as i have inserted test code into the multiplication function to check the results)
However, the function 'show' just outputs junk to the screen.
My issue is this: if the function 'multiply' works then 'multiply' is clearly able to read in the 2d arrays 'M1' and 'M2'. Then why can't the function 'show' read from the 2d array 'Manswer1'? I don't understand why one function can get values from arrays, but the other can't.

Hope you can help,
Mark


Here is the code in full:

#include <iostream>
using namespace std;

class matmult{

private:
	int M1m, M1n, M2m, M2n;
	int M1[100][100];
	int M2[100][100];	
	int Manswer1[100][100];
	int g[100][100];
	int x, y, a;


public:
	void enterdimensions();
	void entermatrix1();
	void entermatrix2();
	void multiply();
	void showresult();
	matmult();

};




void matmult::enterdimensions(){
	cout <<" enter the dimensions of matrix 1 (n*m)"<<endl;
	cout << "n=" << endl;
	cin >> M1n;
	cout << "m=" << endl;
	cin >> M1m;
	cout <<" enter the dimensions of matrix 2 (n*m)"<<endl;
	cout << "n=" << endl;
	cin >> M2n;
	cout << "m=" << endl;
	cin >> M2m;
	if (M1m!=M2n){cout << "matricies not multipliable. M1m MUST equal M2n"<<endl;}
								}

void matmult::entermatrix1(){	
	cout << "enter first matrix elements" << endl;
	for(y=0; y<M1n;y++){
	for(x=0; x<M1m; x++){
	cout << "enter row number " << y+1 << ", column number " << x+1 << endl;
	cin >> M1[y][x];
							}
							}
							}
void matmult::entermatrix2(){
	cout << "enter second matrix elements" << endl;
	for(y=0; y<M2n;y++){
	for(x=0; x<M2m; x++){
	cout << "enter row number " << y+1 << ", column number " << x+1 << endl;
	cin >> M2[y][x];
							}
							}						
							}

void matmult::multiply(){
	int Manswer1[100][100]={0,0,0,0,0,0,0,0};
	for (a=0; a<M1n; a++){	
	for(y=0; y<M2m; y++){
	for(x=0; x<M1m; x++){
	Manswer1[a][y] += (M1[a][x]*M2[x][y]);
						}
						}
						}
	cout << " M1 x M2 = "<< endl;	//test code to see
	for(y=0; y<M1n; y++){			//if the multiplication
	for(x=0; x<M1n;x++){			// was successful
	cout.width(6);
	cout << Manswer1[y][x] << "    ";
						}
						cout << endl;
						}
						}




void matmult::showresult(){

	
	cout << "m1n= " <<M1n <<endl;
	cout << "Manswer1[0][0]= " << Manswer1[0][0] << endl;
	cout << " M1 x M2 = "<< endl;
		for(y=0; y<M1n; y++){
		for(x=0; x<M1n;x++){
		cout.width(6);
		cout << Manswer1[y][x] << "    ";
						}
		cout << endl;
						}
						}



matmult::matmult(){cout << "instance built successfully"<<endl;}



void main (){

matmult(multiply);
multiply.enterdimensions();
multiply.entermatrix1();
multiply.entermatrix2();
multiply.multiply();
multiply.showresult();


}

Don't re-declare another array with the same name as the one in global scope.

Also you may want to initialize your Manswer array in global scope to be full of zeros.

void matmult::multiply(){
  //int Manswer1[100][100]={0,0,0,0,0,0,0,0}; // you were masking the global Manswer here
for (a=0; a<M1n; a++){
for(y=0; y<M2m; y++){
for(x=0; x<M1m; x++){
Manswer1[a][y] += (M1[a][x]*M2[x][y]);
}
}
}
cout << " M1 x M2 = "<< endl; //test code to see
for(y=0; y<M1n; y++){ //if the multiplication
for(x=0; x<M1n;x++){ // was successful
cout.width(6);
cout << Manswer1[y][x] << " ";
}
cout << endl;
}
}

Edit: Just realize you're using a Class - instead of trying to initialize Manswer in global scope you can do so in the constructor of your class.

class matmult{

private:
          int Manswer1[100][100];

public:
           matmult(){int temp[100][100]={0,0,0,0,0,0,0,0}; Manswer1 = temp;}
};

Thanks for the quick reply!

The reason i re-declared was because when i tried to initialise Manswer1 in the global variable declaration by using:

int Manswer1[100][100]= {0, 0, 0, 0, 0, 0}

I get syntax errors. Totally confused me. I need to initialise it somewhere or the multiply loop won't work, but how/where to initialise it? in the constructor?

sorry-just read your edit, i'll try that

Thanks for the quick reply!

The reason i re-declared was because when i tried to initialise Manswer1 in the global variable declaration by using:

int Manswer1[100][100]= {0, 0, 0, 0, 0, 0}

I get syntax errors. Totally confused me. I need to initialise it somewhere or the multiply loop won't work, but how/where to initialise it? in the constructor?

Yes in the constructor, which is why I edited my post when I realized you were using a class.

Thanks that worked a treat!
awesome quick reply

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.