Matrices #2- Please Help Fellow Programmers!

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

char menu(char);
int row, col;

int main()
{
	int temp;
	char op, select;
	
	cout << "Welcome!";
	cout << "\nEnter 2 numbers for the row and column of the matrices:"
	     << "\n(Space in between) ";
	cin >> row >> col;
	
	float A[row][col], B[row][col], C[row][col];
	
	cout << "Input Matrix A values: ";
	for(int b = 0; b < row; b++)
	{
		for(int e = 0; e < col; e++)
		{
			cin >> A[b][e];
		}
	}
	
	cout << "Input Matrix B values: ";
	for(int b1 = 0; b1 < row; ++b1)
	{
		for(int e1 = 0; e1 < col; ++e1)
		{
			cin >> B[b1][e1];
		}
	}
	
	do
	{
		select = menu(op);
		if(select == '?')
		{
			menu(op);
		}
		if(select == 'i')
		{
			cout << "\nEnter 2 numbers for the row and column of the matrices:"
	     		 << "\n(Space in between) ";
			cin >> row >> col;
			
			cout << "Input Matrix A values: ";
			for(int b = 0; b < row; b++)
			{
				for(int e = 0; e < col; e++)
				{
					cin >> A[b][e];
				}
			}
	
			cout << "Input Matrix B values: ";
			for(int b1 = 0; b1 < row; ++b1)
			{
				for(int e1 = 0; e1 < col; ++e1)
				{
					cin >> B[b1][e1];
				}
			}
		}
		if(select == 'a')
		{
			for (int c = 0; c < row; ++c)
			{
				for (int c1 = 0; c1 < col; ++c1)
				{
					C[c][c1] = A[c][c1] + B[c][c1];
				}
			}
			for (int i = 0; i < row; i++)
			{
				for(int j = 0; j < col; j++)
				{
					cout << A[i][j] << "   ";
				}
				for(int j = 0; j < col; j++)
				{
					cout << B[i][j] << "   ";
				}
				for(int j = 0; j < col; j++)
				{
					cout << "\t";
					cout << C[i][j] << "   ";
				}
				cout << endl;
			}
		}
		
		if(select == 's')
		{
			for (int c2 = 0; c2 < row; ++c2)
			{
				for (int c3 = 0; c3 < col; ++c3)
				{
					C[c2][c3] = A[c2][c3] - B[c2][c3];
				}
				
			}
			for (int i = 0; i < row; i++)
			{
				for(int j = 0; j < col; j++)
				{
					cout << A[i][j] << "   ";
				}
				for(int j = 0; j < col; j++)
				{
					cout << B[i][j] << "   ";
				}
				for(int j = 0; j < col; j++)
				{
					cout << "\t";
					cout << C[i][j] << "   ";
				}
				cout << endl;
			}
		}
		if (select == 'm')
		{
			for(int i=0;i<row;i++)
			{
				for(int j=0;j<col;j++)
				{
					C[i][j] = 0;
					for(int k=0;k<col;k++)
					C[i][j] = C[i][j] + (A[i][k]*B[k][j]);
				}
			}
			for (int i = 0; i < row; i++)
			{
				for(int j = 0; j < col; j++)
				{
					cout << A[i][j] << "   ";
				}
				for(int j = 0; j < col; j++)
				{
					cout << B[i][j] << "   ";
				}
				for(int j = 0; j < col; j++)
				{
					cout << "\t";
					cout << C[i][j] << "   ";
				}
				cout << endl;
			}
		}
		if(select == 't')
		{
			for (int i = 0; i < row; i++)
			{
				for(int j = 0; j < col; j++)
				{
					cout << A[i][j] << "   ";
					if(j == (col-1))
					{
						cout << endl;
					}
				}
			}
			cout << endl;
			for (int i = 0; i < row; i++)
   			{
      			for (int j = i+1; j < col; j++)
      			{
				  temp=A[i][j];
         		  A[i][j]=A[j][i];
         		  A[j][i]=temp;
      			}
        	}
        	for (int i = 0; i < row; i++)
			{
				for(int j = 0; j < col; j++)
				{
					cout << A[i][j] << "   ";
					if(j == (col-1))
					{
						cout << endl;
					}
				}
			}
        	
		}
	}
	while (select != 'q');
	
	return 0;
}

char menu(char operation)
{
	cout << "Welcome to the Menu!"; 
	cout << "\n? menu "
		 << "\ni input"
		 << "\na addition"
		 << "\ns subtraction"
		 << "\nm multiplication"
		 << "\nt transpose"
		 << "\nq quit program"; 
		 
	cout << "\n\nInput the symbol for your desired operation: ";
	cin >> operation;
	return operation;
}


Welcome!
Enter 2 numbers for the row and column of the matrices:
(Space in between) 2 2
Input Matrix A values: 1 2 3 4
Input Matrix B values: 1 2 3 4
Welcome to the Menu!
? menu
i input
a addition
s subtraction
m multiplication
t transpose
q quit program

Input the symbol for your desired operation: a
1   2   1   2           2       4
3   4   3   4           6       8
Welcome to the Menu!
? menu
i input
a addition
s subtraction
m multiplication
t transpose
q quit program

Input the symbol for your desired operation: s
1   2   1   2           0       0
3   4   3   4           0       0
Welcome to the Menu!
? menu
i input
a addition
s subtraction
m multiplication
t transpose
q quit program

Input the symbol for your desired operation: m
1   2   1   2           7       10
3   4   3   4           15      22
Welcome to the Menu!
? menu
i input
a addition
s subtraction
m multiplication
t transpose
q quit program

Input the symbol for your desired operation: t
1   2
3   4

1   3
2   4
Welcome to the Menu!
? menu
i input
a addition
s subtraction
m multiplication
t transpose
q quit program

Input the symbol for your desired operation: i

Enter 2 numbers for the row and column of the matrices:
(Space in between) 3 3
Input Matrix A values: 1 2 3 4 5 6 7 8 9
Input Matrix B values: 1 2 3 4 5 6 7 8 9
Welcome to the Menu!
? menu
i input
a addition
s subtraction
m multiplication
t transpose
q quit program

Input the symbol for your desired operation: a
1   2   4   1   2   4           2       4       8
4   5   7   4   5   7           8       10      14
7   8   9   7   8   9           14      16      18
Welcome to the Menu!
? menu
i input
a addition
s subtraction
m multiplication
t transpose
q quit program

Input the symbol for your desired operation:


//How come when I enter new values for the 3 x 3 matrix, some of the values are incorrect?
This is your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int row, col;
cin >> row >> col;

float A[row][col];  // Error 1

cin >> row >> col; // Error 2

cout << "Input Matrix A values: ";
for(int b = 0; b < row; b++)
{
	for(int e = 0; e < col; e++)
	{
		cin >> A[b][e];
	}
}

Error 1: the C++ standard requires that the size of statically allocated array is known during compilation. You have to use dynamic allocation, or allocate "big enough" static array and check that user-given row,col do not exceed that.

Error 2: Changing these variables does not resize the array.
What do i do so that when i change the dimensions and enter new values the values are correct?
Arrays other than one-dimensional are a bit tricky. There are third-party math libraries that do provide matrix-types and usual operations for them.

You could use a resizeable 1D array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::size_t row, col;
cin >> row >> col;

std::vector<float> A( row * col );

cin >> row >> col;

A.resize( row * col );

cout << "Input Matrix A values: ";
for ( int b = 0; b < row; b++ )
{
	for ( int e = 0; e < col; e++ )
	{
		cin >> A[ b*col + e ];
	}
}


Btw, can you tell how these differ:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// A
        	for (int i = 0; i < row; i++)
			{
				for(int j = 0; j < col; j++)
				{
					cout << A[i][j] << "   ";
					if(j == (col-1))
					{
						cout << endl;
					}
				}
			}

// B
        	for (int i = 0; i < row; i++)
			{
				for(int j = 0; j < col; j++)
				{
					cout << A[i][j] << "   ";
				}
				cout << endl;
			}
Topic archived. No new replies allowed.