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.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
usingnamespace std;
constint ROW = 10;
constint 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
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.
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;
}
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.