[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 exit.[/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;
}
}
#include <iostream>
usingnamespace std;
void sort (double x[], int length)
{
// You need to initialize swapDone to true, otherwise it will never make it into the while loopbool continueSort = true; // Changed name to better reflect useint 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!