Question

In: Computer Science

*****In C++***** Exercise #3: Develop a program (name it AddMatrices) that adds two matrices. The matrices...

*****In C++***** Exercise #3: Develop a program (name it AddMatrices) that adds two 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 organize and space the outputs properly as shown below. Sample run 1: Matrix A: 2 1 2 7 1 8 3 20 3 Matrix B: 1 1 1 1 1 1 1 1 1 A + B: 3 2 3 8 2 9 4 21 4 Sample run 2: Matrix A: 2 2 2 2 2 2 2 2 2 Matrix B: 2 2 2 2 2 2 2 2 2 A + B: 4 4 4 4 4 4 4 4 4

Solutions

Expert Solution

If you have any queries write a comment if understood upvote Thank you.

Here to return a two dimensional array i have used double pointer

SOLUTION:

#include <bits/stdc++.h>
using namespace std;
#define N 3
int** addition(int A[][N], int B[][N]);
// This function adds A[][] and B[][], and stores its resukt into a double pointer
//and return that
int** addition(int A[][N], int B[][N])
{
int i, j;
//create a double pointer
   int** C=0;
   C=new int*[3];
   //create its array in one dimesional i.e rows
for (i = 0; i < N; i++) {
   //create array into two dimensional based on the columns
   C[i]=new int[3];
for (j = 0; j < N; j++)
//add and store data into the C
C[i][j] = A[i][j] + B[i][j];
       }
return C;//return the pointer
}
  
// main function
int main()
{
int i, j,A[3][3],B[3][3];
//create two arrays
cout<<"Matrix A: ";
//read data to A matrix
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
{
   cin>>A[i][j];
       }
   cout<<"Matrix B :";
   //read data to B matrix
   for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
{
   cin>>B[i][j];
       }
   //call the addition function and store it into a pointer
   int** C=addition(A,B);
   cout<<"A+B: ";
   //print data
   for (i = 0; i < N; i++) {
for (j = 0; j < N; j++)
{
   cout<<C[i][j]<<" ";
       }
}
}

CODE IMAGE:

OUTPUT:


Related Solutions

**** In C++ ****Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4...
**** In C++ ****Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax(int[] arr) returns the maximum value in the an array int arrayMin(int[] arr) returns the minimum value in an array void arraySquared(int[] arr) changes every value in the array to its square (value²) void arrayReverse(int[] arr) reverses the array (for example: array storing 7 8 9 becomes 9 8 7 ) The program main method creates a single-dimensional array of...
write a c++ program to read two matrices with any size. Your program should have at...
write a c++ program to read two matrices with any size. Your program should have at least the following functions Main() Read a Matrix Add two matrices Subtract two matrices multiply two matrices display a matrice
Write a C++ program to determine the sum of two 1-D matrices: A = [a b...
Write a C++ program to determine the sum of two 1-D matrices: A = [a b c d] B = [e f g h] The user will provide the values of a to h.
C++ Create a program that use the linkedbag 3. Develop a program to maintain a list...
C++ Create a program that use the linkedbag 3. Develop a program to maintain a list of homework assignments. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services: • Add a new assignment. • Remove an assignment. • Provide a list of the assignments in the order they were assigned. • Find the assignment(s) with the earliest...
3. Write a C++ program that takes in the name of a store, and the following...
3. Write a C++ program that takes in the name of a store, and the following details for 4 employees working at the store; their first name, last name, number of hours they worked that week and how much they are paid per hour. Your program should output the name of the store, along with each employee's full name and how much they earned that week in the manner below. Monetary values should be format to 2 decimal places. Also...
Computer Science: Data Structures and Algorithms Practice: Construct a C++ program that takes two SQUARE matrices,...
Computer Science: Data Structures and Algorithms Practice: Construct a C++ program that takes two SQUARE matrices, and multiplies them and produces a new matrix: Example: [Matrix A] * [Matrix B] = [Matrix C] (A 3x3 is the most preferred example to use) (The first two matrices, A and B can be fixed numbers, hard-coded into the program. as opposed to user input) Display Matrix C, also (cout etc.)
Complete the following program so that C = A*B where A and B are the matrices...
Complete the following program so that C = A*B where A and B are the matrices defined below. N=4; M=3; A=rand(M,N); B=rand(N,M); C = ? for m = 1: M    for n = 1 : ? for p = 1 : ? C (m,n) = ?       end    end end You may upload a script
C PROGRAM ..... NOT C++ PROGRAM 3 (UPDATE TO TWO PRIOR PROGRAMS FOR REFERENCE OF PRIOR...
C PROGRAM ..... NOT C++ PROGRAM 3 (UPDATE TO TWO PRIOR PROGRAMS FOR REFERENCE OF PRIOR PROGRAM INSTRUCTIONS PLEASE SEE BELOW) PROGRAM 3 Adjust program II to make use of functions. All the same rules from the previous program specifications still apply, for example input gathering, output formatting and breaking on -1 still apply. Additional requirements. Write a function that prompts the user for hours worked, rate and name. Use parameter passing, and pass by reference. Write a function that...
This exercise allows a user to enter the values of two, 3x3 matrices and then select...
This exercise allows a user to enter the values of two, 3x3 matrices and then select from options including, addition, subtraction, matrix multiplication, and element by element multiplication. You should use numpy.matmul() for matrix multiplication (e.g. np.matmul(a, b) ). The program should computer the appropriate results and return the results, the transpose of the results, the mean of the rows for the results, and the mean of the columns for the results. When entering data you should check that each...
Exercise 7: Name that Shape Write a program that determines the name of a shape from...
Exercise 7: Name that Shape Write a program that determines the name of a shape from its number of sides. Read the number of sides from the user and then report the appropriate name as part of a meaningful message. Your program should support shapes with anywhere from 3 up to (and including) 10 sides. If a number of sides outside of this range is entered then your program should display an appropriate error message. language use : python
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT