Ask Question
5 December, 05:56

Write a program that gets a list of integers from input, and outputs the integers in ascending order (lowest to highest). The first integer indicates how many numbers are in the list. Assume that the list will always contain less than 20 integers. Ex: If the input is:

+4
Answers (1)
  1. 5 December, 07:48
    0
    The c+ + program for the given problem is shown below along with the explanation of each step.

    #include

    using namespace std;

    int main () {

    int arr[20];

    int count = 0, num, swap;

    for (int i=0; i<20; i++)

    {

    arr[i] = 0;

    }

    cout<<"Enter the integers to be sorted in ascending order. Enter 0 to stop entering the integers. " <
    for (int i=0; i<20; i++)

    {

    cin>>num;

    if (num! = 0)

    {

    arr[i] = num;

    count++;

    }

    else

    {

    cout<<"The numbers entered have been recorded. "<
    break;

    }

    }

    for (int i=0; i<20; i++)

    {

    for (int j=i+1; j<20; j++)

    {

    if (arr[i]! = 0)

    {

    if (arr[i] > arr[j])

    {

    swap = arr[i];

    arr[i] = arr[j];

    arr[j] = swap;

    }

    else

    continue;

    }

    else

    break;

    }

    }

    cout<<"Total number of integers entered is "<
    cout<<"The numbers in ascending order are "<
    for (int i=0; i<20; i++)

    {

    if (arr[i]! = 0)

    cout<
    }

    }

    1. We use an integer array to hold the integers entered by the user. Hence, we declare integer variables and array of size 20. The variables and array is initialized to 0.

    int arr[20];

    int count = 0, num, swap;

    Array initialization is done to ease the sorting and displaying of the elements. User enters 0 only to quit, hence, elements having value 0 indicates null values.

    for (int i=0; i<20; i++)

    {

    arr[i] = 0;

    }

    2. The integers entered by the user are put in the array and counted simultaneously.

    for (int i=0; i<20; i++)

    {

    cin>>num;

    if (num! = 0)

    {

    arr[i] = num;

    count++;

    }

    else

    {

    cout<<"The numbers entered have been recorded. "<
    break;

    }

    }

    3. The integers in the array are sorted in ascending order using a third variable swap, for swapping the integers.

    for (int i=0; i<20; i++)

    {

    for (int j=i+1; j<20; j++)

    {

    if (arr[i]! = 0)

    {

    if (arr[i] > arr[j])

    {

    swap = arr[i];

    arr[i] = arr[j];

    arr[j] = swap;

    }

    else

    continue;

    }

    else

    break;

    }

    }

    4. Lastly, the number of integers entered and the integers in ascending order the displayed.

    for (int i=0; i<20; i++)

    {

    if (arr[i]! = 0)

    cout<
    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a program that gets a list of integers from input, and outputs the integers in ascending order (lowest to highest). The first integer ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers