28 lines
492 B
C++
28 lines
492 B
C++
|
// 接收一个数值数组,对数组中的数值求最大值并输出。
|
||
|
#include <iostream>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
int maxNum(int arr[], int size)
|
||
|
{
|
||
|
int max = arr[0];
|
||
|
|
||
|
while (--size)
|
||
|
{
|
||
|
if (arr[size] > max)
|
||
|
{
|
||
|
max = arr[size];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return max;
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
int arr[] = {18, 2, 3, 9, 5, 7}; // 完全初始化
|
||
|
int max = maxNum(arr, sizeof(arr) / sizeof(arr[0]));
|
||
|
cout << "最大值是: " << max << endl;
|
||
|
|
||
|
return 0;
|
||
|
}
|