输入一个3行4列整形二维数组的值,输出最大元素的值及其下标。在主函数中给数组赋值,子函数max中求最大值
2个回答
展开全部
您好,下面的程序请参考,并在最后附有运行结果。
#include <stdlib.h>
#include <stdio.h>
void max(int a[][4], int k, int *x, int *y, int *z);
int main(void)
{
int a[3][4] = { {1, 2, 3, 4,},
{8, 12, 11, 10},
{5, 6, 7, 9}};
int m, n;
int num;
max(a, 3, &num, &m, &n);
printf("The max integer in array a is : a[%d][%d] = %d\n", m, n, num);
return 0;
}
void max(int a[][4], int k, int *x, int *y, int *z)
{
int i, j, m, n;
int num = a[0][0];
for(i = 0; i < k; i++){
for(j = 0; j < 4; j++){
if ( a[i][j] > num){
m = i;
n = j;
num = a[m][n];
}
}
}
*x = num;
*y = m;
*z = n;
}
feng@linux:~$ !g
gcc -Wall test.c
feng@linux:~$ ./a.out
The max integer in array a is : a[1][1] = 12
#include <stdlib.h>
#include <stdio.h>
void max(int a[][4], int k, int *x, int *y, int *z);
int main(void)
{
int a[3][4] = { {1, 2, 3, 4,},
{8, 12, 11, 10},
{5, 6, 7, 9}};
int m, n;
int num;
max(a, 3, &num, &m, &n);
printf("The max integer in array a is : a[%d][%d] = %d\n", m, n, num);
return 0;
}
void max(int a[][4], int k, int *x, int *y, int *z)
{
int i, j, m, n;
int num = a[0][0];
for(i = 0; i < k; i++){
for(j = 0; j < 4; j++){
if ( a[i][j] > num){
m = i;
n = j;
num = a[m][n];
}
}
}
*x = num;
*y = m;
*z = n;
}
feng@linux:~$ !g
gcc -Wall test.c
feng@linux:~$ ./a.out
The max integer in array a is : a[1][1] = 12
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询