The goal of this lab is to help you get familiar with sorting data in an array

Programming Exercise

This lab exercise involves writing a short program to do the following:

  • Prompt the user for an input file name. Open this file for input. The file contains the names of people (one per line).
  • Read the contents of the file into an array of string objects. (The maximum number of names for the array should be at least 50.)
  • Display the input names, in the order that they appear in the file.
  • Your program must have a function called selectionSort that takes parameters for an array of string objects, and the size of the array. The function prototype for the selectionSort function is given below:
void selectionSort(string values[], int size);
  • You may use the same selection sort algorithm from the textbook sample programs, but you will need to change the type of the data variables, so that the algorithm will work with string objects, instead of integers. (This works easily because of some nice features of the string class. We will learn more about the string class in Chapter 10, but for now, just observe that it is easy to modify the selectionSort function to work with string objects.)
  • Display the sorted list.

Add Descriptive Output Statements

  • Add code to the selectionSort function that prints a description of each swap operation that the algorithm executes. The format of each line of this output should be:
Swap [indexA] stringA with [indexB] stringB
  • Maintain a count of the number of swap operations that the algorithm executes. Output the value of this count after the sorting is complete.

Sample Data Files

Input file: firstTen.txt

Washington, George
Adams, John
Jefferson, Thomas
Madison, James
Monroe, James
Adams, John Quincy
Jackson, Andrew
Van Buren, Martin
Harrison, William Henry
Tyler, John

Output from processing the firstTen.txt file

Enter name of input file: firstTen.txt
10 lines of text read from input file.
Here are the unsorted names:
--------------------------
[ 0] Washington, George
[ 1] Adams, John
[ 2] Jefferson, Thomas
[ 3] Madison, James
[ 4] Monroe, James
[ 5] Adams, John Quincy
[ 6] Jackson, Andrew
[ 7] Van Buren, Martin
[ 8] Harrison, William Henry
[ 9] Tyler, John
Swap [ 1] Adams, John with [ 0] Washington, George
Swap [ 5] Adams, John Quincy with [ 1] Washington, George
Swap [ 8] Harrison, William Henry with [ 2] Jefferson, Thomas
Swap [ 6] Jackson, Andrew with [ 3] Madison, James
Swap [ 8] Jefferson, Thomas with [ 4] Monroe, James
Swap [ 6] Madison, James with [ 5] Washington, George
Swap [ 8] Monroe, James with [ 6] Washington, George
Swap [ 9] Tyler, John with [ 7] Van Buren, Martin
Swap [ 9] Van Buren, Martin with [ 8] Washington, George
Number of "swap" operations = 9
Here are the names sorted:
--------------------------
[ 0] Adams, John
[ 1] Adams, John Quincy
[ 2] Harrison, William Henry
[ 3] Jackson, Andrew
[ 4] Jefferson, Thomas
[ 5] Madison, James
[ 6] Monroe, James
[ 7] Tyler, John
[ 8] Van Buren, Martin
[ 9] Washington, George
Exit the program.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.