我輸入一個非零的數,運行結果是 1606416392,但是我的數組是{1,2,3,4}。當我輸入0時,運行結果是 1。這是怎么回事????
#include<iostream>
#include<stdlib.h>
using namespace std;
namespace A
{
? ? int GetMaxOrMin(int *arr,int count,bool isMax)
{
? ? int temp=arr[0];
? ? if(isMax)
? ? {
? ? ? ? for(int i=1;i<=count; i++)
? ? ? ? {
? ? ? ? ? ? if(temp<arr[i])
? ? ? ? ? ? ? ? temp=arr[i];
? ? ? ? }
? ? }
? ? else
? ? {
? ? ? ? for(int i=1;i<=count; i++)
? ? ? ? {
? ? ? ? ? ? if(temp>arr[i])
? ? ? ? ? ? ? ? temp=arr[i];
? ? ? ? }
? ? }
? ? return temp;
}
}
int main(void)
{
? ? int arr1[4]={1,2,3,4};
? ? int count=4;
? ? bool ismax=false;
? ? cin>>ismax;
? ? cout<<A::GetMaxOrMin(arr1,count,ismax)<<endl;
?? ?
?? ?
?? ?
}
2017-05-01
?for(int i=1;i<=count; i++)
? ? ? ? {
? ? ? ? ? ? if(temp<arr[i])
? ? ? ? ? ? ? ? temp=arr[i];
? ? ? ? }
for 循環里不要用<=count ,數組下標會越界,寫<count就ok了.