Lab 1

Lab 2

Lab 3

Lab 4

Lab 5

Lab 6

Lab 7

Lab 8

Lab 9

Lab 10

« hide

DUE: Monday 8/3

Write a C++ program that uses functions to both sort and search an array of elements.

  1. Write a function called read_names() that opens and reads from the names.txt text file. Each name should be stored in an array (which is passed in to the function). Assume that the maximum number of names is 30.
  2. Call the read_names() function from main(), passing in an empty array of names. The function "populates" the array with names. Still in main(), display the following message:
    Successfully read in 21 names.
    
  3. Write a function called sort_names() that sorts the given array of names. The function has no return value (i.e. it's a void function). Use the following sorting algorithm:
    For each array position 0 through length-1:
    
      a. Find the name closest to "A" in the array from the current position to the end
    
      b. Swap name at current position with name closest to "A" found
    
    
    In other words, swap the first name into its correct position, then swap the second
    name into its correct position, then swap the third name into its correct position, etc.
    
    
  4. In main(), pass the array of names to the sort_names() function. Display results in main(), as in:
    Successfully sorted the list of names:
      Brett
      Dave
      David
      Deanna
      Edie
      Francis
      Frank
      Franklin
      George
      Helen
      Hubert
      Jackie
      Jenna
      John
      John
      Jon
      Lois
      Luke
      Mary
      Mary-Anne
      Paula
    
    
  5. Write a function called binary_search() that searches the given array of sorted names for a specific name. The function returns either true or false.
  6. In main(), ask the user to enter a name, then pass the name and the array of names to the binary_search() function. Display results in main(), as in:
    Enter a name to search for: Deanna
    Found Deanna in the list.
    
    Enter a name to search for: Greg
    Sorry, Greg is not in the list.
    
    
  7. Test your program thoroughly.