c++求最大值问题

来源:百度知道 编辑:UC知道 时间:2024/06/30 20:53:37
不知道哪里有错,就是出不来,请帮忙看看
#include <iostream>
using namespace std;
int Findmax(int a[]);
int main()
{
int a[10];
int i;
cout<<"please enter ten numbers:"<<endl;
for (i=0;i<10;i++)
{
cin>>a[i];
}
Findmax(a);
return 0;
}
int Findmax(int a[])
{
int max=a[0];
int j;
for(j=0;j<10;j++)
{
if(a[j]>a[0])
max=a[j];
}
cout<<"the max is"<<a[j];
return a[j];
}

Findmax()函数改为如下:

int Findmax(int a[])
{
int max=a[0];
int j;
for(j=0;j<10;j++)
{
if(a[j]>max)/*注意这里*/
max=a[j];/*注意这里*/
}
cout<<"the max is"<<max;/*注意这里*/
return max;/*注意这里*/
}

cout<<"the max is"<<a[j];
_____________________________
cout<<"the max is"<< max;

输出和返回的是max

2楼说得很好,已经把问题解决了。