MATRIX TYPES
Write a C program to find the type of square matrix using functions:
a) Fn. findSymmetric which returns 1 if the matrix is symmetric and 0 if the matrix is not symmetric.
b) Fn. findIdentity which returns 1 if the matrix is an identity matrix and 0 if the matrix is not an identity matrix.
c) Fn. findMagic which returns 1 if the matrix is a magic square and 0 if the matrix is not a magic square. A Magic Square is a square matrix in which the sum of the elements in each row and column is the same.
d) Fn. findUpperTriangular which returns 1 if the matrix is an upper diagonal matrix and 0 if it is not. An upper triangular matrix is one in which all the elements below the diagonal are equal to 0.
e) Fn. findLowerTriangular which returns 1 if the matrix is a lower diagonal matrix and 0 if it is not.
A lower triangular matrix is one in which all the elements above the diagonal are equal to 0.
f) Fn. findEvenTrace which returns 1 if the sum of the diagonal (L->R) elements of the matrix is even and 0 otherwise.
g) Fn. findNonzero which returns 1 if the all the elements in the matrix are nonzero and 0 otherwise.
h) Fn. findSpecial which returns 1 if the integer n (n corresponds to the number of rows / columns of the matrix) is present in the matrix and 0 otherwise.
i) Fn. findSpecialTrace which returns 1 if the sum of the L-> R diagonal elements of the matrix is the same as the sum of the R->L diagonal elements of the matrix and 0 otherwise.
j) Fn. findLucky which returns 1 if all the elements in the matrix are divisible by the number of rows/columns in the matrix and 0 otherwise.
Use dynamic allocation of memory. (i.e use malloc()). Use functions for reading and displaying a matrix. The function template can be downloaded from the functions editor.
Note:
Refer to the problem requirements.
Input Format:
Input consists of (n*n)+1 integers.
The first integer corresponds to n, the number of rows/columns in the matrix.
The next 'n' integers correspond to the elements in the first row of the matrix.
The next 'n' integers correspond to the elements in the second row of the matrix and so on.
Output Format:
Refer Sample Output for formatting details.
Sample Input:
3
1
2
3
2
4
5
3
5
6
Sample Output:
1 2 3
2 4 5
3 5 6
Matrix is Symmetric
Matrix is not Identity
Matrix is not Magic
Matrix is not UpperTriangular
Matrix is not LowerTriangular
Matrix is not EvenTrace
Matrix is NonZero
Matrix is Special
Matrix is not SpecialTrace
Matrix is not Lucky
Problem Requirements:
int ** readMatrix(int rows, int cols)
void displayMatrix(int** a,int rows, int cols)
int findSymmetric(int ** a, int n)
int findIdentity(int ** a, int n)
int findMagic(int ** a, int n)
int findUpperTriangular(int ** a, int n)
int findLowerTriangular(int ** a, int n)
int findEvenTrace(int ** a, int n)
int findNonZero(int ** a, int n)
int findSpecial(int ** a, int n)
int findSpecialTrace(int ** a, int n)
int findLucky(int ** a, int n)
Here's the code I tried for this problem:
#include<stdio.h>
#include <stdlib.h>
int **readMatrix(int rows,int cols){ int i,j;
int **a=malloc (rows*sizeof(int *));
for (i=0;i<rows;i++){
*(a+i)=malloc (cols*sizeof (int));
}
for (i=0;i<rows;i++){
for (j=0;j<cols;j++){
scanf (" %d",(*(a+i)+j));
}
}
return a;
}
void displayMatrix (int **a,int rows, int cols){ int i,j;
for (i=0;i<rows;i++){
for (j=0;j<cols;j++){
printf ("%d ",*(*(a+i)+j));
}
printf ("
");
}
}
int findSymmetric (int **a,int n){
int **arr1=(int **) malloc (n*sizeof (int *));
int i,j;
for (i=0;i<n;i++){
*(arr1+i)=malloc (n*sizeof(int));
}
for (i=0;i<n;i++){
for (j=0;j<n;j++){
(*(*(arr1+i)+j))=(*(*(a+j)+i));
}
}
int isSymmetric=1;
for (i=0;i<n;i++){
for (j=0;j<n;j++){
if (*(*(arr1+i)+j)!=(*(*(a+i)+j))){
isSymmetric=0;
break;
}
} if (isSymmetric==0) break;
}
if (isSymmetric==1) return 1;
return 0;
}
int findIdentity(int **a,int n){ int i,j;
int isIdentity=1;
for (i=0;i<n;i++){
for (j=0;j<n;j++){
if (*(*(a+i)+j)!=1){
isIdentity=0;
break;
} if (isIdentity==0) break;
}
} if (isIdentity==1) return 1;
return 0;
}
int findMagic(int **a,int n){ int temp=2*n;
int arr2[temp];
int i,j;
for (i=0;i<n;i++){ int sum=0;
for (j=0;j<n;j++){
sum+=a[i][j];
} arr2[i]=sum;
}
int counter=n;
for (i=0;i<n;i++){ int sum1=0;
for (j=0;j<n;j++){
sum1+=a[j][i];
} arr2[counter]=sum1;
counter++;
}
int isMagic=1;
for (i=0;i<n;i++){
for (j=i+1;j<n;j++){
if (arr2[i]!=arr2[j]) {
isMagic=0;
break;
} if (isMagic==0) break;
}
}
if (isMagic==1) return 1;
return 0;
}
int findUpperTriangular(int ** a, int n){
int i,j,isUpperTriangular=1;
for (i=0;i<n;i++){
for (j=0;j<i;j++){
if (a[i][j]!=0){
isUpperTriangular=0;
break;
} if (isUpperTriangular==0) break;
}
} if (isUpperTriangular==1) return 1;
return 0;
}
int findLowerTriangular (int **a, int n){
int i,j,isLowerTriangular=1;
for (i=0;i<n;i++){
for (j=n-1;j>i;j--){
if (a[i][j]!=0){
isLowerTriangular=0;
break;
} if (isLowerTriangular==0) break;
}
} if (isLowerTriangular==1) return 1;
return 0;
}
int findEvenTrace (int **a,int n){
int i; int sum=0;
for (i=0;i<n;i++){
sum+=a[i][i];
}
if (sum%2==0) return 1;
return 0;
}
int findNonZero (int **a, int n){
int i,j;
int isNonzero=1;
for (i=0;i<n;i++){
for (j=0;j<n;j++){
if (a[i][j]==0){
isNonzero=0;
break;
} if (isNonzero==0) break;
}
}if (isNonzero==1) return 1;
return 0;
}
int findSpecial(int **a,int n){
int i,j;
int isSpecial=0;
for (i=0;i<n;i++){
for (j=0;j<n;j++){
if (a[i][j]==n){
isSpecial=1;
break;
} if (isSpecial==1) break;
}
} if (isSpecial==1) return 1;
return 0;
}
int findSpecialTrace(int **a, int n){
int i,sum=0,sum1=0;
for (i=0;i<n;i++) sum+=a[i][i];
for (i=n-1;i>=0;i--) sum1+=a[n-1-i][i];
if (sum==sum1) return 1;
return 0;
}
int findLucky (int **a,int n){
int isLucky=1;
int i,j;
for (i=0;i<n;i++){
for (j=0;j<n;j++){
if (a[i][j] % n!=0){
isLucky=0;
break;
}
} if (isLucky==0) break;
}if (isLucky==1) return 1;
return 0;
}
int main()
{ int n;
scanf ("%d",&n);
int **a =readMatrix (n,n);
displayMatrix (a,n,n);
if ((findSymmetric(a,n))==0) printf ("Matrix is not Symmetric
");
else if ((findSymmetric(a,n))==1) printf ("Matrix is Symmetric
");
if ((findIdentity(a,n))==0) printf ("Matrix is not Identity
");
else if ((findIdentity(a,n))==1) printf ("Matrix is Identity
");
if ((findMagic(a,n))==1) printf ("Matrix is Magic
");
else if ((findMagic(a,n))==0) printf ("Matrix is not Magic
");
if ((findUpperTriangular(a,n))==0) printf ("Matrix is not UpperTriangular
");
else if ((findUpperTriangular(a,n)==1)) printf ("Matrix is UpperTriangular
");
if ((findLowerTriangular(a,n))==0) printf ("Matrix is not LowerTriangular
");
else if ((findLowerTriangular(a,n)==1)) printf ("Matrix is LowerTriangular
");
if ((findEvenTrace(a,n))==0) printf ("Matrix is not EvenTrace
");
else if ((findEvenTrace(a,n))==1) printf ("Matrix is EvenTrace
");
if (findNonZero(a,n)==1) printf ("Matrix is NonZero
");
else if (findNonZero(a,n)==0) printf ("Matrix is Zero
");
if ((findSpecial(a,n)==1)) printf ("Matrix is Special
");
else if ((findSpecial(a,n)==0)) printf ("Matrix is not Special
");
if ((findSpecialTrace(a,n))==0) printf ("Matrix is not SpecialTrace
");
else if ((findSpecialTrace(a,n))==1) printf ("Matrix is SpecialTrace
");
if ((findLucky(a,n))==1) printf ("Matrix is Lucky
");
else if ((findLucky(a,n)==0)) printf ("Matrix is not Lucky
");
return 0;
}
On my coding platform, it says 94% testcases pass when I submit this code. The testcases are hidden. I have tried to debug this many times but couldn't find the error.
Can someone please debug this for me? Or is there some tool which can generate the testcases to check this program.
question from:
https://stackoverflow.com/questions/65870461/testcase-fail-for-finding-matrix-types