求10个整数中的最大值
<1>比较两个相邻元素的大小,然后交换位置,直到最后一个数
#include<stdio.h>
#include<Windows.h>
int main()
{
int arr[10] = { 5, 3, 8, 21, 13, 1, 45, 88, 36, 16 };
int i,j,t
for (i = 0; i < 9; i++)
{
for (j = 0; j <= 9 - i; j++)
{
if (arr[j]>arr[j + 1])
{
t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}
system("pause");
return 0;
}
<2>借助函数实现
#include<stdio.h>
#include<assert.h>
#include<windows.h>
int GetMax(int *a, int num)
{
assert(a);
assert(num > 0);
int max = 0;
int i = 0;
for (; i < num; i++){
if (a[i]>max){
max = a[i];
}
}
return max;
}
int main()
{
int a[] = { 34, 21, 88, 56, 98, 101, 2, 74, 29, 45 };
int size = sizeof(a) / sizeof(a[0]);
int max = GetMax(a, size);
printf("max=%d\n", max);
system("pause");
return 0;
}