2D array multiplication

Hello everyone. I have been trying to figure out how to store and multiply two array from a two files that my program will read. I need to figure out how to read and store the arrays that are in data file to be able to multiply them.

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
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>

const int ROW = 10;
const int COL = 10;

using namespace std;

int process(ifstream &infile, int [ROW][COL]);
void calculate_product(int a[ROW][COL],int c[ROW][COL]);

int main(int argc, char **argv)
{

	if (argc != 3)
	{
		cout << "Error, must provide exactly two arguments" << endl;
		return 0;
	}

	ifstream infile(argv[2]);

	if (!infile)
	{
		cout << "Error, file could not be opened!" << endl;
		return 0;
	}
	infile.close();



	return 0;

}
int process(ifstream &infile, int a[ROW][COL])
{
	string line;
	int rowc = 0, colc = 0;

	while (getline(infile, line))
	{
		cout << " " << line << endl;

		istringstream iss;

		while (iss >> a[rowc][colc++])
		{
			rowc++;
			colc--;
		}

		return 0;
	}
	infile.close();

}
void calculate_product(int c[rowc][colc], int a[rowc][colc])
{


	if (rowc != colc)
		cout << "Error: Matrices cannot be multiplied!" << endl;

	for(int i = 0; i < rowc; ++i){
        	for(int j = 0; j < colc; ++j){
				c[i][j] = 0;
            		for(int k = 0; k < a[i][j]; ++k){
                		c[i][j] += a[i][k] * a[k][j];
			}
		}
	}

	for(int i = 0; i < rowc; i++)
		for(int j = 0; j < colc; j++)
			cout << c[i][j] << endl;


}

  Put the code you need help with here.

nt process(ifstream &infile, int a[ROW][COL])
{
string line;
int rowc = 0, colc = 0;

while (getline(infile, line))
{
cout << " " << line << endl;

istringstream iss;

while (iss >> a[rowc][colc++])
{
rowc++;
colc--;
}

return 0;
}
infile.close();

}

This are the data files i have:
data1:
1 2 3
4 5 6
data2:
7 8
9 10
11 12
data3:
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
Do first things first.
You will have many problems with your matrix multiplying later.

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

const int ROW = 10;
const int COL = 10;

void process( ifstream &infile, int a[ROW][COL], int &rows, int &cols );
void print( int a[ROW][COL], int rows, int cols );


int main(int argc, char *argv[] )
{
   if ( argc != 3 ) { cout << "Error, must provide exactly two arguments\n";   return 0; }

   int A[ROW][COL], B[ROW][COL];
   int rowA, colA, rowB, colB;
   ifstream infile;

   infile.open( argv[1] );   if ( !infile ) { cout << "Error, file could not be opened\n"; return 0; }
   process( infile, A, rowA, colA );
   infile.close();

   infile.open( argv[2] );   if ( !infile ) { cout << "Error, file could not be opened\n"; return 0; }
   process( infile, B, rowB, colB );
   infile.close();

   cout << "First matrix: \n";   print( A, rowA, colA );
   cout << "Second matrix:\n";   print( B, rowB, colB );
}


void process( ifstream &infile, int a[ROW][COL], int &rows, int &cols )
{
   string line;
   rows = 0;
   while ( getline( infile, line ) )
   {
      istringstream iss( line );
      cols = 0;
      while ( iss >> a[rows][cols] ) cols++;
      rows++;
   }
}                                                // By now, both rowc and colc are one past the last index
                                                 // ... which is what is required

void print( int a[ROW][COL], int rows, int cols )
{
   for ( int i = 0; i < rows; i++ )
   {
      for ( int j = 0; j < cols; j++ ) cout << setw( 3 ) << a[i][j] << ' ';
      cout << '\n';
   }
}


First matrix:
  1   2   3 
  4   5   6 
Second matrix:
  7   8 
  9  10 
 11  12 
Last edited on
Thank you very much for the help. I implemented the code but the multiplication for some reason is giving me a segmentation fault. For some reason I cant see whats happening.


#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

const int ROW = 10;
const int COL = 10;

void process( ifstream &infile, int a[ROW][COL], int &rows, int &cols );
void print( int a[ROW][COL], int rows, int cols );
void calculate_product(int [ROW][COL], int[ROW][COL], int [ROW][COL], int rowA, int rowB, int colA, int colB, int rowC, int colC);

int main(int argc, char *argv[] )
{
if ( argc != 3 ) { cout << "Error, must provide exactly two arguments\n"; return 0; }

int A[ROW][COL], B[ROW][COL], C[ROW][COL];
int rowA, colA, rowB, colB, rowC, colC;
ifstream infileA;
ifstream infileB;

infileA.open( argv[1] ); if ( !infileA ) { cout << "Error, file could not be opened\n"; return 0; }
process( infileA, A, rowA, colA );
infileA.close();

infileB.open( argv[2] ); if ( !infileB ) { cout << "Error, file could not be opened\n"; return 0; }
process( infileB, B, rowB, colB );
infileB.close();

cout << "First matrix: \n"; print( A, rowA, colA );
cout << "Second matrix:\n"; print( B, rowB, colB );
cout << "New matrix:\n"; print( C, rowC, colC );

}


void process( ifstream &infile, int a[ROW][COL], int &rows, int &cols )
{
string line;
rows = 0;
while ( getline( infile, line ) )
{
istringstream iss( line );
cols = 0;
while ( iss >> a[rows][cols] )
cols++;
rows++;
}
}

void print( int a[ROW][COL], int rows, int cols )
{
for ( int i = 0; i < rows; i++ )
{
for ( int j = 0; j < cols; j++ ) cout << setw( 8 ) << a[i][j] << ' ';
cout << '\n';
}
}

void calculate_product(int A[ROW][COL], int B[ROW][COL], int C[ROW][COL], int rowA, int rowB, int colA, int colB, int rowC, int colC)
{
if (rowA != colB)
cout << "Error: Matrices cannot be multiplied!" << endl;

int a = 0;

for(int i = 0; i < rowA; ++i){
for(int j = 0; j < colB; ++j){
for(int k = 0; k < colA; ++k){
a += A[rowA][k] * B[k][colB];
C[i][j] = a;
}

cout << right << setw(8) << C[i][j] << " ";
}

cout << endl;

}

}
You aren't multiplying matrices correctly. There are numerous errors, but I can't refer to line numbers because you haven't used code tags.

- You have a segmentation fault because you go out of array bounds, which is because you are using the wrong formula to multiply matrices. In the line where you add to the new matrix element it should be i not rowA and j not colB.

- you also need to initialise a as 0 for EACH i, j element. Actually, you don't need a at all; you could just use C[i][j].

- Your row/column condition for compatability is wrong: you just need colA = rowB.

- You don't actually appear to call calculate_product, and you don't need all of its arguments: the number of rows and columns in C is defined by those in A and B. If you wanted to work them out here and send them back through the argument list then they would have to be passed by reference, not value.
Last edited on
Topic archived. No new replies allowed.