#include<stdlib.h>
#include<iostream>
using?namespace?std;
int?getMaxOrMin(int?*arr,?int?count,?bool?isMax)
{
????int?temp?=?arr[0];
????for(int?i=1;i<=count;i++)
????{
????????if(isMax)
????????{
????????????if?(temp?<?arr[i])
????????????{
????????????temp?=?arr[i];
????????????}
????????else
???????????if?(temp?>?arr[i])
???????????{
???????????????temp?=?arr[i];
???????????}????
????????}
????????
????}
????return?temp;
}
int?main(void)
{
????int?arr1[4]?=?{4,7,3,9};
????bool?isMax?=?false;
????cin>>isMax;
????cout<<getMaxOrMin(arr1,4,isMax)<<endl;
?????system("pause");
?????return?0;
}
?我運行的時候只出了最小值,然后輸入1最大值出不來,而是出現一串奇怪的數字。
2017-01-13
數組下標越界(ArrayIndexOutOfBoundsException)
2016-12-04
在for循環的條件中,i不能<=count,這樣,當i等于4時還是滿足條件的,i再次加1時,此時數組中沒有這個數,所以出現了亂碼