What is wrong with my multiplication?

Can anybody tell me where my mistake is ? The problem is that it doesn't show the multiplicated 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
  #include<iostream>
#include<iomanip>
using namespace std;
void GenMatrix(int matrix [10][10], int rows, int cols)
{
     for(int i=0;i<=rows-1;i++)
     for(int j=0;j<=cols-1;j++)
     matrix[i][j]=rand()%4;
}
void OutputMatrix(int matrix[10][10], int rows, int cols)
{
     for(int i=0;i<=rows-1;i++)
     {
             for(int j=0;j<=cols-1;j++)
             cout<<setw(5)<<matrix[i][j];
             cout<<endl;
     }
}
void MultiplicationMatrixes(int matrix1[10][10], int matrix2[10][10], int matrixResult[10][10], int rows1, int cols2, int cols1rows2)
{
     for(int i=0;i<=rows1-1;i++)
     for(int j=0;j<=cols2-1;j++)
     {
             matrixResult[i][j]=0;
             for(int k=0;k<=cols1rows2-1;k++)
             matrixResult[i][j]=matrixResult[i][j]+(matrix1[i][k]*matrix2[k][j]);
     }
}
int main()
{
    int matrix[10][10];
    int matrix1[10][10];
    int matrix2[10][10];
    int matrixResult[10][10];
    int n,m,p,q,s;
    cout<<"Input the number of rows : ";
    cin>>n;
    cout<<endl<<"Input the number of columns : ";
    cin>>m;
    cout<<endl;
    srand(time(0));
    GenMatrix(matrix,n,m);
    cout<<"The matrix is : "<<endl;
    OutputMatrix(matrix,n,m);
    cout<<endl;
    cout<<"Input the number of rows in the first matrix : ";
    cin>>p;
    cout<<endl;
    cout<<"Input the number of columns in the second matrix : ";
    cin>>q;
    cout<<endl;
    cout<<"Input the number of columns in the first matrix and the number of rows in the   second matrix : ";
    cin>>s;
    cout<<endl;
    cout<<"The result from the multiplication of the two matrixes is : "<<endl;
    MultiplicationMatrixes(matrix1,matrix2,matrixResult,p,q,s);
    cout<<endl;
    system("pause");
    return 0;
}    
Last edited on
closed account (2UD8vCM9)
You are trying to multiple matrix1 and matrix2 before they are ever defined.

Also, you didn't call outputmatrix for the matrixResult matrix.
I still have no idea how to fix it ? Can you write what is missing in the program.I forgot to mention that the only needed thing is to show the result of the multiplied matrix.
Last edited on
closed account (2UD8vCM9)
.... No you aren't even defining matrix1 and matrix2 to multiply...

Did you even read what I wrote?

How can you multiply two things that are never defined?
Ok dude can you add the things that are missing ? I'm not saying right now, on the second, I mean later on when you are free .. just add the missing things, please.Thank you.
just to elaborate on what @pindrought said

1. you have not actually put any values in either of the 2d arrays "matrix1" or "matrix2". As you can see on line 42 you only call the GenMatrix function once for the 2d array "matrix" i'm not sure why you do this because you don't actually use "matrix"anywhere else in your program.

you need to call GenMatrix two more times. Once with "matrix1" as the first parameter and then another time with "matrix2" as your first parameter. this will define the matrix's as @pindrought has previously said.

2. after you call the MultiplicationMatrixes function on line 56, you need to again call the Outputmatrix function with the 2d array matrixResult as your first parameter.

you need to do this so that you can actually see the matrix with the multiplied values.

Ok so this is what I've come to .. can you tell me if there is anything else wrong with the main function now and if you can help me out with the OutputMatrix for the matrixResult .. I can't think of the other 2 parameters..
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
#include<iostream>
#include<iomanip>
using namespace std;
void GenMatrix(int matrix [10][10], int rows, int cols)
{
     for(int i=0;i<=rows-1;i++)
     for(int j=0;j<=cols-1;j++)
     matrix[i][j]=rand()%4;
}
void OutputMatrix(int matrix[10][10], int rows, int cols)
{
     for(int i=0;i<=rows-1;i++)
     {
             for(int j=0;j<=cols-1;j++)
             cout<<setw(5)<<matrix[i][j];
             cout<<endl;
     }
}
void MultiplicationMatrixes(int matrix1[10][10], int matrix2[10][10], int matrixResult[10][10], int rows1, int cols2, int cols1rows2)
{
     for(int i=0;i<=rows1-1;i++)
     for(int j=0;j<=cols2-1;j++)
     {
             matrixResult[i][j]=0;
             for(int k=0;k<=cols1rows2-1;k++)
             matrixResult[i][j]=matrixResult[i][j]+(matrix1[i][k]*matrix2[k][j]);
     }
}
int main()
{
    int a[10][10];
    int b[10][10];
    int matrixResult[10][10];
    int n,m,l;
    cout<<"Input the rows of the first matrix ";
    cin>>n;
    cout<<endl;
    cout<<"Input the cols of the second matrix ";
    cin>>m;
    cout<<endl;
    srand(time(0));
    GenMatrix(a,n,m);
    cout<<"The first matrix is : ";
    OutputMatrix(a,n,m);
    srand(time(0));
    GenMatrix(b,n,m);
    cout<<"The second matrix is : ";
    OutputMatrix(b,n,m);
    cout<<"Input the cols and the rows for the matrix : ";
    cin>>l;
    cout<<endl;
    cout<<"The result from the multiplication of the two matrixes is : ";
    MultiplicationMatrixes(a,b,matrixResult,n,m,l);
    OutputMatrix(matrixResult,n,m); //I tried like that and it seems to be working;
    cout<<endl;
    system("pause");
    return 0;
}
Last edited on
you need to add

#include<cstdlib>
#include <time.h>


to the beginning of your program.

also if you truly understand the function OutputMatrix and understand what each parameter is doing you would know what you need to put for the last two parameters. Nonetheless I will give you a hint.

look at lines 44 and 48 where you first called OutputMatrix and compare them to the function call on line 54.
Welp if you are telling me to put "n,m" as parameters it does no good to the matrixResult output . . So besides the two missing parameters from OutputMatrix for the matrixResult is there anything else wrong with the program ?
Last edited on
Topic archived. No new replies allowed.