【问题标题】:Functions with double pointer arrays - find the max in a matrix具有双指针数组的函数 - 在矩阵中找到最大值
【发布时间】:2015-09-06 13:15:27
【问题描述】:

这是一个问题 - 编写程序,使用函数查找矩阵中的最大元素。

功能说明:

int findMax(int **a, int m, int n) 第一个参数对应于指向矩阵的指针。 第二个参数对应于矩阵中的行数。 第三个参数对应矩阵的列数。

以下是我的代码,虽然没有编译错误,但我不知道我哪里出错了。请提前帮助和感谢!

#include<stdio.h>
#include<malloc.h>
int findMax(int **a, int m, int n) { 
  int c,d, maximum=a[0][0];
  for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         if ( a[c][d] > maximum )
            maximum = a[c][d];
      }
   } return maximum;
}

int main()
{
   int m, n, c, d, maximum;
   int **a = (int **)malloc(10 * sizeof(int *));
   scanf("%d",&m);
  printf("Enter the number of columns in the matrix\n");
   scanf("%d",&n);
   printf("Enter the elements in the matrix\n");

   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         scanf("%d",&a[c][d]);
      }
   }
 printf("The matrix is\n");

   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         printf("%d ",a[c][d]);
      }
     printf("\n");
   } 

   maximum = findMax(a,m,n);

   printf("The maximum element in matrix is %d\n", maximum);

   return 0;
}

【问题讨论】:

    标签: c arrays matrix


    【解决方案1】:

    您为a 分配了内存,但没有为a 的行分配内存。

    for( c = 0 ; c < m ; c++ )
    {
       // Add this
       a[c] = malloc(n*sizeof(int));
    
       for( d = 0 ; d < n ; d++ )
       {
          scanf("%d",&a[c][d]);
       }
    }
    

    另外,请确保添加代码以释放内存。

    for( c = 0 ; c < m ; c++ )
    {
       free(a[c]);
    }
    free(a);
    

    进一步改进:

    不要使用硬编码数字10a 分配内存,而是使用m。使用:

    int m, n, c, d, maximum;
    int **a = NULL
    
    scanf("%d",&m);
    printf("Enter the number of columns in the matrix\n");
    scanf("%d",&n);
    printf("Enter the elements in the matrix\n");
    
    a = malloc(m * sizeof(*a));
    

    不要对malloc 返回的值使用显式强制转换。见Specifically, what's dangerous about casting the result of malloc?

    【讨论】:

    • 这真的很有帮助!非常感谢!
    • @user3193036,很高兴我能帮上忙。
    【解决方案2】:
    #include<stdio.h>
    #include<malloc.h>
    int findMax(int **a,int m,int n);
    int max;
    int main()
    {
      int **b,r,c,i,j,y;
      printf("Enter the number of rows in the matrix\n");
      scanf("%d",&r);
      printf("Enter the number of columns in the matrix\n");
      scanf("%d",&c);
      printf("Enter the elements in the matrix\n");
      b=malloc(sizeof(int*)*r);
      for(i=0;i<r;i++)
      {
        b[i]=malloc(sizeof(int*)*c);
        for(j=0;j<c;j++)
        {
          scanf("%d",&b[i][j]);
             }
      }
        printf("The matrix is\n");
          for(i=0;i<r;i++)
          {
            for(j=0;j<c;j++)
            {
              printf("%d ",b[i][j]);
            }
            printf("\n");
          }
        y=findMax(b,r,c);
          printf("The maximum element in the matrix is %d\n",y);
      return(0);
    }
    int findMax(int **a,int m,int n)
    {
      int i,j;
      for(i=0;i<m;i++)
      {
        for(j=0;j<n;j++)
        {
          if(a[i][j]>max)
            max=a[i][j];
        }
      }
        return(max);
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多