you will write a program in C++ which will read a list of up to 100 integers fro
ID: 3634099 • Letter: Y
Question
you will write a program in C++ which will read a list of up to 100 integers from the user, and print them back to the screen in sorted order. The user can indicate that they are done entering numbers by entering any negative value.Your program will store the entered numbers into an array. It will then sort the array, using the selection sort algorithm. After each iteration of the sorting algorithm, it will print the current state of the array.
Selection Sort
Sorting is a very common and popular problem in computer science. There are numerous algorithms for sorting an array of values. Each algorithm has pros and cons, but ultimately, the most distinguishing factors deal with simplicity, memory usage, and efficiency.
The algorithm that you will implement is called selection sort. In this algorithm, you will scan the numbers in your array and look for the the index of the element that has the minimum value. When found, you will swap the value at the first index in the array with the min value. After one iteration, you will have the smallest value in the first location of the array.
In the next iteration, you will begin your minimum search from the second second element in the array. You will find the new minimum and swap with this location. You will continue this process of finding the next minimum values and putting them in order. Slowly, your array will become sorted. After completing this process count - 1 times, where count is the number of items needing to be sorted, your array will be sorted.
An illustration of the algorithm can be found on Wikipedia. The following demonstrates what your array will look like after each iteration:
Begin: 4 12 2 90 0 24 15 12
Iteration 1: 0 12 2 90 4 24 15 12
Iteration 2: 0 2 12 90 4 24 15 12
Iteration 3: 0 2 4 90 12 24 15 12
Iteration 4: 0 2 4 12 90 24 15 12
Iteration 5: 0 2 4 12 12 24 15 90
Iteration 6: 0 2 4 12 12 15 24 90
Iteration 7: 0 2 4 12 12 15 24 90
Example Session
$ ./sorter
Please enter up to 100 different numbers.
Indicate that you are done by entering a
negative value.
=> 4
=> 12
=> 2
=> 90
=> 0
=> 24
=> 15
=> 12
=> -1
Begin: 4 12 2 90 0 24 15 12
Iteration: 0 12 2 90 4 24 15 12
Iteration: 0 2 12 90 4 24 15 12
Iteration: 0 2 4 90 12 24 15 12
Iteration: 0 2 4 12 90 24 15 12
Iteration: 0 2 4 12 12 24 15 90
Iteration: 0 2 4 12 12 15 24 90
Iteration: 0 2 4 12 12 15 24 90
End: 0 2 4 12 12 15 24 90
$
$ ./sorter
Please enter up to 100 different numbers.
Indicate that you are done by entering a
negative value.
=> -1
Begin:
End:
$
The Code
In order to get you started, I am providing you with your main function along with all of the function declarations that are necessary to complete this lab successfully.
#include <iostream>
#include <string>
using namespace std;
// Reads in integer values from the user up to max_size times or a negative
// value. The values are stored into the numbers array.
// The function will return the number of positive integers read in.
int read_numbers(int numbers[], int max_size);
// Prints the title which is set to a width of 10 for formatting purposes.
// It will then print the contents of the array numbers up to count values.
void print_numbers(string title, const int numbers[], int count);
// Sorts the numbers array using the selection sort algorithm
void sort(int numbers[], int size);
// Swaps two integer variables
void swap(int &a, int &b);
// Returns the INDEX of the element that contains the minimum value in the
// array numbers. The search will begin from start_index and will proceed
// up to element (size - 1)
int min_index(const int numbers[], int size, int start_index);
const int MAX_NUMBERS = 100;
int main()
{
int numbers[MAX_NUMBERS];
int count = read_numbers(numbers, MAX_NUMBERS);
print_numbers("Begin:", numbers, count);
sort (numbers, count);
print_numbers("End:", numbers, count);
return 0;
}
Explanation / Answer
#include #include using namespace std; // Reads in integer values from the user up to max_size times or a negative // value. The values are stored into the numbers array. // The function will return the number of positive integers read in. int read_numbers(int numbers[], int max_size); { int count=0; for(int i=0; inumbers[i]; coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.