1. 程式人生 > >尋找第二大數並輸出

尋找第二大數並輸出

#include <stdio.h>

int getMax2 (int Array[], int len)
{
	if (Array == NULL)
		return -1;
	if (len < 1)
		return -1;
		
	int i;
	int max1 = 0;
	int max2 = 0;
	
	for (i = 0; i < len; i++)
	{
		if (max1 < Array[i])
		{	
			max2 = max1;
			max1 = Array[i];
		}
		if (max2 < Array[i] && Array[i] < max1)
		{	
			max2 = Array[i];
		}
	}
	
	return max2;
}

int main()
{
	int Array[] = {0,1,2,3,4,5,6,7,8,9};
	int len = sizeof(Array) / sizeof(int);
	
	printf ("max2 = %d\n", getMax2(Array,len));
	
	return 0;
}

![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20181124175022897.png)