Reading data from file and performing matrix multiplication

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
You copied the matrix multiplication from elsewhere, so that part of the code is fine. The rest of it is bizarre.

Why are you reading your two-dimensional arrays of numbers into one-dimensional arrays of arrays of strings (lines 12-14)?

For file input (and output) see http://www.cplusplus.com/doc/tutorial/files/
For use of arrays see http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
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;
}
Topic archived. No new replies allowed.