954,190 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

I would liike to multiply two matrices but my code is not working..help me out!

#include <iostream.h>
 
#include <fstream.h>
 
main(int argc, char *argv[]) 
{
//definition of the variables.
 
int mtx1[20][20], mtx2[20][20], mtx3[20][20];
 
int n, i, j, k;
 
//exit if the number of arguments is not 2.
 
if(argc != 3)
{
 
 
cerr << "Usage: mm <filename1> <filename2>\n";
 
return 1;
 
}
 
//open the input file 1. exit if an error occurs.
 
ifstream fin1(argv[1]);
 
if(!fin1) {
 
cerr << "Can't open file1!\n";
 
return 1;
 
}
 
 
//read the input file 1.
 
fin1 >> n; //read the number of rows and columns.
 
 
for(j=1; j<=n; j++) {
 
 
for(i=1; i<=n; i++) {
 
 
fin1 >> mtx1[i][j]; //read the elements of the matrix.
 
 
}
 
}
 
fin1.close(); //close the file 1.
 
//open the input file 2. exit if an error occurs.
 
ifstream fin2(argv[2]);
 
if(!fin2) {
 
cerr << "Can't open file2!\n";
 
return 1;
 
}
 
fin2 >> n; //read the number of rows and columns.
 
for(j=1; j<=n; j++) {
 
for(i=1; i<=n; i++) {
 
fin2 >> mtx2[i][j]; //read the elements of the matrix.
 
}
 
}
 
fin2.close(); //close the file 2.
 
//fill the matrix 3 for the result with zero.
 
for(j=1; j<=n; j++) {
 
 
for(i=1; i<=n; i++) {
 
 
mtx3[i][j] = 0;
 
 
}
 
}
 
//multiply the matrices.
 
for(j=1; j<=n; j++) {
 
 
for(i=1; i<=n; i++) {
 
 
for(k=1; k<=n; k++) {
 
 
mtx3[i][j] += mtx2[i][k] * mtx1[k][j];
 
 
}
 
}
 
}
 
//output the result.
 
cout << n << "\n";
 
for(j=1; j<=n; j++) {
 
 
for(i=1; i<=n; i++) {
 
 
cout << mtx3[i][j];
 
 
if(i < n) {
 
 
cout << "\t";
 
 
}
 
}
 
cout << "\n";
 
}
 
return 0;
 
} //end of the program.
hui
Newbie Poster
6 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

A fat lot of good that did!
Added code tags, but it's still unindented crap.

>
I would liike to multiply two matrices but my code is not working..help me out!

Post a proper question.
Don't just dump the code and hope someone will figure it out.

Does it compile?
Does it run?
What symptoms are there (error messages etc)
What input do you give and what answers do you get / expect ?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You