初学者问题!!请大家帮忙一下,在线等

a97191 2005-12-29 12:34:36
#include <iostream.h>
int *findMax(int *array,int size,int* index);
void main()
{
int a[10]={33,91,54,67,82,37,85,63,19,68};
int *maxaddr;
int idx;
maxaddr=findMax(a,sizeof(a)/sizeof(*a),&idx);
cout<<"the index of maximun element is"<<idx<<endl;
cout<<"the address of it is"<<maxaddr<<endl;
cout<<"the value of it is"<<a[idx]<<endl;
}
int *findMax(int *array,int size,int* index)
{

int a;
a=*array;
for(int i=1;i<size-1;i++)
{
if(array[i]>a)
{
a=array[i];
*index=i;
array++;

}
}

return &a;


}
函数 findMax有没有更好的算法,我觉得自己编的太啰嗦
...全文
126 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
vollin 2005-12-29
  • 打赏
  • 举报
回复
#include <iostream>
#include <algorithm>
using namespace std;




int main()
{
int a[10]={33,91,54,67,82,37,85,63,19,68};
cout<<*max_element(a,a+10)<<endl;
return 0;
}



ouyh12345 2005-12-29
  • 打赏
  • 举报
回复
循环做得不对,if块里不用再array++
vollin 2005-12-29
  • 打赏
  • 举报
回复
template<class ForwardIterator>
ForwardIterator max_element(
ForwardIterator _First,
ForwardIterator _Last
);
template<class ForwardIterator, class BinaryPredicate>
ForwardIterator max_element(
ForwardIterator _First,
ForwardIterator _Last,
BinaryPredicate _Comp
);
一个函数搞定
yuanchuang 2005-12-29
  • 打赏
  • 举报
回复
我的程序错了:
把 return i;
改成: return a;
czg516 2005-12-29
  • 打赏
  • 举报
回复
/*#include <iostream.h>
int *findMax(int *array,int size,int* index);
void main()
{
int a[10]={33,91,54,67,82,37,85,63,19,68};
int *maxaddr;
int idx;
maxaddr=findMax(a,sizeof(a)/sizeof(*a),&idx);
cout<<"the index of maximun element is"<<idx<<endl;
cout<<"the address of it is"<<maxaddr<<endl;
cout<<"the value of it is"<<a[idx]<<endl;
}
int *findMax(int *array,int size,int* index)
{

int a;//这个a的地址此时已经确定,后面所得到的a的只是值是最大值,他和开始数组里的最大值没有关系。
a=*array;
for(int i=1;i<size-1;i++)
{
if(array[i]>a)
{
a=array[i];
*index=i;
array++;
}
}

return &a;//这个值不正确。我认为你既然用index确定了下标,那么就可以直接用&a[idx]给它求值。

}*/
//感觉楼主的代码定义的变量太乱,且可读性差。
//帮楼主修改下代码:
#include <iostream.h>
int findMax(int *array,int size);
void main()
{
int a[10]={33,91,54,67,82,37,85,63,19,68};
int idx;
idx=findMax(a,sizeof(a)/sizeof(*a));
cout<<"the index of maximun element is"<<idx<<endl;
cout<<"the address of it is"<<&a[idx]<<endl;
cout<<"the value of it is"<<a[idx]<<endl;
}
int findMax(int *array,int size)
{
int a=array[0];
int j;
for(int i=1;i<size-1;i++)
{
if(array[i]>a)
{
a=array[i];
j=i;
}
}
return j;//数组的最大优势是下标,查找迅速,只要知道下标就能确定这个数,在这里返回下标。
}
yuanchuang 2005-12-29
  • 打赏
  • 举报
回复
不好意思,是野指针
yuanchuang 2005-12-29
  • 打赏
  • 举报
回复
你没有看出我前d面有一个打印地址吗
------------------------------------
什么意思?是跟我说的吗?

你把a的地址传出来就是错的,接受这个地址的指针会变成也指针。
a97191 2005-12-29
  • 打赏
  • 举报
回复
你没有看出我前d面有一个打印地址吗
yuanchuang 2005-12-29
  • 打赏
  • 举报
回复
你这里没有必要返回地址吗,就一个整数而已,为什么要返回地址?
yuanchuang 2005-12-29
  • 打赏
  • 举报
回复
int findMax(int *array,int size)
{
int a = 0;
for(int i = 1; i < size; i++)
if (array[i] > array[a])
a = i;
return i;
}
a97191 2005-12-29
  • 打赏
  • 举报
回复
那怎么做才能返回他的地址
LiuYinChina 2005-12-29
  • 打赏
  • 举报
回复
stl 中的find_if
yuanchuang 2005-12-29
  • 打赏
  • 举报
回复
这个函数我看第一眼就有问题:
return &a,
a是函数里面定义的,却要返回它的地址。
有这种错误不应该。

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧