Question

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 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

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

Here you go. Explanation goes with code flow.

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int** Addition(int A[][3], int B[][3]){
  
// Declare c to add matrix A B
int** c = 0;
c = new int*[3];
  
for(int i=0;i<3;i++){
c[i] = new int[3];
for(int j=0;j<3;j++){
c[i][j] = A[i][j] + B[i][j];
}
}
  
// Return C
return c;
}

int main() {
  
// Initilize 2D array matriccies
int matrix1[3][3], matrix2[3][3];
  
// Take inputs
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cout << "Enter value of matrix1[" << i << "][" << j << "]: ";
cin >> matrix1[i][j];
}
}
  
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cout << "Enter value of matrix2[" << i << "][" << j << "]: ";
cin >> matrix2[i][j];
}
}
  
// Call the Adition function
int **result = Addition(matrix1, matrix2);
  
// Print the returned matrix
cout << "Final Matrix:"<<endl;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cout << result[i][j] << " ";
}
cout << endl;
}
  
   return 0;
}

O/P:

Enter value of matrix1[0][0]: 1
Enter value of matrix1[0][1]: 2
Enter value of matrix1[0][2]: 3
Enter value of matrix1[1][0]: 4
Enter value of matrix1[1][1]: 5
Enter value of matrix1[1][2]: 6
Enter value of matrix1[2][0]: 7
Enter value of matrix1[2][1]: 8
Enter value of matrix1[2][2]: 9
Enter value of matrix2[0][0]: 1
Enter value of matrix2[0][1]: 1
Enter value of matrix2[0][2]: 1
Enter value of matrix2[1][0]: 1
Enter value of matrix2[1][1]: 1
Enter value of matrix2[1][2]: 1
Enter value of matrix2[2][0]: 1
Enter value of matrix2[2][1]: 1
Enter value of matrix2[2][2]: 1
Final Matrix:
2 3 4
5 6 7
8 9 10

CODE:

#include <iostream> #include <bits/stdc++.h> 4 using namespace std; int** Addition(int A[][3], int B[][3]){ ovou // Declare c

35 36 37 38 for(int i=0;i<3;i++){ for(int j=0; j<3;j++){ cout << Enter value of matrix2[ << i << [ « cin >> matrix2[i][j]

O/P:

Enter value of matrix1[0][0]: 1 Enter value of matrix1[0] [1]: 2, Enter value of matrix1[0] [2]: 3 Enter value of matrix1[1]


Add a comment
Know the answer?
Add Answer to:
in c++ Develop a program (name it AddMatrices) that adds two user provided matrices. The matrices...
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
  • SOLVE IN PYTHON: Exercise #2: Develop a program (name it LocateLargestElement) that prints out the location...

    SOLVE IN PYTHON: Exercise #2: Develop a program (name it LocateLargestElement) that prints out the location of the first largest value in a two-dimensional array. The largest values may appear more than once in the array. The program defines method locateLargest() that takes a two-dimensional array of integers and returns the location (row index and column index) of the first largest value as a single-dimensional array. The program main method prompts the user to enter a 3-by-4 matrix, prints out...

  • In pseudocode only Design (pseudocode) a program (name it Occurrences) to determine whether two two-dimensional arrays...

    In pseudocode only Design (pseudocode) a program (name it Occurrences) to determine whether two two-dimensional arrays are equivalent or not. Two arrays are equivalent if they contain the same values in any order. The program main method defines two two-dimensional array of size 3-by-3 of type integer, and prompts the user to enter integer values to initialize the arrays. The main method calls method isEquivalent()that takes two two-dimensional arrays of integer and returns true (boolean value) if the arrays contain...

  • IN C#.Develop a program that prints out the location of the largest value in a two-dimensional...

    IN C#.Develop a program that prints out the location of the largest value in a two-dimensional array. The largest values may appear more than once in the array, so you must only print out the first instance. The program defines method locateLargest() that takes a two-dimensional array of integers and returns the location (row index and column index) of the first largest value as a single-dimensional array. The program main method prompts the user to enter a 3-by-4 matrix, prints...

  • IN PYTHON WITHOUT USING: .APPEND/ .SORT/ INSERT / .SPLIT Program 1: Design (pseudocode) and implement (source...

    IN PYTHON WITHOUT USING: .APPEND/ .SORT/ INSERT / .SPLIT Program 1: Design (pseudocode) and implement (source code) a program (name it Occurrences) to determine whether two two-dimensional arrays are equivalent or not. Two arrays are equivalent if they contain the same values in any order. The program main method defines two two-dimensional array of size 3-by-3 of type integer, and prompts the user to enter integer values to initialize the arrays. The main method calls method isEquivalent()that takes two two-dimensional...

  • I need this code in C++. Should be able to read from a file as stated...

    I need this code in C++. Should be able to read from a file as stated in the directions Exercise #1: Develop a program that prints out the sum of each column of a two-dimensional array The program defines method sumColumn takes a two-dimensional array of integers and returns a single dimensional array that stores the sum of columns of the passed array. The program main method prompts the user to enter a 3-by-4 array, prints out the array, and...

  • SOLVE IN PYTHON: Exercise #2: Design and implement a program (name it CompareArrays) that compares the...

    SOLVE IN PYTHON: Exercise #2: Design and implement a program (name it CompareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array with integer values. The program defines method Compare() that takes two signal-dimensional arrays of type integer. The method compares the content of the arrays and returns true (Boolean type) if the arrays store same values in the...

  • C# ONLY INCLUDE BOTH PSUDEO CODE AND SOURCE CODE!!!! Design (pseudocode) and implement (source code) a...

    C# ONLY INCLUDE BOTH PSUDEO CODE AND SOURCE CODE!!!! Design (pseudocode) and implement (source code) a program (name it IndexOfLargest) to find the index of the first largest value in the array. Note that the largest value may appear more than once in the array. The program main method defines a single-dimensional array of size 10 elements and prompts the user to enter 10 integers to initialize the array. The main method then calls method findIndex() that takes a single-dimensional...

  • Note: According to the question, please write source code in java only using the class method....

    Note: According to the question, please write source code in java only using the class method. Sample Run (output) should be the same as displayed in the question below. Make sure the source code is working properly and no errors. Exercise #2: Design and implement a program (name it compareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Exercise #2:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Exercise #2: Design and implement a program (name it CompareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array with integer values. The program defines method Compare() that takes two signal-dimensional arrays of type integer. The method compares the content of the arrays and returns...

  • C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional...

    C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional arrays. Your program should include three functions: one for getting input data, one for calculating the sum of matrices, and one for printing the result. a) Function inputMatrix: This Function prompts the user to enter the data and stores data inside two-dimensional array. b) Function addMatrices: This function calculatesthe sum result of two matrices. c) Function printMatrix: This function prints the matrix to screen....

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