-3

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
Jimmyh
  • 3
  • 2
  • Some issues: `for (count1=0;count1<26;++count1)` shouldn't this be `for (count1=0;count1<25;++count1)`? `while (!inFile.eof())` Note this Q&A please: [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – πάντα ῥεῖ Dec 14 '14 at 19:50
  • Can you give us a data example? (How is the output "strange"? Is it strange when you print myarray3 where we see it here, i.e. after computation, or do you try to print it later again?) – Peter - Reinstate Monica Dec 14 '14 at 20:01

2 Answers2

0

Take input from file using this. Hope this till work

       ifstream inFile;
        inFile.open(file1.c_str());

        i=0; j=0;
        while (getline(inFile,mystring1))
        {

                int val=0;

                 for(int k=0; k<mystring1.size(); k++)
                 {

                     if(mystring1[k]==',')
                     {
                         myarray1[i][j++]=val;
                         val=0;
                         continue;
                     }
                     val =val*10+mystring1[k]-48;
                 }
                 myarray1[i][j++]=val;
                 i++;
                 j=0;
          }

      inFile.close();
Shahriar
  • 13,460
  • 8
  • 78
  • 95
0

The other answer already addresses the logic error of multiplying the matrices. I want to point out a problem I am seeing with the code for reading the matrices.

You have:

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];
         }
      }
   }
}

I don't know what where trying to do with the outer loop:

for (count1=0;count1<26;++count1)

If there were enough data in your file, you would end up reading 26 x 5 x 5 numbers from the file. Since you only have data in your file for a 5 x 5 matrix, you can remove the outer loop completely.

R Sahu
  • 204,454
  • 14
  • 159
  • 270