首页 新闻 会员 周边

大神,请问一下我这个按大小排序的C++代码错哪了?

0
悬赏园豆:100 [待解决问题]

#include<iostream>
using namespace std;
const int total_size = 20;
void sample_array(int a[], int&i);
void swapvalues(int&a, int&b);
int small_est(int a[], int size_used,int&index);
void main()
{
int b[total_size], n,m;
sample_array(b, n);//n为实际的长度
for (int i = 0; i < n-1; i++)
{


m = small_est(b, n,i);

swapvalues(b[i], b[m]);
}
for (int j = 0; j < n-1; j++)
{
cout << b[j] << "<";

}

system("pause");
}
void sample_array(int a[],int&i) //输入数字放入数组中,i为数组实际长度
{
int next;
i = 0;
cout<<"please input a series of numbers "<<endl<<"end up the input with a negtive number" << endl;
cin >> next;

do {
a[i] = next;
i++;
cin >> next;
} while (next >= 0);

void swapvalues(int&a,int&b)//交换两个数字
{
int c;
c = a;
a = b;
b = c;
}
int small_est(int a[],int size_used,int&index) //返回最小数的数字的下标
{
int min,u=index;
min = a[index];
for (; index <=size_used-1; index++)
{
if (min > a[index])
{
min = a[index];
u=index;
}
}
return u;
}

fycmugua的主页 fycmugua | 初学一级 | 园豆:102
提问于:2017-10-22 10:17
< >
分享
所有回答(3)
0

m=small_est(b,n,i) 改为 m=small_est(b,n--,i)

喜欢夏天的蓝天白云 | 园豆:202 (菜鸟二级) | 2017-10-22 15:59

不行哦

支持(0) 反对(0) fycmugua | 园豆:102 (初学一级) | 2017-10-22 16:45
0

命名成了啥,不要养成不好习惯。

int a[]明显有性能问题,需要拷贝构造

回去好好学习

迅捷网络[来送福利] | 园豆:576 (小虾三级) | 2017-10-24 08:12
0

int small_est(int a[],int size_used,int&index) //返回最小数的数字的下标

最后一个参数不要用引用。

void sample_array(int a[],int&i) //输入数字放入数组中,i为数组实际长度

有可能越界。

yoyo_zeng | 园豆:202 (菜鸟二级) | 2017-11-07 17:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册