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

I am having alot of trouble figuring out how to do the assignment, in C++. I am

ID: 3690635 • Letter: I

Question

I am having alot of trouble figuring out how to do the assignment, in C++. I am new to programming, I am wondering if anyone can help me through it please. I have been trying to figure it out for hours and this is all I have:

#include<iostream>
using namespace std;

const int col=10;
const int row=10;

void printarray (int moo[row][col])
{
   for(int n=0; n    {
       for (int p=0; p        {  
       cout << moo[n][p] << " ";
       }
       cout << ' ';
   }
   return;
}

int main()
{
   int firstarray[row][col]={{5, 10, 15},{2, 4, 6, 8, 10}};
   int secondarray[row][col]= {{2, 4, 6, 8, 10},{5, 10, 15}};

   printarray (firstarray);
   cout << endl;
   printarray (secondarray);

   system("pause");
   return 0;
}

Project Description:

Given two data files containing different matrices, write a program to read the matrix contents from the data files into arrays A and B in the program and multiply them together. One input file will go into array A, and the other input file will go into array B. All program working arrays, including arrays A and B, will be declared in main as size 10x10. Matrices in the input data files can be variable size up to the 10x10 maximum, and have different names.

This program must not limit the number or names of files available to be multiplied. It must work with any data files given by the user, and be capable of multiplying arrays having varying numbers of rows and columns up to the maximum size of 10x10.

Arrays in data files will be read into program arrays A and B in main, then the input data files will be closed. Multiplication will be carried out based on array data in the program arrays. If the input file dimensions are less than 10x10, the input array will occupy the left and upper most corner of the program array, i.e. the first element in the input array will occupy the first element in the program array. The number of rows and columns of the resulting sub array must follow the rules of matrix multiplication.

Arrays given in the data files to be multiplied are not extracted from other data arrays, but are intended to be used directly. These will be read into the program arrays in main as sub arrays. Sample arrays are provided in the topic module.

Assignment Procedure:

Find the product AB

Find the product BA

If the product is incompatible, provide an output statement stating as such.

Write the product array to a data file.

Report your findings. For compatible arrays, is AB=BA? Give the reasons for your conclusion. Include the conclusion in the file as a multiline comment.

Program structural requirements are as follows:

Your program must check to see if the multiplication is possible before proceeding with the operation. That is, see if the numbers rows and columns are compatible for the operation.

There must be one function to perform the actual array multiplication, but do nothing more. Necessary information will be passed to the function as arguments.

Each array multiplication operation must call the function. No multiplication will be done in main.

Input matrix data files may be variable from 1 to 10 rows and columns. As an example, the data file may hold a 5x5 matrix, but it will be read into a 10x10 matrix in main.

Matrices A and B can hold data files to be multiplied with different dimensions, e.g. different number of rows and columns.

The names of the data files to be read into program arrays will be entered from the keyboard, e.g. dat, matrix1.dat, etc.

The number of rows and columns of the array in the input data file will be input from the keyboard.

Capability of being able to multiply different sizes of matrices will be checked when graded. Use the sample data files to verify this capability before submitting the program.

All input and output data files, must be attached to the assignment submission.

All matrices must be written to the screen.

The final program must be checked for compliance with these instructions before submittal.

Explanation / Answer

Comments added ...

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
const int col=10;
const int row=10;
void printarray (int moo[row][col])
{
for(int n=0; n<10;n++){
for (int p=0; p<10;p++){
cout << moo[n][p] << " ";
}
cout << ' ';
}
return;
}

void multiplyMatrix(int first[row][col] , int second[row][col]){
//declating temp matrix
int temp[row][col];

//initializing array with zero
for(int i=0; i<row; ++i){
for(int j=0; j<col; ++j)
{
temp[i][j]=0;
}
}
  
//multiplying matrices
for(int i=0; i<row; ++i)
for(int j=0; j<col; ++j)
for(int k=0; k<col; ++k)
{
temp[i][j]+=first[i][k]*second[k][j];
}

//printing matrix
for(int i=0; i<row; ++i){
cout<<" ";
for(int j=0; j<col; ++j)
{
cout<<" "temp[i][j];
}
}

}
int main()
{

int firstarray[row][col];
int secondarray[row][col];

std::ifstream readfile("matrix1.txt");//reding numbers from text file
std::string eachline;
int arrRow = 0,arrCol=0;
while (std::getline(readfile, eachline))
{
std::istringstream iss(eachline);
int n;
//filling first matrix
while (iss >> n)//reading each value from file
{
firstarray[arrRow][arrCol] = n; //storing into array
arrCol++;
if(arrCol == 10){
arrCol = 0;
arrRow++;
}
}
}

//filling second matrix
std::ifstream readfile2("matrix2.txt");//reding numbers from text file
std::string eachline2;
arrRow = 0,arrCol=0;
while (std::getline(readfile2, eachline2))
{
std::istringstream iss(eachline2);
int n;
//filling second matrix
while (iss >> n)//reading each value from file
{
secondarray[arrRow][arrCol] = n; //storing into array
arrCol++;
if(arrCol == 10){
arrCol = 0;
arrRow++;
}
}
}


//printing arrays
printarray (firstarray);
cout << endl;
printarray (secondarray);
cout<<" ";
//matrix multiplication AB
multiplyMatrix(firstarray,secondarray);
cout<<" ";
//matrix multiplication BA
multiplyMatrix(secondarray,firstarray);
system("pause");
return 0;
}

*********************************************************************************

AB not equals to BA

Both matrix multiplication result is not equal. They will be different and they are not commutative.