matrix operation not working!!!

Please help, this program is meant to create two matrices and gives the user the choice to chose the operation they want to use to make the new matrix.

the problem that I am having is that its not giving the correct sum for the matrix.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
using namespace std;

int matrixA[2][3];
	int matrixB[2][3];
	int matrixC[2][3];
	int i, j;

void add(){
	for( i=0;i<2;i++){
		for( j=0;j<3;j++){
			matrixC[i][j]=matrixA[i][j]+matrixB[i][j];
			}
	}
}
void sub(){
	for( i=0;i<2;i++){
		for( j=0;j<3;j++){
			matrixC[i][j]=matrixA[i][j]-matrixB[i][j];
			}
	}
}
void multi(){
	for( i=0;i<2;i++){
		for( j=0;j<3;j++){
			matrixC[i][j]=matrixA[i][j]*matrixB[i][j];
			}
	}
}
int div(){
	for( i=0;i<2;i++){
		for( j=0;j<3;j++){
			matrixC[i][j]=matrixA[i][j]/matrixB[i][j];
			}
	}
}

//void printMatrix(){
//}

int main(){
	int op;
	
	// GETING VALUSE FORM A AND B
	cout << "Enter matrix A :" << endl;
	for( i=0;i<2;i++){
		for( j=0;j<3;j++){
			cin >> matrixA[i][j];
			}
	}
	
	cout << "Enter matrix B :" << endl;
	for( i=0;i<2;i++){
		for( j=0;j<3;j++){
			cin >> matrixB[i][j];
			}
	}
	// CHOSING OPPERATION 1
	cout<<"----------------------------------------"<<endl;
	cout<<" Choose Operation to Compute Matrix C "<<endl;
	cout<<"----------------------------------------"<<endl;
	cout<<" Enter 1 to Add"<<endl;
	cout<<" Enter 2 to Subtract"<<endl;
	cout<<" Enter 3 to Multiply"<<endl;
	cout<<" Enter 4 to Divide"<<endl;
	cout<<"----------------------------------------"<<endl;
	cout<<"Enter Your Choice: ";
	cin>>op;

	// CHOSING OPPERATION 2
	switch(op){
		case 1: add();
		case 2: sub();
		case 3: multi();
		case 4: div();
	}
	
	// DISLAYING C
	cout << "Matrix C :" << endl;
	for( i=0;i<2;i++){
		for( j=0;j<3;j++){
			cout << matrixC[i][j] << " ";
			}
		cout << endl;
	}
	
	
	system("pause");
	} 
Last edited on
You have no breaks in the switch so after it has added the matrices it will carry on with the sub(), multi() and div().
Hi. As Peter said, you should have used break statements in each case here. I have boldfaced them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
using namespace std;

int matrixA[2][3];
	int matrixB[2][3];
	int matrixC[2][3];
	int i, j;

void add(){
	for( i=0;i<2;i++){
		for( j=0;j<3;j++){
			matrixC[i][j]=matrixA[i][j]+matrixB[i][j];
			}
	}
}
void sub(){
	for( i=0;i<2;i++){
		for( j=0;j<3;j++){
			matrixC[i][j]=matrixA[i][j]-matrixB[i][j];
			}
	}
}
void multi(){
	for( i=0;i<2;i++){
		for( j=0;j<3;j++){
			matrixC[i][j]=matrixA[i][j]*matrixB[i][j];
			}
	}
}
int div(){
	for( i=0;i<2;i++){
		for( j=0;j<3;j++){
			matrixC[i][j]=matrixA[i][j]/matrixB[i][j];
			}
	}
}

//void printMatrix(){
//}

int main(){
	int op;
	
	// GETING VALUSE FORM A AND B
	cout << "Enter matrix A :" << endl;
	for( i=0;i<2;i++){
		for( j=0;j<3;j++){
			cin >> matrixA[i][j];
			}
	}
	
	cout << "Enter matrix B :" << endl;
	for( i=0;i<2;i++){
		for( j=0;j<3;j++){
			cin >> matrixB[i][j];
			}
	}
	// CHOSING OPPERATION 1
	cout<<"----------------------------------------"<<endl;
	cout<<" Choose Operation to Compute Matrix C "<<endl;
	cout<<"----------------------------------------"<<endl;
	cout<<" Enter 1 to Add"<<endl;
	cout<<" Enter 2 to Subtract"<<endl;
	cout<<" Enter 3 to Multiply"<<endl;
	cout<<" Enter 4 to Divide"<<endl;
	cout<<"----------------------------------------"<<endl;
	cout<<"Enter Your Choice: ";
	cin>>op;

	// CHOSING OPPERATION 2
	switch(op){
		case 1: add(); break;
		case 2: sub(); break;
		case 3: multi(); break;
		case 4: div(); break;
	}
	
	// DISLAYING C
	cout << "Matrix C :" << endl;
	for( i=0;i<2;i++){
		for( j=0;j<3;j++){
			cout << matrixC[i][j] << " ";
			}
		cout << endl;
	}
	
	
	system("pause");
	} 
thank you so much
i am so sorry i forgot to say thanks and mark this problem as solved
Topic archived. No new replies allowed.