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

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...
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...
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
Develop a C++/Python program using visual studio connected to mysql workbench to show all vendor's name...
Develop a C++/Python program using visual studio connected to mysql workbench to show all vendor's name and phone (vendor_name and vendor_phone) in the state "CA" and the city "Los Angeles". here is the database tables CREATE TABLE general_ledger_accounts ( account_number INT PRIMARY KEY, account_description VARCHAR(50) UNIQUE ); CREATE TABLE terms ( terms_id INT PRIMARY KEY AUTO_INCREMENT, terms_description VARCHAR(50) NOT NULL, terms_due_days INT NOT NULL ); CREATE TABLE vendors ( vendor_id INT PRIMARY KEY AUTO_INCREMENT, vendor_name VARCHAR(50) NOT NULL UNIQUE, vendor_address1...
You are to develop a program of exercise and diet to lose 10 pounds in the...
You are to develop a program of exercise and diet to lose 10 pounds in the course of 5 weeks based on your current body weight. If you don't want to reveal your weight make one up. Start with your current calorie need per day. Include calories for daily exercise and amount of calories you need to restrict per day.  
IN C LANGUAGE 16.15 Lab 5: filter Name this program filter.c. The program takes two command...
IN C LANGUAGE 16.15 Lab 5: filter Name this program filter.c. The program takes two command line arguments: the name of an input file and the name of an output file. The program should confirm the input and output files can be opened. If a file cannot be opened, print the error message Cannot open file '<filename>' where <filename> is the name of the file and return. fopen() will return 0 if it fails to open a file. Then, the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT