please anyone c++ helP??

Smokey0066

Senior member
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;
}
}
 

Smokey0066

Senior member
Oct 9, 1999
488
0
0
whoa.. i guess it was just a slight problem on my part.. i realized both inputs were going into matrix A..

well i'll be back if i get stuck again.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: Smokey0066
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 ii = 0; ii < rowsA; ii++)
for(int j = 0; j < colsA; j++)
cin >> A[ii][j];

cout<<"Enter the second matrix by row: " <<endl;
for(int a = 0; a < rowsB; a++)
for(int bb = 0; bb < colsB; bb++)
cin >> A[a][bb];

Note that this is a bit messy. Pick one set of subscript variable & stick with them. Makes it more readable.
Also, I tend to use 'r' & 'c' for subscripts when indexing arrays. Helps keep clear what is what.

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 ii = 0; ii < rowsA; ii++)
{
for(int j = 0; j < colsA; j++)
{
cout<<"A: " <<A[ii][j] <<" " <<"B: " <<B[j][ii];
n = A[ii][j] * B[j][ii];
cout<<"N: " <<n <<endl;
t = t + n;
}
cout<<endl;
cout<<"T: " <<t <<endl;
}
}

You still have problems.
Take a look at the definition for matrix multiplication again.
You're missing a loop, and t never gets set back to 0 for each element.

something like this:
for(r=0; r<rowsA; r++)
for(c=0; c<colsA; c++)
{
c[r][c] = 0;
for(rc=0; rc<colsA; rc++)
c[r][c] += a[r][rc]*b[rc][c]
}

(BTW: I editided some of your subscripts slightly so they don't get interpreted as italic or bold directions for the forum)
 

Smokey0066

Senior member
Oct 9, 1999
488
0
0
i finished the program last night....

for some reason visual wont let me use the same subscripts for the matrices but in unix i can do it fine. thats why i just keep picking new letters. .hehe oh well it works and i'll hand it in tomorrow

thanks.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: Smokey0066
i finished the program last night....

for some reason visual wont let me use the same subscripts for the matrices but in unix i can do it fine. thats why i just keep picking new letters. .hehe oh well it works and i'll hand it in tomorrow

thanks.

Probably VisualC++ isn't enforcing scoping rules properly.
When you declare a variable in a for statement, it should only have scope withing that for statement. Which means you could re-declare it again outside of that statement.