Matrix multiplication

Now i have a problem to calculate multiplication of matrix that's not the problem i wrote it
but the real problem is that i have to write it in function and i dont know how
could anybody tell me how or give me hint or what should i study to solve it
That's my code

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
  #include<iostream>
using namespace std;
int main()
{
int l,m,z,n;
int matrixA[10][10];
int matrixB[10][10];
int matrixC[10][10];

cout<<"enter the dimension of the first matrix"<<endl;
cin>>l>>m;
cout<<"enter the dimension of the second matrix"<<endl;
cin>>z>>n;
if(m!=z||z!=m){
cout<<"error in the multiblication enter new dimensions"<<endl;
cout<<"enter the dimension of the first matrix"<<endl;
cin>>l>>m;
cout<<"enter the dimension of the second matrix"<<endl;
cin>>z>>n;
}

else{
cout<<"enter the first matrix"<<endl;
for(int i=0;i<l;i++){
for(int j=0;j<m;j++){
     cin>>matrixA[i][j];
     }
     }
cout<<"enter the second matrix"<<endl;
for(int i=0;i<z;i++){
for(int j=0;j<n;j++){
    cin>>matrixB[i][j];
}
}
for(int i=0;i<l;i++){
for(int j=0;j<n;j++){
        matrixC[i][j]=0;
        for(int k=0;k<m;k++){
matrixC[i][j]=matrixC[i][j]+(matrixA[i][k] * matrixB[k][j]);
}
}
}

cout<<"your matrix is"<<endl;
for(int i=0;i<l;i++){
for(int j=0;j<n;j++){
cout<<matrixC[i][j]<<" ";
}
cout<<endl;
}
}
//system("pause");
return 0;
}


http://codepad.org/s36rcxls
I don't understand your question.


Please indent your code.
i need to make this code using functions
any help please ???
Last edited on
closed account (D80DSL3A)
The tutorial at this site on arrays may tell you what you need. Passing multi-dimensional arrays to functions is covered near the end:
http://www.cplusplus.com/doc/tutorial/arrays/
thank u i will read it
This is my new code
the problem is in my matrix multiplication
and is this function as what my teacher want ???

#include <iostream>
using namespace std;
#include <cstdlib>

void fn(int **arr) {

}
void fn2 (int **arr2){

}
void fn3 (int **arr3){

}
int main() {

int r, c ,r2 , c2 , r3 , c3 , m , l;

cout << "Enter size of row 1 : " ;
cin >> r ;
cout << "Enter size of column 1 : " ;
cin >> c ;
cout <<"Enter matrix 1 please " ;

int **arr = new int*[r];
for (int i = 0; i < r; i++)
arr[i] = new int[c];

for (int i=0 ; i<r ; i++){
for (int j=0 ; j<c ; j++)
cin >> arr[i][j] ;
}
for (int i=0 ; i<r ; i++){
for (int j=0 ; j<c ; j++)
cout << arr[i][j]<<" " ;
cout << endl ;
}
cout << "Enter size of row 2 : " ;
cin >> r2 ;
cout << "Enter size of column2 : " ;
cin >> c2 ;
cout << "Enter matrix 2 please " ;
int **arr2 = new int*[r2];
for (int i = 0; i < r2 ; i++)
arr2[i] = new int[c2];


for (int i=0 ; i<r2 ; i++){
for (int j=0 ; j<c2 ; j++)
cin >> arr2[i][j] ;
}
for (int i=0 ; i<r2 ; i++){
for (int j=0 ; j<c2 ; j++)
cout << arr2[i][j] <<" " ;
cout << endl ;
}
int **arr3 = new int*[r3];
for (int i = 0; i < r2 ; i++)
arr3[i] = new int[c];
if (c == r2 ){

for (int i=0 ; i<r2 ; i++){
for (int j=0 ; j<c ; j++){
arr3[i][j]=0;
for(int k=0;k<c;k++)
arr3[i][j]=arr3[i][j]+(arr[i][k] * arr2[k][j]);
}
}
cout<<"your matrix is"<<endl;
for(int i=0;i<r3;i++){
for(int j=0;j<r3;j++){
cout<<arr3[i][j]<<" ";
}
cout<<endl;
}
}
else
cout << "Error"<< endl ;

fn(arr);
fn2(arr2);
fn3(arr3);
return 0;
}



code in codepad

http://codepad.org/8i7MlSP4
> the problem is in my matrix multiplication
¿what is the problem?
it doesnt give me the right answer !!!
closed account (D80DSL3A)
You've made some errors in the number of rows and columns of the matrices.
You can't independently specify the rows and columns for all 3 matrices.

Assuming: arr3 = arr*arr2

You can specify r,c and have arr[r][c]. You can specify the number of columns for arr2 (say c2), but not the number of rows - they must equal the number of columns of arr. ie arr2[c][c2]. arr3 will then have dimensions arr3[r][c2].

If you make a drawing of multiplying the matrices you should be able to see it and to straighten out the row and column values throughout your code.
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

#include <iostream>
using namespace std;
#include <cstdlib>

void fn(int **arr) {

}
void fn2 (int **arr2){

}
void fn3 (int **arr3){

}
int main() {

	int r, c ,r2 , c2 , r3 , c3 , m , l;

	cout << "Enter size of row 1 : " ;
	cin >> r ;
	cout << "Enter size of column 1 : " ;
	cin >> c ;
	cout <<"Enter matrix 1 please " ;

	int **arr = new int*[r];
	for (int i = 0; i < r; i++)
		arr[i] = new int[c];

	for (int i=0 ; i<r ; i++){
		for (int j=0 ; j<c ; j++)
                    cin >> arr[i][j] ;
	}
	for (int i=0 ; i<r ; i++){
		for (int j=0 ; j<c ; j++)
			 cout << arr[i][j]<<" " ;
		cout << endl ;
	}
	cout << "Enter size of row 2 : " ;
	cin >> r2 ;
	cout << "Enter size of column2 : " ;
	cin >> c2 ;
	cout << "Enter matrix 2 please " ;
	int **arr2 = new int*[r2];
	for (int i = 0; i < r2 ; i++)
		arr2[i] = new int[c2];


	for (int i=0 ; i<r2 ; i++){
		for (int j=0 ; j<c2 ; j++)
			 cin >> arr2[i][j] ;
	}
	for (int i=0 ; i<r2 ; i++){
		for (int j=0 ; j<c2 ; j++)
			 cout << arr2[i][j] <<" " ;
		cout << endl ;
	}
	int **arr3 ;
	arr3 = new int * [r2];
	for(int i=0;i<c;i++)
    {
        arr2[i] = new int [c2];
        arr3[i] = new int [c2];
    }
	if (c != r2)
        cout << "We cannot solve multiplication " ;

    else {


	for (int i = 0; i < r ; i++){
			for (int j=0 ; j<c ; j++){
                    arr3[i][j]=0;
			    for (int k=0 ; k<2 ; k++){
			        arr3[i][j]=0;
                    arr3[i][j]=arr3[i][j]+(arr[i][k] * arr2[k][j]);
                                    }
                                }
            }
	cout<<"your matrix is"<<endl;
	for(int i=0;i<c;i++){
            for(int j=0;j<r2;j++)
                cout<<arr3[i][j]<<" ";
            cout<<endl;
                        }
	   }

    fn(arr);
	fn2(arr2);
	fn3(arr3);
	return 0;
}







That is my final code is it right ???
please i need one tell me what i need to do as my teacher asks me to
Students MUST use functions

This is the whole question

Write a program that asks the user for two matrices: matrix A with dimensions LxM, and matrix B with dimensions MxN. The program then prints the matrix C of their multiplication with dimensions LxN. You MUST represent every matrix with a two-dimensional array. The program should ask the user to:
a) Enter the dimensions of the first matrix
b) Enter the dimensions of the second matrix
c) Enter the first matrix.
d) Enter the second matrix
After step b), the program should validate if the entered dimensions are valid for matrix multiplication. If invalid dimensions are entered, the program should print an error message, and ask the user again for new valid dimensions. After step d), the program prints the result matrix

sorry for being boring for somebody here !!!
any help ???
Topic archived. No new replies allowed.