Matrices In this exercise you will write several functions t

Matrices In this exercise you will write several functions that operate on matrices or two-dimensional arrays. For this exercise, we will restrict our attention to square matrices-collections of numbers with the same number of columns and rows. You will place all the function prototypes in a header file named matrix.h with their definitions in a source file named matrix.c. You should test your functions by writing a driver program, but you need not hand it in. Write a function that takes two matrices and determines if the sum of all of the elements in matrix A is equal to the sum of all the elements in matrix B. Return 0 if the summation is equal and 1 if they are not equal. int sumEqual(int **A, int **B, int n); Write a function that takes two matrices and determines if they are equal (all of their elements are the same). If the matrices are equal, return 0. Return 1 if they are not equal. int isEqual(int **A, int **B, int n); Write a function to compute the sum of the elements along the diagonal of the matrix. Return the sum of the elements along the diagonal. int diagonal (int **A, int n); Write a function to compute the summation of matrix A and B. int** sumMatrix(int **A, int **B, int n); The product of two square n times n matrices: C = A times B The entries for C are: c_ij = sigma _k = 1^n a_ikb_kj where 1 lessthanorequalto i,  j lessthanorequalto n and c_ij is the (i, j)-the entry of the matrix C. Write the following function to compute the product of two n times n square matrices: int ** product (int **A, int **B, int n);

Solution

Here is the code for first few functions.

#include <stdio.h>
#include <stdlib.h>
int sumEqual(int **A, int **B, int n)
{
int sum1 = 0;
int sum2 = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
sum1 += *(*(A + i) + j);
sum2 += *(*(B + i) + j);
}
if(sum1 == sum2)
return 1;
return 0;   
}
int isEqual(int **A, int **B, int n)
{
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
if(*(*(A + i) + j) != *(*(B + i) + j))
return 0;
return 1;   
}
int diagonal(int **A, int n)
{
int sum = 0;
for(int i = 0; i < n; i++)
sum += *(*(A + i) + i);
return sum;
}
int** sumMatrix(int**A, int**B, int n)
{
int **array;  
array = (int **)malloc(n * sizeof(int *));  
for(int a = 0; a < n; a++)      
array[a] = (int *)malloc(n * sizeof(int));  
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
*(*(array + i) + j) = *(*(A + i) + j) != *(*(B + i) + j);
return array;
}

 Matrices In this exercise you will write several functions that operate on matrices or two-dimensional arrays. For this exercise, we will restrict our attentio

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site