I\'m trying to create a program that will multiply, add, and scale,two 5x5 matri
ID: 3615755 • Letter: I
Question
I'm trying to create a program that will multiply, add, and scale,two 5x5 matrices that are being pulled from a text file. i stillneed to add code for the scaling aspect so I'm not worried aboutthat at the moment. The problem I'm having with my current code isdisplaying the matrix in its proper form. When it displays theresult is displays each point 5 times. Unfortunately it's drawingfrom two text files named "matrix1.txt" and "matrix2.txt" which hasall the numbers for a 5x5 matrix separated by commas so you wont beable to run the code without them. I'll include the values in thetext. Also the results from the product of the matrices are way outthere such as -9.255+063 or something like that. If someone couldplease look over my code and suggest any changes I'd appreciate it.ThanksMatrix1:
5,2,3,6,1,8,7,5,6,4,4,2,6,7,8,9,7,3,4,5,6,7,2,1,7
Matrix2:
1,2,3,4,1,2,3,5,6,4,4,7,7,8,6,9,0,1,2,3,3,3,2,1,0
[code]
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
//class declaration
class matrices
{
public:
double matA[5][5], matB[5][5], matC[5][5],matD[5][5], matE[5][5];
void print_sum();
void print_product();
//void print_scaled();
};
//class implementation for print_sum
void matrices::print_sum()
{
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
for(int x = 0; x < 5;x++)
{
matC[i][j]= matA[i][j] + matB[i][j];
cout<< matC[i][j] << ",";
}
}
}
}
//class implementation for print_product
void matrices::print_product()
{
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
for(int x = 0; x < 5;x++)
{
matD[i][j]+= matA[i][x] * matB[x][j];
cout<< matD[i][j] << ",";
}
}
}
}
//prints the results as needed
int main()
{
matrices m;
ifstream fileA, fileB;
string filenameA, filenameB, item;
cout << "Enter first input file name: ";
cin >> filenameA;
fileA.open(filenameA.c_str());
if(fileA.fail())
{
cout << "Error opening firstinput file. " << endl;
exit(1);
}
else
{
cout << "Enter the second inputfile name: ";
cin >> filenameB;
fileB.open(filenameB.c_str());
if(fileB.fail())
{
cout << "Erroropening second input file. " << endl;
exit(1);
}
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5;j++)
{
getline(fileA,item,',');
m.matA[i][j] = atoi(item.c_str());
getline(fileB,item,',');
m.matB[i][j] = atoi(item.c_str());
}
}
cout << " The sum of the two matrices are: ";
m.print_sum();
cout << " The product of the two matrices are: ";
m.print_product();
}
return 1;
}
[/code]
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
//class declaration
class matrices
{
public:
double matA[5][5], matB[5][5], matC[5][5],matD[5][5], matE[5][5];
void print_sum();
void print_product();
//void print_scaled();
};
//class implementation for print_sum
void matrices::print_sum()
{
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
for(int x = 0; x < 5;x++)
{
matC[i][j]= matA[i][j] + matB[i][j];
cout<< matC[i][j] << ",";
}
}
}
}
//class implementation for print_product
void matrices::print_product()
{
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
for(int x = 0; x < 5;x++)
{
matD[i][j]+= matA[i][x] * matB[x][j];
cout<< matD[i][j] << ",";
}
}
}
}
//prints the results as needed
int main()
{
matrices m;
ifstream fileA, fileB;
string filenameA, filenameB, item;
cout << "Enter first input file name: ";
cin >> filenameA;
fileA.open(filenameA.c_str());
if(fileA.fail())
{
cout << "Error opening firstinput file. " << endl;
exit(1);
}
else
{
cout << "Enter the second inputfile name: ";
cin >> filenameB;
fileB.open(filenameB.c_str());
if(fileB.fail())
{
cout << "Erroropening second input file. " << endl;
exit(1);
}
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5;j++)
{
getline(fileA,item,',');
m.matA[i][j] = atoi(item.c_str());
getline(fileB,item,',');
m.matB[i][j] = atoi(item.c_str());
}
}
cout << " The sum of the two matrices are: ";
m.print_sum();
cout << " The product of the two matrices are: ";
m.print_product();
}
return 1;
}
[/code]