In: Computer Science
C++ Tony Gaddis
8.11: Using Files-- String Selection Sort Modification 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 that reads an integer, N, from standard input and then reads the first N strings from a file called href="asfunction:_root.doTutor,7,CPP">names. (Assume that N is less than or equal to 20.) Input Validation. If N read in from standard input is greater than 20 the program terminates silently.
`Hey,
Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
Please note there should be an input.txt file in the same directory of your code which will contain strings as per question.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// Function prototypes
void selectionSort(string[], int);
void displayArray(string[], int);
int main()
{
int n;
cout<<"Enter n: ";
cin>>n;
if(n>20)
return 0;
int SIZE = n;
string name[SIZE];
// Insert your code to complete this program.
ifstream myfile ("input.txt");
if (myfile.is_open())
{
for(int i=0;i<n;i++)
{
myfile>>name[i];
}
myfile.close();
}
else
{
cout << "Unable to open input.txt file";
return 0;
}
// Call the selectionSort function
selectionSort(name, SIZE);
// Call the displayArray function
displayArray(name, SIZE);
return 0;
}
void selectionSort(string array[], int size)
{
int startScan, minIndex;
string minValue;
for (int 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;
}
}
void displayArray(string name[], int size)
{
for (int i = 0; i < size; i++)
{
cout << name[i] <<
endl;
}
}
Kindly revert for any queries
Thanks.