d. 135 pts] Function addMat takes in 2 square matrices (matA and mat8) ofsize n
ID: 3699169 • Letter: D
Question
d. 135 pts] Function addMat takes in 2 square matrices (matA and mat8) ofsize n xn. Complete the addMat function with the following specifications: i. Allocate an nxn integer matrix and have it pointed by pointer matR i. Perform matrix addition: matR matA+matB ili. Return resulting matirx at the end of function v. Be aware of the data type declared in the function. v. You must not use any array annotation, such as 1, in this problem. Any use of array annotation will result in receiving O point. int addMat( int** matA, int* matB, int n) int matR: //Hint: A double pointer is a pointer to pointers //allocate memory here: //add matrices here: //Return resulting natirx here:Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n=3, i, j, count;
// Allocate memory to matrix A
int **matA = (int **)malloc(n * sizeof(int *));
for (i=0; i<n; i++)
*(matA+i)= (int *)malloc(n * sizeof(int));
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf("%d",(*(matA+i)+j));
// Allocate memory to matrix B
int **matB = (int **)malloc(n * sizeof(int *));
for (i=0; i<n; i++)
*(matB+i)= (int *)malloc(n * sizeof(int));
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf("%d",(*(matB+i)+j));
// Allocate memory to matrix C
int **matC = (int **)malloc(n * sizeof(int *));
for (i=0; i<n; i++)
*(matC+i)= (int *)malloc(n * sizeof(int));
// Addition of matrix
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
*(*(matC + i) + j) = *(*(matA + i) + j) + *(*(matB + i) + j);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
printf("%d ", *(*(matC+i)+j));
return matC;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.