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++*
C++.how to write a selection sort to sort it. read_book_data() This member function takes one parameter,...
C++.how to write a selection sort to sort it. read_book_data() This member function takes one parameter, a string that contains the name of a file. This string parameter can be a C++ string or a C string (your choice). The function returns nothing. This constructor should do the following: Declare and open an input file stream variable using the file name string passed in as a parameter. Check to make sure the file was opened successfully. If not, print an...
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
Reversing certain segments of the alphabet, using a recursive function. (C++) 1. Obtain the following string:...
Reversing certain segments of the alphabet, using a recursive function. (C++) 1. Obtain the following string: abcdefghijklmnopqrstuvwxyz (as input or using initialization) 2. Using recursion, write a reverse function that reverses the characters in a string or character array given two indices (starting and ending). The string or the character array should reflect the reversal. 3. Read indices as input 11,18 (i.e. letters 12,19) 4. Call the reverse function to reverse letters: 12-19 5. Read indices as input 4,22 (i.e....
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’ that returns true if s is a palindrome and false if not. #5: Write a recursive function 'void reverse(string &word)' that reverses the given input string. string name = "damian"; reverse(name); cout << name << endl; //should display "naimad". #7: Write a function 'int numTwos(int n)' which returns the number of 2's in the base-4 expansion of n. cout << numTwos(2170) << endl; //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT