仅供参考,谢谢!
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <limits.h>
bool alloc_mem(int **p, int length)
{
return (*p = (int *)malloc(length * sizeof(int))) != NULL ? true : false;
}
int main(int argc, char *argv[])
{
int M, N, max = INT_MIN, sum = 0;
do
{
scanf("%d%d", &M, &N);
} while (M <= 0 || N <= 0);
int *p = NULL;
_Bool result = alloc_mem(&p, M * N);
if (result == true)
puts("内存申请成功!");
else
{
puts("内存申请失败!");
exit(-1);
}
int i, j;
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
scanf("%d", &p[i * M + j]);
if (p[i * M + j] > max)
max = p[i * M + j];
sum += p[i * M + j];
}
}
printf("\n\n最大值:%d\n平均值:%.1f\n\n", max, 1.0 * sum / (M * N));
free(p);
p = NULL;
return 0;
}