提示:“運行成功,輸出錯誤”,這是哪里錯了?
#include <iostream>
using namespace std;
/**
? *函數功能:返回a和b的最大值
? *a和b是兩個整數
? */
int getMax(int a, int b)
{
? ? return a > b ? a : b;
}
/**
? * 函數功能:返回數組中的最大值
? * arr:整型數組
? * count:數組長度
? * 該函數是對上面函數的重載
? */
int getMax(int *arr,int count)
{
? ? //定義一個變量并獲取數組的第一個元素
? ? int maxNum = arr[0];
for(int i = 1; i < count; i++)
{
? ? ? ? //比較變量與下一個元素的大小
if(maxNum < arr[i])
{
? ? ? ? ? ? //如果數組中的元素比maxNum大,則獲取數組中的值
maxNum = arr[i];
}
}
return maxNum;
}
int main(void)
{
? ? //定義int數組并初始化
int numArr[3] = {3, 8, 6};
? ??
? ? //自動調用int getMax(int a, int b)
cout << getMax(numArr[0], numArr[1]) << endl;
? ??
? ? //自動調用返回數組中最大值的函數返回數組中的最大值
cout << getMax(numArr,3) << endl;
return 0;
}
2021-02-10
cout << getMax(numArr[0], numArr[1]) << endl改為cout << getMax(numArr[0], numArr[2]) << endl