判斷最大最小值
#include "stdafx.h"
int main()
{
const int a = 3;
const int b = 3;
int arr[a][b] = {0};
for (int i = 0; i <= a - 1; i++)
{
for (int j = 0; j <= b - 1; j++)
{
scanf_s("%d", &arr[i][j]);
}
}
int sum = 0, pjz = 0;//總和與平均值
int n = sizeof(arr) / sizeof(int);//數組一共有幾個數
for (int i = 0; i <= a - 1; i++)
{
for (int j = 0; j <= b - 1; j++)
{
sum = arr[i][j] + sum;//計算總和
}
}
printf("平均值為:%d\n", sum / n);//輸出平均值
int max = arr[0][0];//最大值
int min = arr[0][0];//最小值
for (int i = 0; i <= a - 1; i++)
{
for (int j = 0; j <= b - 1; j++)
{
if (arr[i][j] > max)
{
max = arr[i][j];//判斷最大值
}
if (arr[i][j] < min)
{
min = arr[i][j];//判斷最小值
}
}
}
printf("最大值為:%d,最小值為:%d", max, min);//輸出最大最小值
return 0;
}
判斷最大最小值