Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to delete identical rows and columns from matrix without deleting the first identical row/column. For example, if second, third, and fourth row are identical, third and fourth row should be deleted and second row should be kept. The same goes for columns. I tried to solve this task, but, I have some mistake in my code, and I don't know if my approach is correct. I hope that you could help me.
#if 0
Sample Input:
1 2 1 2 3
1 2 1 2 3
1 2 1 2 3
   
Sample Output:
1 2 3
#endif


What I have tried:

I have tried to do something like this, but I am not sure since I am a beginner with this.
C++
#include <stdio.h>

int main()
{
    int M, N, m[100][100], i, j, a = 0, k, b = 0;

    printf("Enter dimensions: ");
    scanf("%d %d", &M, &N);

    for(i = 0; i < M; i++)
    {
        for(j = 0; j < N; j++)
        {
            scanf("%d", &m[i][j]);
        }
    }

    for(i = 0; i < M; i++)
    {
        for(j = 0; j < N; j++)
        {
            if(m[i][j] != m[a][j])
            {
                a++;
                continue;
            }

            if(m[i][j] != m[b][j])
            {
                b++;
                continue;
            }

            if(i == M)
            {
                for(i = 0; i < M; i++)
                {
                    m[i][k] = m[i][k + 1];
                }
                N--;
            }

        }
        if(j == N)
        {
            for(k = 0; k < N; k++)
            {
                m[i][k] = m[i + 1][k];
            }
            M--;
        }

    }

    printf("New matrix: \n");
    for(i = 0; i < M; i++)
    {
        for(j = 0; j < N; j++)
        {
            printf("%d", m[i][j]);
        }
        printf("\n");
    }
}
Posted
Updated 15-Feb-21 17:41pm
v2

Maybe you'll find someone who's willing to inspect your code manually, but it usually doesn't work that way.

I would suggest the following. I don't know how much of this you've done, but definitely not #4:

1. Convince yourself that the result will be the same, regardless of whether you delete identical rows first, and then columns, or vice versa.
2. Write down your algorithm in pseudo-code, using plain language, before converting it to C.
3. Take an example like the one you gave, and run it through your code manually to see where the problem is. This can be tedious because you have to look at your code very carefully instead of just assuming that a line does what you intended.
4. If you still haven't found the problem, learn how to use a debugger. This allows you to execute your code line by line so that you can find where its behaviour differs from what you expected. Learning to use a debugger will save you a lot of time and is critical for all but the most trivial software. There's no alternative when you have thousands of lines of software or more.
 
Share this answer
 
v3
Comments
CPallini 16-Feb-21 2:15am    
5.
Quote:
I don't know if my approach is correct.

I think your approach is wrong.
In first, I would try to combine row and column search in same piece of code. I would do a code to check rows, then a code to check columns.
A new row is identical to a previous row if all cells are identical.
Advice: Solve the problem by hand and pay attention to changes in matrix each time you do a delete.
Then for checking purpose, print the matrix after each delete until it match your manual solve.
Quote:
I tried to solve this task, but, I have some mistake in my code

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
CPallini 16-Feb-21 2:15am    
5.
Patrice T 16-Feb-21 2:19am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900