C++ In this problem you will implement integer matrix addition. This will requir
ID: 3796409 • Letter: C
Question
C++ In this problem you will implement integer matrix addition. This will require you to
dynamically allocate space for a 2 dimensional array. An example of matrix addition is:
123 101 1+12+03+1 222 A+B= 085 + 010 = 0+08+15+0 = 095 =C
In general, if A and B are matrices with the same number of rows and columns, then their sum C = A + B is defined by C[i][j] = A[i][j] + B[i][j]. Here, A[i][j] is the integer in the ith row and jth column of A. In the example above, A[1][1] = 1, A[1][3] = 3, and A[2][2] = 8.
This program has been partially written for you in matrixaddition.c. Write the body of functions that are marked with a comment that begins with
Do not modify other parts of the code.
-------------------------------------------------------------
#include
#include
#include
int** getMatrix(int n, int m);
int** allocateMatrix(int n, int m);
int** addMatrices(int** A, int** B, int n, int m);
void printMatrix(int** A, int n, int m);
void deallocateMatrix(int** A, int n);
// This program reads in two n by m matrices A and B and
// prints their sum C = A + B
//
// This function is complete, you do not need to modify it
// for your homework
int main() {
int n = 0, m = 0;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &n, &m);
assert(n > 0 && m > 0);
printf("Enter matrix A: ");
int** A = getMatrix(n, m);
printf("Enter matrix B: ");
int** B = getMatrix(n, m);
int** C = addMatrices(A, B, n, m);
printf("A + B = ");
printMatrix(C, n, m);
deallocateMatrix(A, n);
deallocateMatrix(B, n);
deallocateMatrix(C, n);
}
// Creates a new n by m matrix whose elements are read from stdin
//
// This function is complete, you do not need to modify it
// for your homework
int** getMatrix(int n, int m) {
int** M = allocateMatrix(n, m);
for (int i = 0; i < n; i++) {
printf("Input row %d elements, separated by spaces: ", i);
for (int j = 0; j < m; j++) {
scanf("%d", &M[i][j]);
}
}
return M;
}
// Allocates space for an n by m matrix of ints
// and returns the result
int** allocateMatrix(int n, int m) {
// Homework TODO: Implement this function
}
// Adds two matrices together and returns the result
int** addMatrices(int** A, int** B, int n, int m) {
// Homework TODO: Implement this function
}
// Prints out the entries of the matrix
void printMatrix(int** A, int n, int m) {
// Homework TODO: Implement this function
}
// Deallocates space used by the matrix
void deallocateMatrix(int** A, int n) {
for (int i = 0; i < n; i++) {
free(A[i]);
}
free(A);
}
---------------------------------------------------------------------
Example output:
Enter the number of rows and columns: 2 2 Enter matrix A:
Input row 0 elements, separated by spaces: 1 0 Input row 1 elements, separated by spaces: 0 1 Enter matrix B:
Input row 0 elements, separated by spaces: 2 2 Input row 1 elements, separated by spaces: 3 4 A+B=
32
35
Enter the number of rows and columns: 3 6
Enter matrix A:
Input row 0 elements, separated by spaces: 1 2 3 4 5 6
Input row 1 elements, separated by spaces: 0 0 0 0 0 0
Input row 2 elements, separated by spaces: 10 5 23 84 91 2 Enter matrix B:
Input row 0 elements, separated by spaces: 9 8 7 6 5 4
Input row 1 elements, separated by spaces: 2 1 2 1 2 1
Input row 2 elements, separated by spaces: 14 83 62 93 45 32 A+B=
10 10 10 10 10 10
212121
24 88 85 177 136 34
Explanation / Answer
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int** getMatrix(int n, int m);
int** allocateMatrix(int n, int m);
int** addMatrices(int** A, int** B, int n, int m);
void printMatrix(int** A, int n, int m);
void deallocateMatrix(int** A, int n);
// This program reads in two n by m matrices A and B and
// prints their sum C = A + B
//
// This function is complete, you do not need to modify it
// for your homework
int main() {
int n = 0, m = 0;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &n, &m);
//assert(n > 0 && m > 0);
printf("Enter matrix A: ");
int** A = getMatrix(n, m);
printf("Enter matrix B: ");
int** B = getMatrix(n, m);
int** C = addMatrices(A, B, n, m);
printf("A + B = ");
printMatrix(C, n, m);
deallocateMatrix(A, n);
deallocateMatrix(B, n);
deallocateMatrix(C, n);
}
// Creates a new n by m matrix whose elements are read from stdin
//
// This function is complete, you do not need to modify it
// for your homework
int** getMatrix(int n, int m) {
int** M = allocateMatrix(n, m);
for (int i = 0; i < n; i++) {
printf("Input row %d elements, separated by spaces: ", i);
for (int j = 0; j < m; j++) {
scanf("%d", &M[i][j]);
}
}
return M;
}
// Allocates space for an n by m matrix of ints
// and returns the result
int** allocateMatrix(int n, int m) {
int i;
int** matrix = (int**)malloc(sizeof(int*) * n);
for (i=0; i < n; i++) {
matrix[i] = (int*)malloc(sizeof(int) * m);
memset(matrix[i], 0, m);
}
return matrix;
}
// Adds two matrices together and returns the result
int** addMatrices(int** A, int** B, int n, int m) {
// Homework TODO: Implement this function
int i=0,j=0;
int** matrix = (int**)malloc(sizeof(int*) * n);
for (i=0; i < n; i++) {
matrix[i] = (int*)malloc(sizeof(int) * m);
memset(matrix[i], 0, m);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
matrix[i][j]=A[i][j]+B[i][j];
}
}
return matrix;
}
// Prints out the entries of the matrix
void printMatrix(int** A, int n, int m) {
// Homework TODO: Implement this function
int i,j;
for( i=0;i<n;i++){
for(j=0;j<m;j++)
printf("%d ", A[i][j]);
printf(" ");
}
}
// Deallocates space used by the matrix
void deallocateMatrix(int** A, int n) {
for (int i = 0; i < n; i++) {
free(A[i]);
}
free(A);
}
Ouput:
Enter the number of rows and columns: 3 6
Enter matrix A:
Input row 0 elements, separated by spaces: 1 2 3 4 5 6
Input row 1 elements, separated by spaces: 0 0 0 0 0 0
Input row 2 elements, separated by spaces: 10 5 23 84 91 2
Enter matrix B:
Input row 0 elements, separated by spaces: 9 8 7 6 5 4
Input row 1 elements, separated by spaces: 2 1 2 1 2 1
Input row 2 elements, separated by spaces: 14 83 62 93 45 32
A + B =
10 10 10 10 10 10
2 1 2 1 2 1
24 88 85 177 136 34
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.