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];
		}
		if (a[i] < min) {
			min = a[i];
		}
	}

	printf("max is %d\n", max);
	printf("min is %d\n", min);
	system("pause");
	return 0;
}

 

展开阅读全文

没有更多推荐了,返回首页