Question: Lab 1 1 : Intro to 2 D Arrays Exercise 1 - Matrix Addition 1 . Write a function that adds two M x N
Lab : Intro to D Arrays
Exercise Matrix Addition
Write a function that adds two M x N matrices:
declare M and N as global constants where M N
void matrixAdd const int aN const int bN int sumN;
The addition of two matrices is performed by adding elements in corresponding positions in the matrices. Therefore, matrices that are added must be of the same size; the result is another matrix of the same size.
Example of matrix addition:
Input: The program should take in two matrices as input from the user in the following form: User input is underlined and bolded
Enter Matrix A:
Enter Matrix B:
Output: The program should output the result of the sum of Matrix A and B in the following
form:
Res:
Exercise Matrix Multiplication
Write a function that multiplies a Q x R matrix by a R x S matrix produces a Q x S matrix:
declare Q R and S as global constants where Q R S
void matrixMultconst int aR const int bS int productS;
Matrix multiplication is not computed by multiplying corresponding elements of the two matrices.
The value in position cij of the product C of two matrices A and B is the product of row i of the
first matrix and column j of the second matrix:
The product of row i and column j requires that the row i and the column j have the same
number of elements. Therefore, the first matrix A must have the same number of elements in
each row as there are in the columns of the second matrix B Thus, if A and B both have five
rows and five columns, their product has five rows and five columns. Furthermore, for these
matrices, we can compute both AB and BA but in general, they will not be equal.
Another nonsquare example: if A has two rows and three columns and B has three
rows and three columns, the product AB will have two rows and three columns.
Given:
The first element in the product C AB is
Note: c will actually be stored in product
Similarly, we can compute the rest of the elements in the product of A and B:
Input: The program should take in two matrices as input from the user in the following form:
Enter Matrix Asize x:
Enter Matrix Bsize x:
Note: If cin breaks for any reason, all values in the array after that point must be As long as you initialize your arrays to and then read directly into the array, it will work properly.
Output: The program should output the result of the sum of Matrix A and B in the following
form:
Res:
Note: Neither function should output anything. Any outputs should be in main.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
