Explore BrainMass

Dealing with Matrices in C++

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

Write a C++ program that uses class Matrix for dealing with the square matrices (n x n tables of integers). The class constructor has to create a dynamic nxn matrix for n passed as a parameter. Supply the class with a copy constructor. Two overloaded operators + and * have to be implemented for adding and multiplying two matrices, respectively. The sum and product of two matrices A and B are square matrices with the entries defined as follows:
Cij = Aij + Bij, i=1,...,n, j=1,...,n
Cij = Ai1*B1j + Ai2*B2j + ... + Ain*Bnj

Where Aij is the matrix entry at the intersection of the i-th row and j-th column with rows numbered from top to bottom (and similarly for the matrices B and C).

The matrix elements have to be initialized by the method setMatrix() that reads the matrix elements from the standard input from a file by using the redirection of input. The file have to contain 2·n2 matrix elements (for two nxn matrices) separated by spaces corresponding to the row-by-row scan of each matrix. The program must figure out the matrix dimension n automatically (n must not be a part of input).

© BrainMass Inc. brainmass.com October 25, 2018, 12:47 am ad1c9bdddf

Solution Summary

This solution provides a C++ program that uses a class matrix for dealing with the square matrices.

See Also This Related BrainMass Solution


Attached is a C++ program four of us did many years ago.I have a much better understanding of C++ than Matlab, but in this program their are many arrays I think in the form of for example j[]=[3 5 6], etc. First, I am terrible at arrays and through this program, which another guy took care of the arrays part since he did a previous robot arm movement program before this program was made did the array parts.

First, please help me...An array is nothing more than a row vector or like in MAtlab right, which stores a set of numbers to be used elsewhere right?

Second, now some are in form j[] and some in form j[10], what is difference?

Next, look at the fprint statements on the 3rd page of the program attached. What does thefprintf(stdprn,I%d,....do and what is going on with that code compared to the rest of the code, then fprintf(stdprn, S5/nN...etc. Explain all this please!

Fouth, in the same routine on 3rd page for example, for int (j=0; j<6, j++) I know this for loop takes all 10 values from each array and outputs them somehow - explain how, and also in the code routine you have arrays b5 - h5, the the for loop using the variable j. OK, in programming or this program how does the for loop know to execute each array b5 - h5 and its elements if the variable j in the for loop does not have any connection with the arrays? In other words I do not see how hte code works, or is it just the fact as the code executes from the fisrt line to the last the code is in order. Bare with me. As the arrays b5 - h5 etc are listed in the code, or its order, the next statement of the for loop is basically just execusted for the arrayss. In other words C++ looks above the for loop code and says, oh, everything above me needs to be outputted until my for loop according to the boundaries set( j=0; j<6, j++) , then go to the next line of code beneath me and keep running the program till the end, etc? Is this logic correct?

When answering please list each answer as answer1, answer 2, etc. Thanks!

View Full Posting Details