Question

In: Computer Science

C++ Tony Gaddis 8.11: Using Files-- String Selection Sort Modification Modify the selectionSort function presented in...

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.

Solutions

Expert Solution

`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.


Related Solutions

Write a version of the selection sort algorithm in a function called selectionSort that can be...
Write a version of the selection sort algorithm in a function called selectionSort that can be used to sort a string vector object. Also, write a program to test your algorithm. The program should prompt the user for a series of names. The string zzz should end the input stream. Output the sorted list to the console. *Need answer in C++*
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT