C language: Find the maximum value in a one-dimensional array

Idea: Create a variable to store the first element of the array 
           and use a for loop to traverse the array. If it is larger than the first element of the array, replace max with a larger number 

#include<stdio.h> 
int main(){ 
//  一维数组中求最大值
//	创建一个变量存储数组第一个元素 
//  用for循环以此遍历数组,如果比数组第一个元素大,就把max替换为大的数 
	int a[5]={100,50,36,121,655};
	int max=a[0];
	for(int i=0;i<5;i++){
		if(max<a[i])
		max=a[i];
	} 
	printf("数组最大值为:%d",max);
	return 0;
} 

 

Guess you like

Origin blog.csdn.net/weixin_63987141/article/details/129170337