Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

arrays - C program to find the duplicate values from an m×n matrix using malloc() and free()

SO i was solving this problem and i get this output

enter image description here

**But i am expecting output like this **

enter image description here

what is my mistake here? I know i don't code efficiently and i want to know how can i make this code more compact any suggestions will be helpful as i am still learning these things.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
    int i,j,k,l,f=0,x=0,q=0,row,column,size;
    int res[100];

    printf("Enter the row: ");
    scanf("%d",&row);
    printf("Enter the column: ");
    scanf("%d",&column);
    size= row*column;


    int **arr;
    arr=(int**)malloc(row * sizeof(int));
    for(i=0;i<row;i++)
    {
        arr[i] = (int*)malloc(column*sizeof(int));
    }

    for(i=0;i<row;i++)
    {
        for (j=0;j<column;j++)
        {
            printf("Enter the value at row %d and column %d : ",i,j);
            scanf("%d",(*(arr+i)+j));

        }
    }
    for(i=0;i<row;i++)
    {
        for (j=0;j<column;j++)
        {
            printf("
The value at row %d and column %d : %d",i,j,*(*(arr+i)+j));


        }
    }
    printf("
The duplicate value(s) are:
");
    for(i=0; i<row; i++)
    {
        for(j=0; j<column; j++)
        {

            for(k=0; k<row; k++)
            {
                for(l=0; l<column; l++)
                {
                    if(*(*(arr+i)+j)== *(*(arr+k)+l))
                    {
                        f=f+1;
                    }
                    if(f>1)
                    { 
                        printf("%d in positions (%d,%d)",*(*(arr+i)+j),k,l);
                    }
                }
            }f=0;
        }
    }


    free(arr);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is closer to the original code.
skip duplicates that are found closer to the beginning of arr so those are only printed once.
Use a first flag to print the first indices of the first pair of matching values.
pending stores a set of indices to defer printing. This allows printing the last set of indices with a leading and while printing any other indices with a leanding comma.

#include<stdio.h>
#include<stdlib.h>

int scanint ( int *value) {
    int result = 0;
    result = scanf ( "%d", value);//ampersand not needed. value is int *
    if ( 0 == result) {//could not parse an int from input
        while ( '
' != ( result = getchar ( ))) {//read until newline
            if ( EOF == result) {
                fprintf ( stderr, "EOF
");
                exit ( EXIT_FAILURE);
            }
        }
        return 0;
    }
    return 1;
}

int main()
{
    int eachrow = 0;
    int eachcol = 0;
    int row = 0;
    int column = 0;
    int result = 0;

    do {
        printf ( "Enter the row: ");
        fflush ( stdout);
        result = scanint ( &row);
    } while ( 0 == result);
    do {
        printf ( "Enter the column: ");
        fflush ( stdout);
        result = scanint ( &column);
    } while ( 0 == result);

    int **arr = NULL;

    if ( NULL == ( arr = malloc ( sizeof *arr * row))) {
        fprintf ( stderr, "malloc arr problem
");
        exit ( EXIT_FAILURE);
    }
    for ( eachrow = 0; eachrow < row; ++eachrow) {
        if ( NULL == ( arr[eachrow] = malloc ( sizeof **arr * column))) {
            fprintf ( stderr, "malloc arr[] problem
");
            while ( eachrow) {
                eachrow--;
                free ( arr[eachrow]);
            }
            free ( arr);
            exit ( EXIT_FAILURE);
        }
    }

    for ( eachrow = 0; eachrow < row; ++eachrow) {
        for ( eachcol = 0; eachcol < column; ++eachcol) {
            do {
                printf ( "Enter the value for (%d %d) : ", eachrow, eachcol);
                fflush ( stdout);
                result = scanint ( &arr[eachrow][eachcol]);
            } while ( 0 == result);
        }
    }
    for ( eachrow = 0; eachrow < row; ++eachrow) {
        for ( eachcol = 0; eachcol < column; ++eachcol) {
            printf ( "The value at (%d %d) : %d
", eachrow, eachcol, arr[eachrow][eachcol]);
        }
    }

    char pending[30] = "";
    int checkrow = 0;
    int checkcol = 0;
    int skip = 0;
    int first = 0;
    int title = 0;
    int line = 1;

    for ( eachrow = 0; eachrow < row; ++eachrow) {
        for ( eachcol = 0; eachcol < column; ++eachcol) {
            first = 0;//will need to print the first part of line
            pending[0] = 0;//nothing pending
            for ( checkrow = 0; checkrow < row; ++checkrow) {
                skip = 0;//do not skip
                for ( checkcol = 0; checkcol < column; ++checkcol) {
                    if ( arr[eachrow][eachcol] == arr[checkrow][checkcol]) {//match
                        if ( checkrow * column + checkcol > eachrow * column + eachcol) {//subsequent match
                            if ( ! title) {
                                title = 1;
                                printf ( "
The duplicate value(s) are:
");
                            }
                            if ( ! first) {//need to print first part of line
                                first = 1;//printed
                                printf ( "%d.  %d in positions (%d,%d)"
                                , line, arr[eachrow][eachcol], eachrow, eachcol);
                            }
                            if ( pending[0]) {//print pending indices
                                printf ( ", %s", pending);
                            }
                            sprintf ( pending, "(%d,%d)", checkrow, checkcol);//copy indices to pending
                        }
                        else {//current or previous match
                            //ignore current match    ( checkrow * column + checkcol == eachrow * column + eachcol)
                            if ( checkrow * column + checkcol < eachrow * column + eachcol) {//previous match
                                skip = 1;//need to skip checkcol and checkrow
                                break;//out of for checkcol loop
                            }
                        }
                    }
                }
                if ( skip) {
                    break;//out of checkrow loop
                }
            }
            if ( first) {//there were matches
                printf ( " and %s
", pending);//print  and  with pending indices
            }
        }
    }
    if ( ! title) {
        printf ( "
No duplicates
");
    }

    for ( eachrow = 0; eachrow < row; ++eachrow) {
        free ( arr[eachrow]);
    }
    free(arr);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...