C语⾔-出数组元素中的最⼤值和最⼩值
#include<stdio.h>
#include<stdlib.h>
int main(void) {
int a[5] = { 7,3,20,8,9 };
int max = a[0];
for (int i = 0; i < 5; i++) {
if (a[i] > max) {
max = a[i];
}
}
printf("max is %d\n", max);
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
/*定义宏常量,代表数组元素的个数*/
#define LENGTH 5
int main(void) {
int a[LENGTH];
printf("Please enter %d integers:", LENGTH);
for (int i = 0; i < LENGTH; i++) {
scanf_s("%d", &a[i]);
}
int max = a[0], min = a[0];
for (int i = 0; i < LENGTH; i++) {
if (a[i] > max) {
max = a[i];
}c语言数组最大值最小值
if (a[i] < min) {
min = a[i];
}
}
printf("max is %d\n", max);
printf("min is %d\n", min);
system("pause");
return 0;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。