【问题标题】:How to find the maximum element in the matrix using function?如何使用函数找到矩阵中的最大元素?
【发布时间】:2015-04-18 14:58:30
【问题描述】:
 #include<stdio.h>
 int findMax(int **a,int r,int c);
 int main()
 {
  int a[10][10],i,j,max,r,c;
  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");
  for(i=1;i<=r;i++)
  {  for(j=1;j<=c;j++)
     scanf("%d",&[i][j]);
  }
  printf("The matrix is\n");
  for(i=1;i<=r;i++)
  { for(j=1;j<=c;j++)
    scanf("%d",&a[i][j]);
  }printf("\n");}
  max=findMax((int **)a,r,c);
  printf("The maximum elements in the matrix is %d\n",max);
  return 0;
  }
  int findMax(int **a,int r,int c)
  { 
    int t,i,j;
    t=a[1][1];
    for(i=1;i<r;i++)
    { for(j=1;j<c;j++)
    { if(a[i][j]>t)
        t=a[i][j];
    }
    }
   return (t);
  }

这里附上我的编码,我需要使用函数找到矩阵中存在的最大元素,我正在编码,调用函数没有执行,我不知道为什么,帮我弄清楚。

【问题讨论】:

  • 矩阵的第一个元素是 [0][0] 而不是 [1][1] 最后一个是 [MAXRAW-1][MAXCOL-1]。在第二个中,您必须使用 printf 来显示元素的内容,而不是 scanf!我认为最好向函数发送一个指向矩阵第一个元素的指针,并在函数内部使用 a[i*c+j] 之类的东西(矩阵中的数据是连续的)。

标签: c function matrix


【解决方案1】:

改变

int findMax(int **a,int r,int c)

int findMax(int (*a)[10],int r,int c)

还有,

for(i=1;i<r;i++)
{ 
    for(j=1;j<c;j++)
    { 
        if(a[i][j]>t)
            t=a[i][j];
    }
} 

for(i=1;i<=r;i++)
{ 
    for(j=1;j<=c;j++)
    { 
        if(a[i][j]>t)
            t=a[i][j];
    }
} 

编辑:

我想,你的代码应该是这样的:

#include<stdio.h>

int findMax(int (*a)[10],int r,int c);

int main()
{
    int a[10][10],i,j,mx,r,c;
    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");

    for(i=1;i<=r;i++)
    {
        for(j=1;j<=c;j++)
            scanf("%d",&a[i][j]);
    }
    printf("The matrix is\n");
    for(i=1;i<=r;i++)
    {
        for(j=1;j<=c;j++)
            printf("%d ",a[i][j]);
        printf("\n");
    }
    printf("\n");
    mx=findMax(a,r,c);
    printf("The maximum elements in the matrix is %d\n",mx);
    return 0;
}

int findMax(int (*a)[10],int r,int c)
{
    int t,i,j;
    t=a[1][1];
    for(i=1;i<=r;i++)
    {
        for(j=1;j<=c;j++)
        {
            if(a[i][j]>t)
                t=a[i][j];
        }
    }
    return (t);
}

希望,它会有所帮助。 :)

【讨论】:

  • if change int findMax(int (*a)[10],int r,int c) this,怎么调用...在main函数里面怎么调用...
  • 我得到了答案。谢谢!!!但它在门户中提交时显示编译时间错误......对此有任何解决方案吗???
  • 我应该更改函数定义。它必须使用函数定义为“int findMax (int **a, int m, int n)”。请帮我解决这个问题
【解决方案2】:

您不能将二维数组 (int a[10][10]) 传递给指向指针的指针。

int findMax(int **a, int r, int c)

应该是

int findMax(int (*a)[10], int r, int c) /* Pointer to array of 10 ints */ 

如果您事先不知道 2d 数组的大小(并注意数组以 0 为基数),请使用堆:

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

int findMax(int *a, int r, int c);

int main(void)
{
    int *a, r, c, i, j, max;
    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);
    a = malloc(r * c * sizeof(*a));
    if (a == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    printf("Enter the elements in the matrix\n");
    for(i = 0; i < r; i++) {
        for(j = 0; j < c; j++)
            scanf("%d", &a[i * c + j]);
    }
    printf("The matrix is\n");
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++)
            printf("%d", a[i * c + j]);
    }
    printf("\n");
    max = findMax(a, r, c);
    printf("The maximum elements in the matrix is %d\n", max);
    free(a);
    return 0;
}

int findMax(int *a,int r, int c)
{ 
    int t, i, j;

    t = a[0];
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            if(a[i * c + j] > t)
                t = a[i * c + j];
        }
    }
    return t;
}

如果你在 C99 下,也可以使用变长数组:

#include <stdio.h>

int findMax(int r, int c, int (*a)[]);

int main(void)
{
    int i, j, max, r, c;
    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);
    int a[r][c];
    printf("Enter the elements in the matrix\n");
    for(i = 0; i < r; i++) {
        for(j = 0; j < c; j++)
            scanf("%d", &a[i][j]);
    }
    printf("The matrix is\n");
    for(i = 0; i < r; i++)  {
        for(j = 0; j < c; j++)
            printf("%d", a[i][j]);
    }
    printf("\n");
    max = findMax(r, c, a);
    printf("The maximum elements in the matrix is %d\n", max);
    return 0;
}

int findMax(int r, int c, int (*a)[c])
{ 
    int t, i, j;

    t = a[0][0];
    for(i = 0; i < r; i++) {
        for(j = 0; j < c; j++) {
            if(a[i][j] > t)
                t = a[i][j];
        }
    }
    return t;
}

【讨论】:

    【解决方案3】:

    此代码 sn-p 适合您。

    printf("Enter the elements in the matrix\n");
       for(i=0;i<m;i++){
          a[i]=malloc(sizeof(int)*c);
       for(j=0;j<n;j++)
          scanf("%d", &a[i][j]);
     }
    

    然后您的函数调用 ma​​x=findMax(a,r,c); 将起作用。

    【讨论】:

      【解决方案4】:
      #include<stdio.h>
      #include<stdlib.h>
      int findMax(int **a,int m,int n)
      {
          int i,j,larg;
          larg=a[0][0];
          for(i=0;i<m;i++)
          {
              for(j=0;j<n;j++)
              {
                  if(larg<a[i][j])
                      larg=a[i][j];
              }
          }
          return larg;
      }
      int main()
      {
          int m,n,i,j,larg;
          printf("Enter the number of rows in the matrix\n");
          scanf("%d",&m);
          printf("Enter the number of columns in the matrix\n");
          scanf("%d",&n);
          printf("Enter the elements in the matrix\n");
          int **a=(int**)malloc(m*sizeof(int *));
          for(i=0;i<m;i++)
              a[i]=(int *)malloc(n*sizeof(int));
          for(i=0;i<m;i++)
              for(j=0;j<n;j++)
                  scanf("%d\n",&a[i][j]);
          larg=findMax(a,m,n);
          printf("The matrix is\n");
          for(i=0;i<m;i++)
          {
              for(j=0;j<n;j++)
              {
                  printf("%d ",a[i][j]);
              }
              printf("\n");
          }
          printf("The maximum element in the matrix is %d\n",larg);
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2014-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-24
        • 2013-05-03
        • 2023-01-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多