#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 :(得分:2)
更改
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);
}
希望,这会有所帮助。 :)
答案 1 :(得分:1)
您不能将2d数组(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;
}
答案 2 :(得分:1)
此代码段适合您。
let x : Int! = 1
switch x {
case nil:
break // x is nil
case 1?:
break // x is 1
default:
break // x is some other number
}
然后您的函数调用 max = findMax(a,r,c); 将起作用。
答案 3 :(得分:0)
#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;
}