I have been given the task of creating code to read from 2 .txt files into 2 5*5 arrays. I have been checking the code at various sections to see if it works and I have no problem storing the data form the .txt files into my 2 arrays, the problem is I have now to use the 2 arrays as matrices and multiply them together to create a 3 array/matrix. I have been searching the net and forums for hours on how to do this and I created the code below based on what I found. The code to me seems sound (although I am a novice when I comes to c++) but I am getting strange outputs when I try to display my array 3.
using namespace std;
#include<iostream>
#include<fstream>
#include<string>
int main ()
{ int count1,count2;
int i,j,m,n;
int myarray1[5][5];
int myarray2[5][5];
int myarray3[5][5];
string file1,file2,mystring1,mystring2;
cout<<"please enter the name of the first file"<<endl;
cin>>file1;
cout<<"please enter the name of the second file"<<endl;
cin>>file2;
ifstream inFile;
inFile.open(file1.c_str());
for (count1=0;count1<26;++count1)
{
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
while (!inFile.eof())
{
getline(inFile,mystring1,',');
int value1 = atoi(mystring1.c_str());
myarray1[i][j]=value1;
cout<<myarray1[i][j];
}
}
}
}
system("pause");
inFile.close();
system("pause");
inFile.open(file2.c_str());
for (count2=0;count2<26;++count2)
{
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
while (!inFile.eof())
{
getline(inFile,mystring2,',');
int value2 = atoi(mystring2.c_str());
myarray2[i][j]=value2;
cout<<myarray2[i][j];
}
}
}
}
inFile.close();
system("pause");
for(m=0;m<5;m++)
{
for(n=0;n<5;n++)
{
myarray3[m][n]=(myarray1[0][m]*myarray2[n][0])
+(myarray1[1][m]*myarray2[n][1])
+(myarray1[2][m]*myarray2[n][2])
+(myarray1[3][m]*myarray2[n][3])
+(myarray1[4][m]*myarray2[n][4]);
cout<<myarray3[m][n]<<endl;
}
system("pause");
}
this the output I get:
please enter the name of the first file
C:\matrix1.txt
please enter the name of the second file
C:\matrix2.txt
1234554321234543212345432Press any key to continue . . .
Press any key to continue . . .
5678923412457892562112345Press any key to continue . . .
-1546188214
1030792152
1030792152
1030792152
1030792152
-1546188228
-858993456
-858993456
-858993456
-858993456
-1546188228
-858993456
-858993456
-858993456
-858993456
-1546188228
-858993456
-858993456