Read Displays 7.9 and 7.12 carefully and then modify C++ code written for Displa
ID: 3534556 • Letter: R
Question
Read Displays 7.9 and 7.12 carefully and then modify C++ code written for Display 7.12, so that
it reads a list of positive numbers and sort numbers from highest to lowest.
These are the links for display 7.9..
_______________________________________________________________________
First Photo of display 7.9
http://www.2shared.com/photo/Xk0OVCAl/79_part1.html
Second Photo of display 7.9
http://www.2shared.com/photo/TiHc_7q_/79_part2.html
Third Photo of Display 7.9
http://www.2shared.com/photo/vpbNaaQ_/79_part3_.html
_______________________________________________________________________
//DISPLAY 7.12 Sorting an Array
//Tests the procedure sort.
#include <iostream>
void fill_array(int a[], int size, int& number_used);
//Precondition: size is the declared size of the array a.
//Postcondition: number_used is the number of values stored in a.
//a[0] through a[number_used - 1] have been filled with
//nonnegative integers read from the keyboard.
void sort(int a[], int number_used);
//Precondition: number_used <= declared size of the array a.
//The array elements a[0] through a[number_used - 1] have values.
//Postcondition: The values of a[0] through a[number_used - 1] have
//been rearranged so that a[0] <= a[1] <= ... <= a[number_used - 1].
void swap_values(int& v1, int& v2);
//Interchanges the values of v1 and v2.
int index_of_smallest(const int a[], int start_index, int number_used);
//Precondition: 0 <= start_index < number_used. Referenced array elements have
//values.
//Returns the index i such that a[i] is the smallest of the values
//a[start_index], a[start_index + 1], ..., a[number_used - 1].
int main( )
{
using namespace std;
cout << "This program sorts numbers from lowest to highest. ";
int sample_array[10], number_used;
fill_array(sample_array, 10, number_used);
sort(sample_array, number_used);
cout << "In sorted order the numbers are: ";
for (int index = 0; index < number_used; index++)
cout << sample_array[index] << " ";
cout << endl;
return 0;
}
//Uses iostream:
void fill_array(int a[], int size, int& number_used)
{
using namespace std;
cout << "Enter up to " << size << " nonnegative whole numbers. "
<< "Mark the end of the list with a negative number. ";
int next, index = 0;
cin >> next;
while ((next >= 0) && (index < size))
{
a[index] = next;
index++;
cin >> next;
}
number_used = index;
}
void sort(int a[], int number_used)
{
int index_of_next_smallest;
for (int index = 0; index < number_used - 1; index++)
{//Place the correct value in a[index]:
index_of_next_smallest =
index_of_smallest(a, index, number_used);
swap_values(a[index], a[index_of_next_smallest]);
//a[0] <= a[1] <=...<= a[index] are the smallest of the original array
//elements. The rest of the elements are in the remaining positions.
}
}
 
void swap_values(int& v1, int& v2)
{
int temp;
temp = v1;
v1 = v2;
v2 = temp;
}
 
int index_of_smallest(const int a[], int start_index, int number_used)
{
int min = a[start_index],
index_of_min = start_index;
for (int index = start_index + 1; index < number_used; index++)
if (a[index] < min)
{
min = a[index];
index_of_min = index;
//min is the smallest of a[start_index] through a[index]
}
return index_of_min;
}
Explanation / Answer
If I post the code here, it shows lots of errors by itself. Lots of A comes.
I put the code in an online C++ compiler. You can check the output too. It works well. Done it exactly your way, you wanted it.
Its 100% correct.
Here is the link
Dont forget to rate.
Cheers!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.