Nov 29, 2017 at 6:16am UTC
hii.. i am new to c++ am trying to perform a matrix multiplication taking data from a file.But am unable to get the multiplication part.Someone plz help me to fix this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#include <iostream>
#include <fstream>
#include <array>
#include <sstream>
#include <string>
#include <stdio.h>
int rowA=0;
int colA=0;
int i=0,j=0,k=0;
using namespace std;
int main()
{
string line;
ifstream myfile;
int x;
int y;
int arrayA[100][100] = {{0}};
int arrayB[100][100] = {{0}};
int arrayC[100][100] = {{0}};
myfile.open("numeric.txt" );
if (myfile.fail())
{
cerr<<"file does not exist" ;
}
cout<<"\n" <<endl;
while (myfile.good())
{
while (getline(myfile,line))
{
istringstream stream(line);
colA=0;
while (stream >> x)
{
arrayA[rowA][colA]=x;
colA ++;
}
rowA ++;
}
for (int i=0;i<rowA;i++)
{
for (int j=0;j<colA;j++)
{
cout<<arrayA[i][j]<< " " ;
}
cout<<endl;
}
cout<<"\n" <<endl;
}
cout<<"first Matrix" <<endl;
for (i=1;i<=3;i++)
{
for (j=0;j<3;j++)
{
cout<<arrayA[i][j]<<" " ;
}
cout<<endl;
}
cout<<"Second Matrix" <<endl;
for (i=1;i<=3;i++)
{
for (j=1;j<=3;j++)
{
arrayB[i][j] = arrayA[i][j];
cout<<arrayB[i][j]<<" " ;
}
cout<<endl;
}
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
for (k=0;k<3;k++)
{
arrayC[i][j]+=arrayA[i][k]*arrayB[k][j];
}
}
}
cout<<"Multiplication Result" <<endl;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cout<<arrayC[i][j]<<" " ;
}
cout<<endl;
}
return 0;
}
Last edited on Nov 30, 2017 at 9:36am UTC
Nov 29, 2017 at 7:21am UTC
Last edited on Nov 29, 2017 at 8:31am UTC
Nov 30, 2017 at 12:14pm UTC
Thanku for your reply..I have made changes to my code...Can you plz tell me what is the issue with the code...
#include <iostream>
#include <fstream>
#include <array>
#include <sstream>
#include <string>
#include <stdio.h>
int rowA=0;
int colA=0;
int i=0,j=0,k=0;
using namespace std;
int main()
{
string line;
ifstream myfile;
int x;
int y;
int arrayA[100][100] = {{0}};
int arrayB[100][100] = {{0}};
int arrayC[100][100] = {{0}};
int arrayD[100][100] = {{0}};
myfile.open("numeric.txt");
if(myfile.fail())
{
cerr<<"file does not exist";
}
cout<<"\n"<<endl;
while(myfile.good())
{
while(getline(myfile,line))
{
istringstream stream(line);
colA=0;
while(stream >> x)
{
arrayA[rowA][colA]=x;
colA ++;
}
rowA ++;
}
for(int i=0;i<rowA;i++)
{
for(int j=0;j<colA;j++)
{
cout<<arrayA[i][j]<< " " ;
// arrayB[i][j] = arrayA[i][j];
// cout<<endl;
// cout<<arrayB[i][j]<< " " ;
}
cout<<endl;
}
cout<<"\n"<<endl;
}
cout<<"first Matrix"<<endl;
for(i=1;i<=3;i++)
{
for(j=0;j<3;j++)
{
// arrayB[i][j] = arrayA[i][j];
cout<<arrayA[i][j]<<" ";
}
cout<<endl;
}
cout<<"Second Matrix"<<endl;
for(i=3;i<6;i++)
{
for(j=3;j<6;j++)
{
// arrayC[i][j] = arrayA[i][j];
cout<<arrayA[i][j]<<" ";
}
cout<<endl;
}
cout<<"Multiplication Result"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
int sum = 0;
for(k=0;k<3;k++)
{
sum=sum+arrayA[i][k]*arrayA[k][j];
}
arrayD[i][j]=sum;
}
}
// cout<<"Multiplication Result"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<arrayD[i][j]<<" ";
}
cout<<endl;
}
return 0;
}