Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ PROGRAMMING LANGUAGE!!! modify that code to now sort strings data instead of

ID: 3816013 • Letter: C

Question

 C++ PROGRAMMING LANGUAGE!!!  modify that code to now sort strings data instead of int data.  Use this base code in your main program.    const int NUM_NAMES = 20;    string names[NUM_NAMES] = {"Collins, Bill", "Smith, Bart", "Allen, Jim",                                "Griffin, Jim", "Stamey, Marty", "Rose, Geri",                                "Taylor, Terri", "Johnson, Jill", "Allison, Jeff",                                "Looney, Joe", "Wolfe, Bill", "James, Jean",                                "Weaver, Jim", "Pore, Bob", "Rutherford, Greg",                                "Javens, Renee", "Harrison, Rose", "Setzer, Cathy",                                "Pike, Gordon", "Holland, Beth" };  ************************************************************************       //Code for program 8-5 below   // This program uses the selection sort algorithm to sort an // array in ascending order. #include <iostream> using namespace std;  // Function prototypes void selectionSort(int [], int); void showArray(int [], int);  int main() {          // Define an array with unsorted values    const int SIZE = 6;    int values[SIZE] = {5, 7, 2, 8, 9, 1};     // Display the values.    cout << "The unsorted values are ";    showArray(values, SIZE);        // Sort the values.    selectionSort(values, SIZE);        // Display the values again.    cout << "The sorted values are ";    showArray(values, SIZE);    return 0; }  //************************************************************** // Definition of function selectionSort.                       * // This function performs an ascending order selection sort on * // array. size is the number of elements in the array.         * //**************************************************************  void selectionSort(int array[], int size) {    int startScan, minIndex, minValue;     for (startScan = 0; startScan < (size - 1); startScan++)    {       minIndex = startScan;       minValue = array[startScan];       for(int index = startScan + 1; index < size; index++)       {          if (array[index] < minValue)          {             minValue = array[index];             minIndex = index;          }       }       array[minIndex] = array[startScan];       array[startScan] = minValue;    } }  //************************************************************** // Definition of function showArray.                           * // This function displays the contents of array. size is the   * // number of elements.                                         * //**************************************************************  void showArray(int array[], int size) {    for (int count = 0; count < size; count++)       cout << array[count] << " ";    cout << endl; }    

Explanation / Answer

here is yor code. I have changed some data types in your code because there is comma in name so while assigning names to string array, compiler is actually dividing the name into two parts like Collins and Bill. this is happening in case of string.

I tried with char array it worked.

#include<iostream>
#include<cstring>
using namespace std;

const int numNames = 20, size = 17;
void selectionSort(char [][size], int);
void showArray(char [][size], int);


int main()
{

   char names[numNames][size]= { "Collins, Bill", "Smith, Bart", "Michalski, Jacob",
                               "Griffin, Jim", "Sanchez, Manny", "Rubin, Sarah",
                               "Taylor, Tyrone", "Johnson, Jill", "Allison, Jeff",
                               "Moreno, Juan", "Wolfe, Bill", "Whitman, Jean",
                               "Moretti, Bella", "Wu, Hong", "Patel, Renee",
                               "Harrison, Rose", "Smith, Cathy", "Conroy, Patrick",
                               "Kelly, Sean", "Holland, Beth" };

cout << "The unsorted string is: ";
showArray(names, numNames);
selectionSort(names, numNames);
cout << "The sorted string is: ";
showArray(names, numNames);

system("pause");
return 0;
}



void selectionSort(char arrays[][size], int rows)
{
int startScan= 0, minIndex;
char minValue[17];

   for(int i=0; i<17; i++)
   {
   minValue[i]=arrays[startScan][i];
   }

       for (startScan = 0; startScan < (rows - 1); startScan++)
       {
           minIndex = startScan;
           for(int i=0; i<17; i++)
       {
           minValue[i]=arrays[startScan][i];
       }


           for (int index = startScan + 1; index < rows; index++)
           {
               if (strcmp(arrays[index], minValue) < 0)
               {
                   for(int i=0; i<17; i++)
                   {
           minValue[i]=arrays[index][i];
       }
                   minIndex = index;
}
               }
           for(int i=0; i<17; i++)
           {
           arrays[minIndex][i]=arrays[startScan][i];
           }
           for(int i=0; i<17; i++)
               {
           arrays[startScan][i]=minValue[i];
               }
                   }
}


void showArray(char arrays[][size], int rows)
{
   for (int count = 0; count < rows; count++)
       cout<<arrays[count] << " "<<endl;
   cout<<endl;
}

OutPut: -

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote