亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

我輸入一個非零的數,運行結果是 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;

?? ?

?? ?

?? ?

}


正在回答

1 回答

?for(int i=1;i<=count; i++)

? ? ? ? {

? ? ? ? ? ? if(temp<arr[i])

? ? ? ? ? ? ? ? temp=arr[i];

? ? ? ? }

for 循環里不要用<=count ,數組下標會越界,寫<count就ok了.

0 回復 有任何疑惑可以回復我~
#1

YiShen23 提問者

明白了,謝謝
2017-05-01 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
C++遠征之起航篇
  • 參與學習       290974    人
  • 解答問題       817    個

C++亮點盡在其中,本課程是在C語言基礎上的一個延伸,得以升華

進入課程

我輸入一個非零的數,運行結果是 1606416392,但是我的數組是{1,2,3,4}。當我輸入0時,運行結果是 1。這是怎么回事????

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號