Please help me! Homework 08A COMSC-44 Sorting Strings Modify the selectionSort f
ID: 3693696 • Letter: P
Question
Please help me!
Homework 08A COMSC-44
Sorting Strings
Modify the selectionSort function presented in chapter 8 so it sorts an array of strings instead of an array of ints. You’ll also want to modify the showArray function to accommodate strings. Test the function with a driver program. Use Program 8-8, shown below, as a skeleton to complete.
// Program 8-8:
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Define an array with unsorted values
const int SIZE = 20;
string names[SIZE] = {"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;
}
Explanation / Answer
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
//Constant globals
const int NUM_NAMES = 20;
//Function protoypes
void selectionSort(string [], int);
void showArray(const string [] , int);
int main()
{
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"};
cout << "The unsorted values are ";
showArray(names, NUM_NAMES);
//Sort array
selectionSort(names, NUM_NAMES);
//Display sorted array
cout << "The sorted values are ";
showArray(names, NUM_NAMES);
return 0;
}
void selectionSort(string array[], int NUM_NAMES)
{
int startScan, minIndex;
string minValue;
for(startScan = 0; startScan < (NUM_NAMES -1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan +1; index < NUM_NAMES; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
void showArray(string array[], int N)
{
for(int i=0;i<N;i++)
{
cout<<array[i]<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.