- Oct 9, 1999
- 488
- 0
- 0
I'm trying to write a program to multiply 2 matrices together. Well my problem is when i print out the matrix in the function for a check its calling up the wrong values. can someone look over this and tell me how to do the multiplication part? am i doing it right? if not can you show me how?
its getting really hard too look at this after a couple hrs.. its just confusing.. so if anyone could help a newb out.. THANKS
BTW the prototype/call was given to us; i guess we are supposed to have all those parameters..
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const int MAX=20;
void matMult(double A[MAX][MAX], double B[MAX][MAX],
double C[MAX][MAX], int rowsA, int colsA,
int rowsB, int colsB, int &rowsC, int &colsC);
int main()
{
int rowsA = 2, colsA = 3, rowsB = 3, colsB = 2, rowsC = 2, colsC = 2;
double A[MAX][MAX];
double B[MAX][MAX];
double C[MAX][MAX];
//matrix A and B
cout << "Enter the number of rows in matrix A: ";
cin >>rowsA;
cout << "Enter the number of columns in matrix A: ";
cin >>colsA;
cout << "Enter the number of rows in matrix B: ";
cin >>rowsB;
cout << "Enter the number of columns in matrix B: ";
cin >>colsB;
//check
if(colsA != rowsB)
{
cout<<"ERROR. The two matrices do not match; can not continue. " <<endl;
exit(1);
}
// input component data
cout<<"Enter the first matrix by row: " <<endl;
for(int i = 0; i < rowsA; i++)
for(int j = 0; j < colsA; j++)
cin >> A[j];
cout<<"Enter the second matrix by row: " <<endl;
for(int a = 0; a < rowsB; a++)
for(int b = 0; b < colsB; b++)
cin >> A[a];
matMult(A, B, C, rowsA, colsA, rowsB, colsB, rowsC, colsC);
}
// matrix multiply function
void matMult(double A[MAX][MAX], double B[MAX][MAX],
double C[MAX][MAX], int rowsA, int colsA,
int rowsB, int colsB, int &rowsC, int &colsC)
{
int t = 0, n = 0;
for(int i = 0; i < rowsA; i++)
{
for(int j = 0; j < colsA; j++)
{
cout<<"A: " <<A[j] <<" " <<"B: " <<B[j];
n = A[j] * B[j];
cout<<"N: " <<n <<endl;
t = t + n;
}
cout<<endl;
cout<<"T: " <<t <<endl;
}
}
its getting really hard too look at this after a couple hrs.. its just confusing.. so if anyone could help a newb out.. THANKS
BTW the prototype/call was given to us; i guess we are supposed to have all those parameters..
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const int MAX=20;
void matMult(double A[MAX][MAX], double B[MAX][MAX],
double C[MAX][MAX], int rowsA, int colsA,
int rowsB, int colsB, int &rowsC, int &colsC);
int main()
{
int rowsA = 2, colsA = 3, rowsB = 3, colsB = 2, rowsC = 2, colsC = 2;
double A[MAX][MAX];
double B[MAX][MAX];
double C[MAX][MAX];
//matrix A and B
cout << "Enter the number of rows in matrix A: ";
cin >>rowsA;
cout << "Enter the number of columns in matrix A: ";
cin >>colsA;
cout << "Enter the number of rows in matrix B: ";
cin >>rowsB;
cout << "Enter the number of columns in matrix B: ";
cin >>colsB;
//check
if(colsA != rowsB)
{
cout<<"ERROR. The two matrices do not match; can not continue. " <<endl;
exit(1);
}
// input component data
cout<<"Enter the first matrix by row: " <<endl;
for(int i = 0; i < rowsA; i++)
for(int j = 0; j < colsA; j++)
cin >> A[j];
cout<<"Enter the second matrix by row: " <<endl;
for(int a = 0; a < rowsB; a++)
for(int b = 0; b < colsB; b++)
cin >> A[a];
matMult(A, B, C, rowsA, colsA, rowsB, colsB, rowsC, colsC);
}
// matrix multiply function
void matMult(double A[MAX][MAX], double B[MAX][MAX],
double C[MAX][MAX], int rowsA, int colsA,
int rowsB, int colsB, int &rowsC, int &colsC)
{
int t = 0, n = 0;
for(int i = 0; i < rowsA; i++)
{
for(int j = 0; j < colsA; j++)
{
cout<<"A: " <<A[j] <<" " <<"B: " <<B[j];
n = A[j] * B[j];
cout<<"N: " <<n <<endl;
t = t + n;
}
cout<<endl;
cout<<"T: " <<t <<endl;
}
}