86.6k views
4 votes
Modify the selectionSort function presented in this chapter so it sorts an array of strings instead of an array of ints. Test the function with a driver program. Use program 8-8 as a skeleton to complete.

Program 8-8

#include

#include

using namespace std;

int main()

{

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" };

//Insert your code to complete this program

return 0;

}

1 Answer

4 votes

Answer:

Following are the method to this question:

void selectionSort(string strArr[], int s) //defining selectionSort method

{

int i,ind; //defining integer variables

string val; //defining string variable

for (i= 0; i< (s-1);i++) //defining loop to count value

{

ind = i; //assign loop value in ind variable

val = strArr[i]; // strore array value in string val variable

for (int j = i+1;j< s;j++) // defining loop to compare value

{

if (strArr[j].compare(val) < 0) //using if block to check value

{

val = strArr[j]; //store value in val variable

ind= j; //change index value

}

}

strArr[ind] = strArr[i]; //assign string value in strArray

strArr[i] = val; // assign array index value

}

}

void print(string strArr[], int s) //defining print method

{

for (int i=0;i<s;i++) //using loop to print array values

{

cout << strArr[i] << endl; //print values

}

cout << endl;

}

Step-by-step explanation:

In the above-given code, two methods "selectionsort and print" is declared, in which, both method stores two variable in its parameter, which can be described as follows:

  • Insides the selectionSort method, two integer variable "i and ind" and a string variable "val" is defined, inside the method a loop is declared, that uses the if block condition to sort the array.
  • In the next line, another method print is declared that declared the loop to prints its value.
  • Please find the attachment for full code.
Modify the selectionSort function presented in this chapter so it sorts an array of-example-1
User Magali
by
4.5k points