我的代碼可以運(yùn)行可是結(jié)果為什么不對(duì)
#include<iostream>
#include<stdlib.h>
using namespace std;
???
int getMaxorMin(int *arr,int count,bool ismax)
{
int temp;
if(ismax)
{
temp=arr[0];
for(int i=1;i<count;i++)
{
if(temp<arr[i])
{
temp=arr[i];
}
}
return temp;
}
if(ismax)
{
temp=arr[0];
for(int i=1;i<count;i++)
{
if(temp>arr[i])
{
temp=arr[i];
}
}
return temp;
}
}
int main(void)
{
int ismax;
cin>>ismax;
int arr1[4]={1,5,7,2};
cout<<getMaxorMin(arr1,4,ismax)<<endl;
system("pause");
return 0;
}
2019-07-17
為什么你有8個(gè){卻只有7個(gè) };注意下return的位置和一開始定義的在一個(gè)大括號(hào)里面
2019-07-11
①你在getMaxorMin里用了兩個(gè)if(isMax)。if(isMax)的意思是如果isMax結(jié)果為true則怎么怎么樣?,F(xiàn)在如果傳入isMax=true就會(huì)有兩個(gè)入口,如果isMax=false就會(huì)沒有入口。
建議:
if(isMax)
{
...
}
else
{
...
}
②你在getMaxorMin里需要的是布爾型的isMax,在main函數(shù)里傳入的是整型的isMax,不匹配。(不知道這個(gè)會(huì)不會(huì)有問(wèn)題,還沒有試過(guò))
③你在main函數(shù)中聲明的isMax沒有賦初值,可能導(dǎo)致編譯時(shí)隨機(jī)賦值。
在下才疏學(xué)淺,說(shuō)的有什么不對(duì)的還請(qǐng)見諒。
2019-04-14
#include<iostream>
#include<stdlib.h>
using namespace std;
???
int getMaxorMin(int *arr,int count,bool ismax)
{
int temp;
if(ismax)
{
temp=arr[0];
for(int i=1;i<count;i++)
{
if(temp<arr[i])
{
temp=arr[i];
}
else
{
?if(temp>arr[i])
?{
??temp=arr[i];
?}
}
if(temp>arr[i])
{
temp=arr[i];
}
}
return temp;
}
}
int main(void)
{
int arr1[4]={1,5,7,2};
bool ismax =false;
cin>>ismax;
?
cout<<getMaxorMin(arr1,4,ismax)<<endl;
system("pause");
return 0;
}
改完后的代碼,請(qǐng)參考