C程序使用malloc()和free()从m×n矩阵中查找重复值 [英] C program to find the duplicate values from an m×n matrix using malloc() and free()

查看:109
本文介绍了C程序使用malloc()和free()从m×n矩阵中查找重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在解决这个问题,我得到了这个输出

**但是我期望这样的输出**

**But i am expecting output like this **

这里的错误是什么?
我知道我的编码效率不高,我想知道如何制作

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("\nThe value at row %d and column %d : %d",i,j,*(*(arr+i)+j));


        }
    }
    printf("\nThe duplicate value(s) are:\n");
    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);
}


推荐答案

代码。

skip 重复项的位置更接近 arr 的开头,因此仅

使用 first 标志打印第一对匹配值的第一个索引。

pending 存储一组索引以延迟打印。这样可以打印最后一组以开头的索引,同时以倾斜逗号打印任何其他索引。

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 ( '\n' != ( result = getchar ( ))) {//read until newline
            if ( EOF == result) {
                fprintf ( stderr, "EOF\n");
                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\n");
        exit ( EXIT_FAILURE);
    }
    for ( eachrow = 0; eachrow < row; ++eachrow) {
        if ( NULL == ( arr[eachrow] = malloc ( sizeof **arr * column))) {
            fprintf ( stderr, "malloc arr[] problem\n");
            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\n", 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 ( "\nThe duplicate value(s) are:\n");
                            }
                            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\n", pending);//print  and  with pending indices
            }
        }
    }
    if ( ! title) {
        printf ( "\nNo duplicates\n");
    }

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

这篇关于C程序使用malloc()和free()从m×n矩阵中查找重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆