C++从键盘输入五个整数放到数组中,将它们由大到小排列后输出

来源:百度知道 编辑:UC知道 时间:2024/07/03 00:37:55
要求:使用变量的引用

完整的C++程序;
#include <iostream>
uisng namespace std;
int main()
{
int array[5];
for(int i=0; i<5;i++)
{
cin>>array[i];
}
int temp;
for(int i=0; i<4;i++)//冒泡排序法
{
for(int j=i+1;j<5;j++)
if(array[i]<array[j])
{
temp =arrayp[i];
array[i] = array[j];
array[j]=temp;
}
}
cout<<"After the sort:"<<endl;
for(i=0;i<5;i++)
cout<<array[i]<<" ";
return 0;
}

文件名 a.cpp

#include <stdio.h>
#include <stdlib.h>

void swap (int &a, int &b)
{int temp = a;
a = b;
b = temp;
}

void main()
{
int a[5];
int i,j;
printf("Please input 5 int data:\n");
for (i=0;i<5;i++) scanf("%d",&a[i]);

for (i=0;i<4;i++)
for (j=i+1;j<5;j++)
if (a[j] > a[i]) swap( a[i], a[j]);
f