#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 0 Newbie Poster
Salem 5,199 Posting Sage
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.