Question
USING C++ and #include Starting with a txt file and saving it as a .c file In this exercise we will learn how to sort
USING C++ and #include
Starting with a txt file and saving it as a .c file
In this exercise we will learn how to sort a list of numbers in descending order (from the largest to the smallest). Suppose an array (a list) of N numbers is available. We want to order the numbers from the largest to the smallest. It is assumed that N 100.
Function Sort: This function receives a 1-D integer array A of size N and sorts the numbers in descending order.
For k = 0 to N-2 do the following:
Call function Largest to find the largest number in the list from A[k] to A[N-1]. Suppose j is the index of the largest number in the list from A[k] to A[N-1].
Switch A[k] and A[j].
Function Largest: This function receives an array A, its size N, and two indices m and n. It then returns the index of the largest number in the list from A[m] to A[n]. It is assumed that n > m, m >= 0 and n < N.
Let L = A[m] and j = m.
For k = m+1 to n do the following:
If A[k] > L, let L = A[k] and j = k;
Return j.
DESCRIPTION:
Prompt the user for N, then for N integers.
Sort numbers in the list in descending order by calling function Sort.
Whenever there is a change in the list, print the list. For example, when array A contains, {1, 4, -9, 3, 2}, your program should print:
{-9, 4, 1, 3, 2}, {-9, 1, 4, 3, 2}, , {-9, 1, 2, 3, 4}
REQUIREMENT:
Write functions Sort and Largest in addition to function main and call Sort within main.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started