-4

我真正想要我的程序做的是要求用户输入 5 个值,将这 5 个值输出到屏幕,将它们从最低到最高排序,然后将排序顺序输出到屏幕。我有以下代码:

void sort(float[], int);

int main()
{

    const int SIZE = 5;
    float a[SIZE];

    cout << "Enter " << SIZE << "numbers:\n";

    for(int i = 0; i<SIZE; i++);
    cin >> a[i];
    sort(a,5);
    cout << "In sorted order: ";
}

void sort (float a[], int n)
{
    for (int i=1; i < n; i++)
        for (int j=0; j < n-i; j++)
            if (a[j] > a[j+1]) swap (a[j], a[j+1]);
}
4

1 回答 1

3

You have a semicolon after your for loop. Remove that and try again.

for(int i = 0; i<SIZE; i++)
    cin >> a[i];

Then call your sort function. Print out the array after you are done sorting.

sort(a,5);
cout << "In sorted order: ";
for(int i = 0; i<SIZE; i++)
        cout << a[i] << " ";

You can also take a peek at the working code here

于 2013-05-28T16:44:18.350 回答