I need to delete (not skip while printing) the rows and columns of a matrix that appear more than once in program, and I should print only first row from the top that appears more than once or the first column from the left that appears more than once.
Example input:
1 2 3 2
4 5 6 5
1 2 3 2
7 8 9 8
After deleting:
1 2 3
4 5 6
7 8 9
Here's my code:
#include <stdio.h>
int main() {
int i, j, m, n,row,col, mat[200][200];
scanf("%d %d", &m, &n);
row = m; col = n;
for (i = 0; i < m; i )
for (j = 0; j < m; j )
scanf("%d", &mat[i][j]);
for (i = 0; i < m; i )
for (j = 0; j < m; j ) {
if (mat[i][j] == mat[i ][j ])
row--;
if (mat[j][i] == mat[j ][i ])
col--;
}
for (i = 0; i < row; i ) {
for (j = 0; j < col; j ) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
Do you have any idea how to make the algorithm work for this task? Mine has mistakes.
CodePudding user response:
Would you please try the following:
#include <stdio.h>
#define ROWS 200
#define COLS 200
#define TRUE 1
#define FALSE 0
/*
* delete k'th row from the m x n matrix
*/
void deleterow(int mat[ROWS][COLS], int m, int n, int k)
{
int i, j;
for (i = k; i < m - 1; i ) {
for (j = 0; j < n; j ) {
mat[i][j] = mat[i 1][j];
}
}
}
/*
* delete k'th columns from the m x n matrix
*/
void deletecol(int mat[ROWS][COLS], int m, int n, int k)
{
int i, j;
for (j = k; j < n - 1; j ) {
for (i = 0; i < m; i ) {
mat[i][j] = mat[i][j 1];
}
}
}
int main() {
int i, j, m, n,row,col, mat[ROWS][COLS];
int iref, jref; // reference indexes to compare
int match; // flag to show if the row/col duplicates
// read input matrix
scanf("%d %d", &m, &n);
row = m; col = n;
for (i = 0; i < m; i )
for (j = 0; j < n; j )
scanf("%d", &mat[i][j]);
// examine row by row
for (iref = 0; iref < m; iref ) {
// compare rows below iref and remove the row if duplicates
for (i = iref 1; i < m; i ) {
match = TRUE;
for (j = 0; j < n; j ) {
if (mat[i][j] != mat[iref][j]) {
match = FALSE;
break;
}
}
if (match) {
deleterow(mat, m, n, i);
m--;
}
}
}
// examine column by column
for (jref = 0; jref < n; jref ) {
// compare columns more right than jref and remove the col if duplicates
for (j = jref 1; j < n; j ) {
match = TRUE;
for (i = 0; i < m; i ) {
if (mat[i][j] != mat[i][jref]) {
match = FALSE;
break;
}
}
if (match) {
deletecol(mat, m, n, j);
n--;
}
}
}
// see the result
for (i = 0; i < m; i ) {
for (j = 0; j < n; j ) {
printf("-%s", mat[i][j], j == n - 1 ? "\n" : " ");
}
}
return 0;
}
Output with the provided example:
1 2 3
4 5 6
7 8 9
[Explanation]
As for the operations of rows:
- First, focus on the top row as a "reference". The row is indexed by
the variable
iref
which is assigned to 0 at first. - Then compare the remaining rows with the reference, changing
the row index
i
from iref 1 (just below the reference row) to n-1 (the bottom row). - If a row duplicates with the reference, remove the row with the
deleterow()
function and decrement the row sizem
by one. The modification ofm
affects thefor
loops which compare the loop variables withm
, meaning the matrix size is updated immediately. This is a preferable nature of the for loop (IMHO). - If the comparizon reaches the bottom row, increment
iref
and repeat the comparisons again. - Finally every row has been compared to each other and the duplicates have been deleted.
Then perform the similar operations with columns.