C程序从m×中查找重复值;使用malloc()和free()的n矩阵

C程序从m×中查找重复值;使用malloc()和free()的n矩阵,c,arrays,memory,malloc,allocation,C,Arrays,Memory,Malloc,Allocation,所以我解决了这个问题,得到了这个输出 **但我期待这样的输出** 我的错误是什么? 我知道我的代码效率不高,我想知道如何使代码更紧凑。任何建议都会很有帮助,因为我还在学习这些东西 #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]; prin

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

**但我期待这样的输出**

我的错误是什么? 我知道我的代码效率不高,我想知道如何使代码更紧凑。任何建议都会很有帮助,因为我还在学习这些东西

#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);
}
#包括
#包括
#包括
int main()
{
int i,j,k,l,f=0,x=0,q=0,行,列,大小;
国际资源[100];
printf(“输入行:”);
scanf(“%d”行和第行);
printf(“输入列:”);
scanf(“%d”列和列);
大小=行*列;
int**arr;
arr=(int**)malloc(row*sizeof(int));

对于(i=0;i这更接近原始代码。
跳过在
arr
开头附近找到的重复项,这样这些重复项只打印一次。
使用
first
标志打印第一对匹配值的第一个索引。
pending
存储一组要延迟打印的索引。这允许使用前导的
打印最后一组索引,同时使用倾斜的逗号打印任何其他索引

#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);
}
#包括
#包括
整数扫描整数(整数*值){
int结果=0;
结果=scanf(“%d”,value);//不需要和号。值为int*
如果(0==result){//无法从输入中分析int
而('\n'!=(result=getchar()){//一直读到换行
如果(EOF==结果){
fprintf(标准“EOF”);
退出(退出失败);
}
}
返回0;
}
返回1;
}
int main()
{
int-eachrow=0;
int-eachcol=0;
int行=0;
int列=0;
int结果=0;
做{
printf(“输入行:”);
fflush(stdout);
结果=扫描输入(&row);
}而(0==结果);
做{
printf(“输入列:”);
fflush(stdout);
结果=扫描输入(&列);
}而(0==结果);
int**arr=NULL;
if(NULL==(arr=malloc(sizeof*arr*row))){
fprintf(标准,“malloc arr问题”);
退出(退出失败);
}
对于(每个箭头=0;每个箭头<行;++每个箭头){
if(NULL==(arr[eachrow]=malloc(sizeof**arr*列))){
fprintf(stderr,“malloc arr[]问题\n”);
while(每次){
每一个--;
免费(arr[每小时]);
}
免费(arr);
退出(退出失败);
}
}
对于(每个箭头=0;每个箭头<行;++每个箭头){
用于(每一氯=0;每一氯<列;++每一氯){
做{
printf(“输入(%d%d)的值):”,每个箭头,每个图标);
fflush(stdout);
结果=扫描点(&arr[eachrow][eachcol]);
}而(0==结果);
}
}
对于(每个箭头=0;每个箭头<行;++每个箭头){
用于(每一氯=0;每一氯<列;++每一氯){
printf(“位于(%d%d)处的值):%d\n”,每一个箭头,每一个指针,每一个指针,每一个指针[eachrow][eachcol]);
}
}
字符挂起[30]=“”;
int checkrow=0;
int checkcol=0;
int skip=0;
int first=0;
int title=0;
内线=1;
对于(每个箭头=0;每个箭头<行;++每个箭头){
用于(每一氯=0;每一氯<列;++每一氯){
first=0;//将需要打印行的第一部分
挂起[0]=0;//没有挂起的内容
for(checkrow=0;checkroweachrow*column+eachcol){//后续匹配
如果(!标题){
标题=1;
printf(“\n重复值为:\n”);
}
如果(!first){//需要打印行的第一部分
first=1;//已打印
printf(“%d.%d位置(%d,%d)”
,行,arr[eachrow][eachcol],eachrow,eachcol);
}
if(挂起[0]){//打印挂起索引
printf(“,%s”,待定);
}
sprintf(挂起,(%d,%d)”,检查行,检查列);//将索引复制到挂起
}
else{//当前或以前的匹配
//忽略当前匹配(检查行*列+检查列==eachrow*列+eachcol)
如果(checkrow*column+checkcol
请不要发布文本图像。只需发布文本。因此,您希望的输出具有
(0,1)、(2,3)和(3,1)
-在哪里打印?是否打印
之间的
(%d,%d)
代码中的某个地方?为什么会发生这种情况?您希望代码打印文本
1.
的原因是什么