int read_file(ifstream& in_file, int &row, int &col,double **matrix)
{
//Reads elements of a matrix from a in_file and stores the elements into one
// dimensional array and two dimensional matrix parameters.
// row and col parameters represents the number of rows and columns of the matrix.
int write_file(ofstream& out_file, int row, int col, double **matrix)
{
// Writes the elements of a matrix to out_file
// row and col parameters represents the number of rows and columns of the matrix.
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
out_file<<matrix[i][j]<<" ";
}
out_file<<endl;
}
}
void print_matrix(double **matrix, int row, int col)
{
//Prints the elements of a matrix to the screen.
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
cout<<matrix[i][j]<<endl;
}
}
}
/*void print_array(double *array, int row, int col)
{
//Prints the elements of a matrix (matrix is represented as an array) to the screen.
for(int i=0; i<row*col; i++){
cout<<array[i]<<endl;
}
}
*/
void multip(double **matrix1, double **matrix2, double **result, int k, int m, int n)
{
// Function that multiplies two dimensional arrays
//matrix1(size: kxm) and matrix2 (size: mxn).
// Results are stored in the result (size: kxn) matrix
// You can use pointer arithmetic or matrix notation for multiplication.
for(int i=0; i<k; i++) {
for(int j=0; j<m; j++) {
for(int z=0; z<n; z++) {
result[i][j]=matrix1[i][j]*matrix2[i][z];
}
}
}
}
/*void multip_array(double *array1, double *array2, double *array_result, int k, int m, int n)
{
//multiplies one dimensional arrays array1(size: 1x(kxm)) and array2 (size: 1x(mxn)) by using
//pointer
//arithmetic and stores the results at one dimensional array array_result (size: 1x(kxn)).
for(int i=0; i<k*n; i++) {
array_result[i]=array1[i]*array2[i];
}
}
*/
int main(int argc, char* argv[])
{
// Input and output files will be obtained from user using argv.
int row1, col1, row2, col2;
ifstream input1;
input1.open(argv[1],ios::in);
ifstream input2;
input2.open(argv[2],ios::in);
ofstream out_file;
out_file.open(argv[3],ios::out|ios::app);
//row1 and col1 will be read from first line of the input file1
input1>>row1>>col1;
//row2 and col2 will be read from first line of the input file2
input2>>row2>>col2;
// Two dimensional arrays can be defined using pointer to pointers:
double **matrix1, **matrix2, **result;
// Dynamic memory allocation for two dimensional arrays:
matrix1 = new double *[row1];
for (int i = 0; i < row1; i++)
matrix1[i] = new double [col1];
matrix2 = new double *[row2];
for (int i = 0; i < row2; i++)
matrix1[i] = new double [col2];
result = new double *[row1];
for (int i = 0; i < row1; i++)
matrix1[i] = new double [col2];
//You can access the elements of the two dimensional arrays using matrix notation: matrix1[i][j] or
// pointer arithmetic: *(*(matrix + i) + j)
/*
//one dimensional arrays
double *array1, *array2, *array_result;
//dynamic memory allocation for one dimensional arrays
array1 = new double [row1*col1];
array2 = new double [row2*col2];
array_result = new double [row1*col2];
*/
read_file(input1, row1,col1,/*array1,*/matrix1); // Read the first file
read_file(input2, row2,col2,/*array2,*/ matrix2); // Read the second file
//Multiply the two matrices using two dimensional matrices
multip(matrix1, matrix2, result, row1, col1, col2);
// Multiply the two matrices using one dimensional arrays
/* multip_array(array1, array2, array_result, row1, col1, col2); */
//print out the result matrix
print_matrix(result, row1, col2);
// print out the result array
/* print_array(array_result, row1, col2);*/
// Write the result of the multiplication (result matrix) to a file
// File name will be obtained from the user
write_file(out_file, row1, col2, result);