//VS2005#include using namespace std; void printAarry(int num[],int counter) { counter=sizeof(num)/sizeof(num[0]); //这么取数组长度,未必正确,至少ANSI C不行吧.//若不用STL,并不能定义大小不确定的数组,//所以,通常情况下数组长度是已知的,这里counter可传入一个常数.for(int i=0;i<counter;i++) { cout<<num<<","; } cout<<endl; } void butte(int num[],int counter) { counter=sizeof(num)/sizeof(num[0]); int temp; for(int i=0;i<counter;i++) { for(int j=0;j<counter-i-1;j++) { if(num[j+1]<num[j]) { temp=num[j]; num[j]=num[j+1]; num[j+1]=temp; } } } } void main() { int s[] = {5,7,4,3,11,8,12,32,1},counter=0; cout<<"输入排序前的数组:"<<endl;//若不定义一个变量,也可传入一个常量给counter.//比如printAarry(s,9);printAarry(s,counter); butte(s,counter); cout<<"排序后的数组:"<<endl; printAarry(s,counter); } |