1. Engineering
  2. Computer Science
  3. im having very hard time figuring out how i would...

Question: im having very hard time figuring out how i would...

Question details

I'm having very hard time figuring out how I would determine if an array with the same number of rows and columns has all different values or not (so whether if its a distinct matrix or not). I also need to be able to sort the same array using row wise approach and calculate and print its transpose. All of this should be done in C. Heres my code so far, the bolded code is me trying to determine if it has different values or not, its not working. Please help me determine if an array with the same number of rows and columns has all different values or not, sort the array using row wise approach and calculate its transpose:

#include <stdio.h>
#define MAX 100

void main() {
int mat[MAX][MAX];
int i, j, m, n;
int size;
int rowsum, columnsum, diagonalsum;

int k;


int magic = 0;

printf("Enter the # of rows and columns of the square matrix (must be same number):\n");
scanf("%d %d", &m, &n);
if(m==n) {
printf("Enter the elements of matrix \n");
for(i=0; i<m; i++) {
for(j=0; j<n; j++) {
scanf("%d", &mat[i][j]);
}
}

printf("\n\nMATRIX is\n");
for(i=0; i<m; i++) {
for(j=0; j<n; j++) {
printf("%3d\t", mat[i][j]);
}
printf("\n");
}

// calculate diagonal sum
diagonalsum = 0;
for(i=0; i<m; i++) {
for(j=0; j<n; j++) {
if(i==j) {
diagonalsum = diagonalsum + mat[i][j];
}
}
}

// calculate row sum
for(i=0; i<m; i++) {
rowsum = 0;
for(j=0; j<n; j++) {
rowsum = rowsum + mat[i][j];
}
if(rowsum != diagonalsum) {
printf("\nGiven matrix is not a magic square");
return;
}
}

// calculate column sum
for(i=0; i<m; i++) {
columnsum = 0;
for(j=0; j<n; j++) {
columnsum = columnsum + mat[j][i];
}
if(columnsum != diagonalsum) {
printf("\nGiven matrix is not a magic square");
return;
}
}

printf("\nGiven matrix is a magic square matrix");
} else {
printf("\n\nPlease enter the square matrix order(m=n) \n\n");
}

int all_distinct = 1;

int count, l;

    for (i = 0; i < mat[i]; i++) {
        for(j = 0; j < mat[j]; j++) {
            count = 0;
            for(k = 0; k < mat[i]; k++) {
                for(l = 0; l < mat[j]; l++) {
                    if(mat[k][l] == mat[i][j]) {
                        count++;
                    }
                }
            }
            if(count != 1) {
                all_distinct = 0;
            }
        }
    }
    if(all_distinct) {
        printf("All elements in the array are distinct\n");
    } else {
        printf("All elements in the array are not distinct\n");
    }
    return 0;



}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution