Solved programming problem

Replace all Duplicate element with (-1) from 2D array in C.

Source code

#include <stdio.h>

int main()

{

    int array[100][100];

    int i,j,r,c;

    int p=0,q=0;

    printf("Please Enter how many row and column you want to use: ");

    scanf("%d %d",&r,&c);

    printf("Please Enter the elements of the array:
");

    for (i=0;i<r;i++)

    {

        for (j=0;j<c;j++)

        {

            scanf("%d",&array[i][j]);

        }

    }

     for(i=0;i<=r;i++)

     {

         for(j=0;j<=c;j++)

         {

             for(p=i+1;p<=r;p++)

             {

                 for(q=0;q<=c;q++)

                 {

                     if (array[i][j]==array[p][q])

                        array[p][q]=-1;

                 }

             }

         }

     }

    printf("
New matrix:
");

    for(i=0;i<r;i++)

    {

        for(j=0;j<c;j++)

        {

            printf("%d ",array[i][j]);

        }

        printf("
");

    }

    return 0;

}

Output

Please Enter how many row and column you want to use: 3 3

Please Enter the elements of the array:

1 2 3

1 2 1

1 2 1

New matrix:

1 2 3

-1 -1 -1

-1 -1 -1

Join the discussion

Add your comment