c++上机实验八(2):对10个浮点数排序

c++的输入和输出

实验目的和要求

  1. 熟悉流及流类库的作用。
  2. 熟悉掌握流类库中常用的类及其成员函数的使用方法。
  3. 掌握重载“<<”和“>>”的方法。
  4. 掌握控制输出宽度的函数width和setw。

实验内容:

  1. 编程对10个浮点数排序,排序后用浮点格式显示。

代码

#include 
#include 
#include 
using namespace std;

int main()
{
    float a[10];
    int i,j;
    cout<<"Please input 10 float numbers: "<<endl;
    
    for(i=0;i<10;i++)
    {
        cin>>a[i];
    }
    
    for(i=0;i<10;i++)
    {
        for(j=0;j<9-i;j++)
        {
            if(a[j]>a[j+1])
            {
                float t;
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
        }
    }
    cout<<"The sorted numbers are: "<<endl;
    
    for(i=0;i<10;i++)
    {
        cout<<a[i];
        cout<<endl;
    }
    cout<<"\n"<<endl;
	system("pause");
    return 0;
}

你可能感兴趣的:(#,——【,C++】)