0
#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.
2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Salem
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 ?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.