Matrix_Utils.c C program help. Hello, I am stumped with this problem and I do no
ID: 3802803 • Letter: M
Question
Matrix_Utils.c C program help.
Hello,
I am stumped with this problem and I do not know where to go. I am in an introductory programming class in C language and I have this problem due later in the week.
It is quite lengthy so I have included a screen grab. Please note: all functions must be implemented exactly as they are stated in the problem to recieve credit, also I am using C LANGUAGE.
Thank you very much.
2. Matrix Utilities (matrix.utils c) A matrix is a two dimensional array. It can also be descried as a pointer to a pointer or In the product function you will find the matrix product of multiplying two matrices an array of pointers. In this program we will write several small utilitiy functions to test together. Function takes two matrices and a size integer and returns a new matrix that is the result of matrix multiplicati ion. Matrix multiplication is done as follows: mastery of this concept. The function signatures and descriptions follow below. Also u will want to include the stdbool.h header file for this exercise that contains the definition of a an enumerated type named "bool" should only two values: true and be a square matrix meaning false. You may make the assumption that the matrix w A x B that the number of rows equals the number of columns in the matrix. The entries for C Implement these functions: make Matrix int n) int void printMatrix(int A, int na where is i, S n and cu is he (ij)-th entry of the matrix C. bool sumalEqual(int A, int eB, int n) Example (product): bool isEqual Cint A, int B, int n); int diagonal (int eeA, int n intee sunMatrix (int A int B, int n) int product int .A, int B, int n Please read the description of these functions carefully. The make Matrix function should dynamically allocate memory for a matrix of si and allow the user to enter values for each position of the matrix from left to right row by row then return the created matrix to the main function. The print Matrix function should print out a matrix row by row legibly enough so that a grader can see which number corresponds to each position of the matrix. In the sumEqual function you will sum up all the elements in each matrix and see if the sum of each array is equal to one another. The function returns true if the sums are equal or returns false if the sums do not equal one another. Function takes two d a size matrices an Example (sumEqual): 1 2 3 4 In the isEqual function you will check each matrix element by element and see if each element matrix A is equal to its corresponding element in Matrix B. Return true if this is the case and false if even one element is not equal to its corresponding element in he other matrix. Function takes two matrices and a size Example (isEqual): Your main function should prompt the user to input the size of the two matrices, and hen create the two matrices by calling appropriate functio Them it should call the following functies the town matriere: Fgnal, isFqual, diagnnal, Matri Sample output from the program: 3 4 ter val matrix A In the diagonal function you will calculate the product of the elements along the diagonal of the matrix and return that number. Function takes a single matrix and an integer size of that matrix and returns the product. Matrix A 3 4 Enter values or natrix B Example (diagonal): Sun of enents in each trix is equal Matrix function you will find the summation of the two matries Matrix integer and retums matrix that is the element by element sammation. The product along the diagonal A and B Example (sum Matrix 67 3 4 The product o matrix A and B is 55Explanation / Answer
#include<stdio.h>
#include <stdlib.h>
#include<stdbool.h>
//Creates a matrix dynamically and returns it
int ** makeMatrix(int n)
{
int i, j;
//Creates row size for the matrix
int **arr = (int **)malloc(n * sizeof(int *));
//Creates column size for the matrix
for (i = 0; i < n; i++)
arr[i] = (int *)malloc(n * sizeof(int));
//Accepts data
printf(" Enter data for %d x %d matrix ", n, n);
//Loops till end of row
for (i = 0; i < n; i++)
{
//Loops till end of column
for (j = 0; j < n; j++)
{
printf(" Enter data for [%d][%d]",i, j);
scanf("%d", &arr[i][j]); // OR *(*(arr+i)+j) = ++count
}//End of inner for
}//End of outer for
//Returns arr
return arr;
}//End of function
//To display the matrix
void printMatrix(int **A, int n)
{
int r, c;
//Loops till end of row
for (r = 0; r < n; r++)
{
//Loops till end of column
for (c = 0; c < n; c++)
{
printf("%d ", A[r][c]);
}//End of inner loop
printf(" ");
}//End of outer loop
}//End of function
//Returns true if sum of the matrix A and sum of the matrix B is equal otherwise false
bool sumEqual(int **A, int ** B, int n)
{
int r, c, sumA = 0, sumB = 0;
//Loops till end of row
for (r = 0; r < n; r++)
{
//Loops till end of column
for (c = 0; c < n; c++)
{
//Calculates sum for matrix A
sumA += A[r][c];
//Calculates sum for matrix B
sumB += B[r][c];
}//End of inner loop
}//End of outer loop
//Checks if sumA is equals to sumB return true otherwise return false
if(sumA == sumB)
return true;
else
return false;
}//End of function
//Returns true if matrix A and matrix B is same
bool isEqual(int **A, int **B, int n)
{
int r, c, flag = 0;
//Loops till end of row
for (r = 0; r < n; r++)
{
//Loops till end of column
for (c = 0; c < n; c++)
{
//Checks the contents of A and B is not equal set the flag value to 1 and come out of the loop
if(A[r][c] != B[r][c])
{
flag = 1;
break;
}
}//End of inner loop
//Check flag value if it is 0 return true otherwise return false
if(flag == 0)
return true;
else
return false;
}//End of outer loop
}
int diagonal(int **A, int n)
{
int r, c, diagonalProduct = 1;
//Loops till end of row
for (r = 0; r < n; r++)
{
//Loops till end of column
for (c = 0; c < n; c++)
{
if(r == c)
diagonalProduct *= A[r][c];
}//End of inner loop
}//End of outer loop
//Returns the production of diagonal
return diagonalProduct;
}//End of function
//Adds matrix A and matrix B stores the result in matrix C and returns the matrix C
int ** sumMatrix(int **A, int **B, int n)
{
int r, c;
//Creates row size for the matrix
int **C = (int **)malloc(n * sizeof(int *));
//Creates column size for the matrix
for (r = 0; r < n; r++)
C[r] = (int *)malloc(n * sizeof(int));
//Loops till end of row
for (r = 0; r < n; r++)
{
//Loops till end of column
for (c = 0; c < n; c++)
{
//Adds the each element of the matrix A with each element of matrix B and stores the result in matrix C.
C[r][c] = A[r][c] + B[r][c];
}//End of inner loop
}//End of outer loop
//Returns matrix C
return C;
}//End of function
//Multiply Matrix A and Matrix B stores the result in Matrix C and return matrix C
int ** product(int **A, int **B, int n)
{
int r, c, k, sum = 0;
//Creates row size for the matrix
int **C = (int **)malloc(n * sizeof(int *));
//Creates column size for the matrix
for (r = 0; r < n; r++)
C[r] = (int *)malloc(n * sizeof(int));
//Loops till end of row
for (r = 0; r < n; r++)
{
//Loops till end of column
for (c = 0; c < n; c++)
{
//Loops till end of column
for (k = 0; k < n; k++)
{
sum = sum + A[r][k] * B[k][c];
}//End of loop for variable k
C[r][c] = sum;
sum = 0;
}//End of loop for variable c
}//End of loop for variable r
//Returns the product matrix
return C;
}//End of function
//Main function
int main()
{
//For size of matrix
int n;
//Declares pointer to pointer to store matrix return by the functions
int **mat1, **mat2, **mat3;
//Accepts size for matrix one
printf(" Enter the size of first matrix: ");
scanf("%d", &n);
//Creates first matrix
mat1 = makeMatrix(n);
//Creates and Accepts size for matrix two
printf(" Enter the size of second matrix: ");
scanf("%d", &n);
//Creates and Accepts data for matrix two
mat2 = makeMatrix(n);
//Display first matrix
printf(" First matrix contents: ");
printMatrix(mat1, n);
//Display second matrix
printf(" Second matrix contents: ");
printMatrix(mat2, n);
//Checks sum is equal or not for both the matrix
if(sumEqual(mat1, mat2, n))
printf(" Sum of both matrix is same ");
else
printf(" Sum of both matrix is not same ");
//Checks whether both the matrix are same or not
if(isEqual(mat1, mat2, n))
printf(" Both matrix are same ");
else
printf(" Both matrix are not same ");
//Displays diagonal product of first matrix
printf(" Diagonal Product of Matrix A = %d ", diagonal(mat1, n));
//Displays diagonal product of second matrix
printf(" Diagonal Product of Matrix B = %d ", diagonal(mat2, n));
//Calculates matrix sum
mat3 = sumMatrix(mat1, mat2, n);
//Displays matrix sum result
printf(" Matrix Sum ");
printMatrix(mat3, n);
//Calculates matrix product
mat3 = product(mat1, mat2, n);
//Displays matrix product result
printf(" Matrix Product ");
printMatrix(mat3, n);
}
Output 1:
Enter the size of first matrix: 2
Enter data for 2 x 2 matrix
Enter data for [0][0]1
Enter data for [0][1]2
Enter data for [1][0]3
Enter data for [1][1]4
Enter the size of second matrix: 2
Enter data for 2 x 2 matrix
Enter data for [0][0]1
Enter data for [0][1]2
Enter data for [1][0]3
Enter data for [1][1]4
First matrix contents:
1 2
3 4
Second matrix contents:
1 2
3 4
Sum of both matrix is same
Both matrix are same
Diagonal Product of Matrix A = 4
Diagonal Product of Matrix B = 4
Matrix Sum
2 4
6 8
Matrix Product
7 10
15 22
Output 2:
Enter the size of first matrix: 3
Enter data for 3 x 3 matrix
Enter data for [0][0]1
Enter data for [0][1]2
Enter data for [0][2]3
Enter data for [1][0]4
Enter data for [1][1]5
Enter data for [1][2]6
Enter data for [2][0]7
Enter data for [2][1]8
Enter data for [2][2]9
Enter the size of second matrix: 3
Enter data for 3 x 3 matrix
Enter data for [0][0]2
Enter data for [0][1]1
Enter data for [0][2]3
Enter data for [1][0]4
Enter data for [1][1]5
Enter data for [1][2]6
Enter data for [2][0]1
Enter data for [2][1]2
Enter data for [2][2]3
First matrix contents:
1 2 3
4 5 6
7 8 9
Second matrix contents:
2 1 3
4 5 6
1 2 3
Sum of both matrix is not same
Both matrix are not same
Diagonal Product of Matrix A = 45
Diagonal Product of Matrix B = 30
Matrix Sum
3 3 6
8 10 12
8 10 12
Matrix Product
13 17 24
34 41 60
55 65 96