Hi, need helps with this problem.

[i][i]Write a program that will sort in ascending order a set of decimal data items provided by the user. The user may enter up to a maximum of 20 decimal data items. First, the program will ask the user to enter data item count. Then it will ask the user to enter a data item. It will ask the user to enter a data item repeatedly till all the data items are entered. Then it will display all the data items in the order entered by the user. After that, it will display all the data items sorted in ascending order.

Above, when the user is asked to enter data item count and the user enters a count that is outside the required range, the program will display a message to that effect and ex
it.[/i]
[/i]

This is what I wrote, but it won't work, and I don't know what's wrong.
It would be so nice if anyone could help. Thank you so much.

#include <iostream>

using namespace std;

void sort (double x[], int length)
{
bool swapDone;
int i,temp;
while (swapDone == true)
{
swapDone = false;

for (i=0;i< length-1; i++)
{
if (x[i] > x [i+1])
{
temp = x [i];
x[i] = x[i+1];
x[i+1] = temp;
swapDone = true;
}
}

}
for (i=0;i<length;i++)
{
cout << x[i] << " " ;
}
cout << endl;

}

int main()
{

int i,scoreCount,length;
cout << "Enter Score Count:" << endl;
cin >> scoreCount;
if (scoreCount >= 1 & scoreCount <= 20)
{
double x[scoreCount];
for (i=0;i<scoreCount;i++)
{
cout << "Enter Score:" << endl;
cin >> x[i];

}
cout << "Original Data: "<< endl;
for (i=0;i<scoreCount;i++)
{
cout << x[i] << " ";
}
cout << endl;
cout << "Sorted Data: " << endl;
sort( x, scoreCount);
}
else
{
cout << "Class size is NOT within required range. The required range is 1 to 20." << endl;
}


return 0;

}
closed account (4z0M4iN6)
I didn't look at your code and I don't know your problem.
But if it should be, that your sort wouldn't work,
you could have a look to following topic:

http://cplusplus.com/forum/beginner/70422/

Because it contains also sorting in C,
posted by CodeMonkey and me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>

using namespace std;

void sort (double x[], int length)
{
     // You need to initialize swapDone to true, otherwise it will never make it into the while loop
    bool continueSort = true; // Changed name to better reflect use
    int i;
    double temp; // Your temp variable need to have the same type as the variable it's storing 
    while (continueSort == true)
    {
        continueSort = false;

        for (i=0;i< length-1; i++)
        {
            if (x[i] > x [i+1])
            {
                temp = x [i];
                x[i] = x[i+1];
                x[i+1] = temp;
                continueSort = true;
            }
        }
    }
    // Moved output to main function
}

int main()
{
    int i,scoreCount,length;
    cout << "Enter Score Count: ";
    cin >> scoreCount;
    if (scoreCount >= 1 && scoreCount <= 20) // The AND operator is &&
    {
        double x[scoreCount];
        for (i=0;i<scoreCount;i++)
        {
            cout << "Enter Score #" << i + 1 << ": "; // Cout now displays index of score to be input
            cin >> x[i];
        }
        cout << "Original Data: "<< endl;
        for (i=0;i<scoreCount;i++)
        {
            cout << x[i] << " ";
        }
        cout << endl;
        sort( x, scoreCount);
        cout << "Sorted Data: " << endl;
        for (i=0;i<scoreCount;i++)
        {
            cout << x[i] << " " ;
        }
        cout << endl;
    }
    else
    {
        cout << "Class size is NOT within required range. The required range is 1 to 20." << endl;
    }
    return 0;
}


The logic was decent enough. A few errors here and there. Works now!
Thank you very much bcrawford, it works perfectly!
Topic archived. No new replies allowed.