求10 个整数中最大值。
给定一个数组a[10] = { 1,3,4,7,8,2,6,9,10,5 };
在数组中找寻最大值。创建一个int型的变量,将数组中的第一个值赋给他,作为最大值。
使用循环遍历一遍数组,在遍历的过程中,如果数组中有数值比max当前的值要大,就将数组中的值赋给max,直到数组遍历结束。
#include <stdio.h>
int main()
{
//创建一个包含十个整型变量的数组,并将第一个暂时定为最大值
int a[10] = { 1,3,4,7,8,2,6,9,10,5 };
int max = a[0];
//开始遍历数组
for (int i = 1;i < 10;++i)
{
//如果当前值比max值大,就将当前值赋给max
if (max < a[i])
{
max = a[i];
}
}
printf("最大值为:%d\n", max);
system("pause");
return 0;
}
实验结果: