2017-02-23 58 views
-4

我正在研究气球排序,因为它是我的任务之一,但Google只给我一个气球排序链接样本,其余的是泡泡排序。 我编译开发的C++代码,并说,它有一些错误...气球排序C++

这里[链接](http://www.codemiles.com/c-examples/balloon-sort-algorithm-c-implementation-code-sorting-array-t10823.html)! ,谷歌给了我一个 这里是代码...

#include<iostream> 
using namespace std; 
void balloon() 
{ int num, N[10], x, y, z,temp; 
clrscr(); 
cout<<"How many number would you like to sort? "; 
cin>>num; 
cout<<"Input the "<<num<<" numbers:"<<endl; 
for(x=0;x<num;x++) 
cin>>N[x]; 
for(x=0;x<num;x++) 
{ 
for(y=0;y<num-x;y++) 
{ if(N[x] > N[x+y]) 
{ temp=N[x]; 
N[x] =N[x+y]; 
N[x+y]=temp; 
} 
} 
cout<<"pass "<<x+1<<"] "; 
for(z=0;z<num;z++) 
{ 
cout<<setw(5)<<N[z]; 
} 
cout<<endl; 
} 
} 

Error Picture Link

你能帮助我如何气球排序在C++中有一些解释...提前代码谢谢!

+0

等待..我将编辑帖子...我只需要知道如何在此处输入程序在stackoverflow ... – Kolliath01

+0

您缺少两个include语句。做一个编译器找不到的两个函数的websearch,你应该找到文档页面,告诉你需要添加哪些头文件。 – user4581301

+0

您发布的代码只是[选择排序](https://en.wikipedia.org/wiki/Selection_sort)。术语“气球排序”并不常见。在我看到的很少的引用中,有些人说它和冒泡排序类似,有人说它和插入排序类似。但是我看到的几个代码示例显示它是一个简单的选择排序。 –

回答

0

它会有一些错误,因为编译器将搜索int main()所以更改无效气球,并去掉clrsrc();

由于您使用的setw(),则必须使用#include <iomanip>

标题是一部分C++标准库的输入/输出库。它定义了机械手功能resetiosflags(),setiosflags(),setbase(), setfill(),setprecision()setw()。 C++程序可以方便地使用这些函数来影响iostream对象的状态。

最后一点,你必须使cout<<setw(5)<<N[z];cout<<std::setw(5)<<N[z];

#include<iostream> 
#include<iomanip> 
using namespace std; 
int main() 
{ int num, N[10], x, y, z,temp; 

cout<<"How many number would you like to sort? "; 
cin>>num; 
cout<<"Input the "<<num<<" numbers:"<<endl; 
for(x=0;x<num;x++) 
cin>>N[x]; 
for(x=0;x<num;x++) 
{ 
for(y=0;y<num-x;y++) 
{ if(N[x] > N[x+y]) 
{ temp=N[x]; 
N[x] =N[x+y]; 
N[x+y]=temp; 
} 
} 
cout<<"pass "<<x+1<<"] "; 
for(z=0;z<num;z++) 
{ 
cout<<std::setw(5)<<N[z]; 
} 
cout<<endl; 
} 
} 

,如果你运行它...这里是我的样本输出

How many number would you like to sort? 5 
Input the 5 numbers: 
8 
2 
4 
9 
0 
pass 1]  0 8 4 9 2 
pass 2]  0 2 8 9 4 
pass 3]  0 2 4 9 8 
pass 4]  0 2 4 8 9 
pass 5]  0 2 4 8 9 

-------------------------------- 
Process exited after 8.305 seconds with return value 0 
Press any key to continue . . . 

希望这个作品你的任务!祝你好运!