Matrix multiplication

Hi everyone!
I am student and I have started c++ program. I need some help in my assignment( Matrix multiplication ) This is an assignment:
Multiplication of matrices is often used for solving system of equations and for rotation of graphic objects in computer graphics. In this task you will write a program that takes a pair of matrices, multiplies and produces another matrix.
Theory: In the multiplication C = A*B, where A is a m•p-matrix and B is a p•n-matrix, C will then be a m•n-matrix. A condition must be that the number of columns in A = the number of rows in B.
Write a program that multiplies two loaded matrices. The program should at least use the following declarations:
const int MAXDIM = 2;
typedef double Matrix[MAXDIM][MAXDIM];
void readMatrix(Matrix M, int row, int col);
// Reads one matrix with row rows and col columns
void multMatrix(const Matrix A, const Matrix B, Matrix C, int m, int n, int p);
// Multiplies m•p-matrix A with p•n matrix B and stores the result in the m•n-matrix C
void printMatrix(const Matrix M, int row, int col);
// Prints the matrix M
Global variables may not be used!


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
This is my code does not work very well. I have to follow the required rules above in assignment. Please Help!
#include<iostream>
#include<iomanip>
using namespace std;


 const int MAXDIM = 2;
typedef double Matrix[MAXDIM][MAXDIM];

void readMatrix(Matrix M, int row, int col);
void multMatrix( const Matrix A, const Matrix B, Matrix C, int m, int n, int p);
void printMatrix(const Matrix M, int row, int col);

int main()
{
	//double Matrix[MAXDIM][MAXDIM];
	Matrix mat;
	    int rows = 0;
		int cols = 0;
		int i = 0;
		int j = 0;
		int k = 0;
		Matrix a;
		Matrix b;
		Matrix c;
		do
		{
			cout << "Matrix multiplication " << endl
				<< "======================" << endl;
			cout << "Enter dimension of matrix A (row x col) with space between: ";
			cin >> rows >> cols;
		
		} while (rows < 0 || cols < 0 || rows > MAXDIM || cols > MAXDIM);
		
		//cout << "Enter the matrix values A in free format\n";
		cout << "Enter matrix A in free format: \n";
		readMatrix(mat, rows, cols);
	    
		
		  cout << "Enter matrix A in free format : \n";
			//cin >> i >> k;
			for (i = 0; i <MAXDIM; i++)
			{
				for (k = 0; k < MAXDIM; k++)

					cin >> a[i][k];
				cin.ignore(80, '\n');


			}
			cout << "Enter matrix B in free format : \n";
			//cin >> k >> j;
			for (k = 0; k <MAXDIM; k++)
			{
				for (j = 0; j < MAXDIM; j++)

					cin >> b[k][j];
				cin.ignore(80, '\n');
			}

			c[i][j] = (a[i][k] * b[k][j]) + c[i][j];
			multMatrix(a, b, c, i, j, k);
	
	
	
	 cout << "--------------------------------------------------" << endl;
	 cout << "ANSWER IS :" << endl;
		printMatrix(mat, rows, cols);
	return 0;

}
void readMatrix(Matrix M, int row, int col)
{
	for (int r = 0; r < row; ++r)
	{
		for (int c = 0; c < col; ++c)
		
			cin >> M[r][c];
			cin.ignore(80, '\n');
		   
	}
	/*cout << "\n The value of the first matrix A: ";
	for (int r = 0; r < row; ++r)
	{
		cout << "\n";
		for (int c = 0; c < col; ++c)
		{
			cout << M[r][c] << "\t";
		}
	}*/
	cout << endl;
	cout << "Enter dimension of matrix B (row x col) with space between: ";
	cin >> row >> col;
	cout << "Enter matrix B in free format: \n";
	for (int r = 0; r < row; ++r)
	{
		for (int c = 0; c < col; ++c)
			cin >> M[r][c];
		cin.ignore(80, '\n');

	}

	/*cout << "\n The value of the first matrix B: ";
	for (int r = 0; r < row; ++r)
	{
		cout << "\n";
		for (int c = 0; c < col; ++c)
		{
			cout << M[r][c] << "\t";
		}
	}
	cout << endl;*/

}

void multMatrix( const Matrix A,  const Matrix B, Matrix C, int m, int n, int p)
{
	for (m = 0; m < 2; m++)
	{
		for (p = 0; p < 2; p++)
		
			cout << A[m][p] << "\t";
		cin.ignore(80, '\n');
	}
	cout << endl;
	for (p = 0; p < 2; p++)
	{
		for (n = 0; n < 2; n++)
		
			cout << B[p][n] << " \t";
		
		cin.ignore(80, '\n');
	}
	cout << endl;
	if (m==n)
	{
		for ( m = 0; m<2; m++){
			for ( n = 0; n < 2; n++){
				C[m][n] = 0;
				for (p = 0; p < 2; p++)
					C[m][n] = (A[m][p] * B[p][n]) + C[m][n];

			}
		}
	}
	else
	{
		cout << "WRONG!" << endl;
	}
		

	
}

	
void printMatrix(const Matrix M, int row, int col)
{
	for (int r = 0; r < row; ++r) { 
		for (int c = 0; c < col; ++c) 
			cout << setw(16) << M[r][c]; 
		cout << endl; 
	}
	cout << endl;
}

  Put the code you need help with here.
Topic archived. No new replies allowed.