In this problem you will implement integer matrix addition. This will require yo
ID: 3796933 • Letter: I
Question
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:
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.
Here are examples of what the output should look like:
example 1:
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=
3 2
3 5
Example 2:
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
Here is the code in question:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
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);
}
1231 11 0 1 1+12+0 3+11 12 24 A+B=10 8 510 1 A+B=10 8 5H0 1 01 =10+0 0851:10 1 01-10+0 8+1 5+01-10 9 5 C 45 29 20 10 35 28 10 10 10 10 35 28 10Explanation / Answer
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
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 **arr = (int **)malloc(n * sizeof(int *));
int i = 0;
for (i=0; i<n; i++)
arr[i] = (int *)malloc(m * sizeof(int));
return arr;
// Homework TODO: Implement this function
}
// Adds two matrices together and returns the result
int** addMatrices(int** A, int** B, int n, int m) {
int i,j;
int** C = allocateMatrix(n, m);
for(i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
C[i][j] = A[i][j] + B[i][j];
}
}
// Homework TODO: Implement this function
}
// Prints out the entries of the matrix
void printMatrix(int** A, int n, int m) {
int i,j;
for(i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
printf("%d ", A[i][j]);
}
printf(" ");
}
// 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);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.