Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi I need help debugging this code. Im supposed to print out the multiplication

ID: 3721790 • Letter: H

Question

Hi I need help debugging this code. Im supposed to print out the multiplication of 2 multidimensional arrays but im getting a segmentation fault. My program can read and store the two data files from where im supposed to get the arrays from but the multiplication part which is the last function in the program is not working properly. Please help.

#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 ";   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 "; return 0; }
   process( infileA, A, rowA, colA );
   infileA.close();

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

   cout << "First matrix: ";   print( A, rowA, colA );
   cout << "Second matrix: ";   print( B, rowB, colB );
//   cout << "New matrix: ";   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 << ' ';
   }
}

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;

        }

}

Explanation / Answer

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){

a=0;//have to reset this here

for(int k = 0; k < colA; ++k){

a += A[i][k] * B[k][j];// probelm was here in your code

C[i][j] = a;

}

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

}

cout << endl;

}

}