Question

MUST BE PROCEDURAL CODE. Write a program in C++ that reads two matrices and: 1) adds...

MUST BE PROCEDURAL CODE. Write a program in C++ that reads two matrices and: 1) adds them (if compatible) and prints their sum, 2) subtracts them (if compatible) and prints their difference, and 3) multiplies them (if compatible) and prints their product. Prompt the user for the names of two matrix input files (see format below) and place the output in a file of the user’s choosing.

The format of the input files should contain the number of rows, followed by the number of columns, followed by the matrix data; here is an example of a 2 by 3 matrix input file:

2 3

1 -1 0

2 0 1

Although the examples here use integers as elements of matrices, your code should allow matrices to contain doubles.

Here is an example of the output expected:

Matrix A =

1 -1 0

2 1 1

0 -1 1

Matrix B =

1 -2 1

0 1 0

1 -1 1

A + B =

2 -3 1

2 2 1

1 -2 2

A - B =

0 1 -1

2 0 1

-1 0 0

A * B =

1 -3 1

3 -4 3

1 -2 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have added the code below !

It is a procedural code having different functions for different procedures !

Comments are added to signify which operation is being performed !

For taking input, I have displayed proper msgs to the user to explain what value is needed next !

Fuctions are performed in this order -

1) Addition

2) Subtraction

3) Multiplication

CODE :

#include <iostream>
using namespace std;

double matA[100][100];
double matB[100][100];
double addResult[100][100];
double subResult[100][100];
double mulResult[100][100];


void displayMatrix(double mat[][100], int row, int col){
  
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
cout<<mat[i][j];
cout<<" ";
}
cout<<endl;
}
cout<<endl;
  
}

void addMatrices(int Arow, int Acol, int Brow, int Bcol){
  
//addition
  
if(Arow != Brow || Acol != Bcol){
cout<<"The two input matrices are not capable of addition";
}
else{
  
for(int i=0; i<Arow; i++){
for(int j=0; j<Acol; j++){
addResult[i][j]=matA[i][j]+matB[i][j];
}
}
  
cout<<"A + B ="<<endl;
displayMatrix(addResult, Arow, Acol);
}
  
}

void subtractMatrices(int Arow, int Acol, int Brow, int Bcol){
  
//subtraction
  
if(Arow != Brow || Acol != Bcol){
cout<<"The two input matrices are not capable of subtraction";
}
else{
  
for(int i=0; i<Arow; i++){
for(int j=0; j<Acol; j++){
subResult[i][j]=matA[i][j]-matB[i][j];
}
}
  
cout<<"A - B ="<<endl;
displayMatrix(subResult, Arow, Acol);
}
  
}

void multiplyMatrices(int Arow, int Acol, int Brow, int Bcol){
  
//multiply
  
if(Acol != Brow){
cout<<"The two input matrices are not capable of multiplication";
}
  
else{
  
for(int i=0; i<Arow; i++){
for(int j=0; j<Brow; j++){
mulResult[i][j]=0;
for(int k=0; k<Acol; k++){
mulResult[i][j] += (matA[i][k] * matB[k][j]);
}
}
}
  
cout<<"A * B ="<<endl;
displayMatrix(mulResult, Arow, Bcol);
}
  
}

int main() {
  
int Arow, Acol;
cout<<"Please enter the number of rows for matrix A";
cin>>Arow;
cout<<"Please enter the number of coloumns for matrix A";
cin>>Acol;
  
cout<<"Please enter the elements for the matrix A";
for(int i=0; i<Arow; i++){
for(int j=0; j<Acol; j++){
cin>>matA[i][j];
}
}
  
int Brow, Bcol;
cout<<"Please enter the number of rows for matrix B";
cin>>Brow;
cout<<"Please enter the number of coloumns for matrix B";
cin>>Bcol;
  
cout<<"Please enter the elements for the matrix B";
for(int i=0; i<Brow; i++){
for(int j=0; j<Bcol; j++){
cin>>matB[i][j];
}
}
  
//display
cout<<endl;
cout<<"Matrix A ="<<endl;
for(int i=0; i<Arow; i++){
for(int j=0; j<Acol; j++){
cout<<matA[i][j];
cout<<" ";
}
cout<<endl;
}
cout<<endl;
  
cout<<"Matrix B ="<<endl;
for(int i=0; i<Brow; i++){
for(int j=0; j<Bcol; j++){
cout<<matB[i][j];
cout<<" ";
}
cout<<endl;
}
cout<<endl;
  
  
//addition
  
addMatrices(Arow, Acol, Brow, Bcol);
  
  
//subtraction
  
subtractMatrices(Arow, Acol, Brow, Bcol);
  
  
//multiply
  
multiplyMatrices(Arow, Acol, Brow, Bcol);
  
return 0;
  
}

Add a comment
Know the answer?
Add Answer to:
MUST BE PROCEDURAL CODE. Write a program in C++ that reads two matrices and: 1) adds...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • in c++ Develop a program (name it AddMatrices) that adds two user provided matrices. The matrices...

    in c++ Develop a program (name it AddMatrices) that adds two user provided matrices. The matrices must of the same size. The program defines method Addition() that takes two two-dimensional arrays of integers and returns their addition as a two-dimensional array. The program main method defines two 3-by-3 arrays of type integer. The method prompts the user to initialize the arrays. Then it calls method Addition(). Finally, it prints out the array retuned by method Addition(). Document your code, and...

  • MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random...

    MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random words from a training set as explained in the lectures on graphs. Do not hard-code the training set! Read it from a file. A suggested format for the input file: 6 a e m r s t 10 ate eat mate meet rate seat stream tame team tear Here are some suggestions for constants, array declarations, and helper functions #include <iostream> #include <fstream> #include...

  • This needs to be linux compatible in C++ code. Write a program that reads a file...

    This needs to be linux compatible in C++ code. Write a program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167,...

  • Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp...

    Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp takes a single argument, which is the path to a file containing a square (k × k) matrix M and a non-negative exponent n. It computes M and prints the result Note that the size of the matrix is not known statically. You ust use malloc to allocate space for the matrix once you obtain its size from the input file. Tocompute M". it...

  • Write a MATLAB program that loads two matrices A and B from the files hw41matA.txt andhw41matB.txt...

    Write a MATLAB program that loads two matrices A and B from the files hw41matA.txt andhw41matB.txt and multiplies them together to obtain matrix C such that the element C(i,j) ofmatrix C is given by: C(i, j) =  ∑A(i, k) ∗ B(k, j) where n = number of columns in A = number of rows in B. Display the matrix C in defaultMATLAB format.
You cannot use the MATLAB matrix multiply or other inbuilt MATLAB functions forarithmetic operations. You must implement it....

  • Here's the main function of a C program that Reads in two matrices by prompting the...

    Here's the main function of a C program that Reads in two matrices by prompting the user for their dimensions, dynamically allocating memory from the heap, and reading the values into the memory using row major ordering for the matrix values. Multiplies the two matrices together and puts the result into another dynamically allocated piece of memory (after checking that the dimensions are appropriate for matric multiplication). Outputs the two input and the result matrices. Write the implementations of the...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • C++ must use header files and implementation files as separate files. I’ll need a header file,...

    C++ must use header files and implementation files as separate files. I’ll need a header file, implementation file and the main program file at a minimum. Compress these files into one compressed file. Make sure you have adequate documentation. We like to manipulate some matrix operations. Design and implement a class named matrixMagic that can store a matrix of any size. 1. Overload the addition, subtraction, and multiplication operations. 2. Overload the extraction (>>) and insertion (<<) operators to read...

  • Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix...

    Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix algebra: A vector is a one-dimensional set of numbers, such as [42 9 20]. The dot product of two equal-length vectors A and B is computed by multiplying the first entry of A by the first entry of B, the second entry of A by the second entry of B, etc., and then summing these products. For example, the dot product of [42 9...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT