為啥出來個(gè)32765
#include <iostream>
using namespace std;
/**
? *函數(shù)功能:返回a和b的最大值
? *a和b是兩個(gè)整數(shù)
? */
int getMax(int a, int b)
{
? ? return a > b ? a : b;
}
/**
? * 函數(shù)功能:返回?cái)?shù)組中的最大值
? * arr:整型數(shù)組
? * count:數(shù)組長度
? * 該函數(shù)是對(duì)上面函數(shù)的重載
? */
int getMax(int a[],int count)
{
? ? //定義一個(gè)變量并獲取數(shù)組的第一個(gè)元素
? ? int maxNum=a[0];
for(int i = 1; i < count; i++)
{
? ? ? ? //比較變量與下一個(gè)元素的大小
if(maxNum<a[i])
{
? ? ? ? ? ? //如果數(shù)組中的元素比maxNum大,則獲取數(shù)組中的值
maxNum=a[i];
}
}
return maxNum;
}
int main(void)
{
? ? //定義int數(shù)組并初始化
int numArr[3] = {3, 8, 10};
? ??
? ? //自動(dòng)調(diào)用int getMax(int a, int b)
cout << getMax(numArr[0],numArr[1]) << endl;
? ??
? ? //自動(dòng)調(diào)用返回?cái)?shù)組中最大值的函數(shù)返回?cái)?shù)組中的最大值
cout << getMax(numArr[3],3)<< endl;
return 0;
}
2020-07-16
numArr數(shù)組的長度是3,但是數(shù)組中元素是從0開始計(jì)算,也就是numArr[0],numArr[1],numArr[2]三個(gè)元素,numArr[3]是不存在的,出現(xiàn)錯(cuò)誤,而且在將數(shù)組作為參數(shù)傳入函數(shù)時(shí),只需寫明數(shù)組名即可,中括號(hào)中帶有數(shù)字時(shí),表示數(shù)組中的某個(gè)元素,而不代表數(shù)組本身。
2020-02-23
32765
?這個(gè)數(shù)是因?yàn)樵跀?shù)值numArr中,長度為3,但是索引值只到了2,使用numArr[3]的時(shí)候,程序會(huì)自動(dòng)尋找一個(gè)值作為numArr[3]的數(shù)值,實(shí)質(zhì)上 最后那條語句并沒有實(shí)現(xiàn)在數(shù)組中找到最大值,而是實(shí)現(xiàn)的numArr[3]和 3 兩個(gè)整數(shù)的大小比較。
2020-02-23
cout << getMax(numArr[3],3)<< endl; 這句需要修改, -->cout<<getMax(numArr,3);
2019-05-04
cout << getMax(numArr[3],3)<< endl;
numArr[3]:表示第4個(gè)數(shù)組元素,改為numArr