【发布时间】: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] 之类的东西(矩阵中的数据是连续的)。