Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I could use some help with this C++ program, I have some bugs in my code that I

ID: 3694412 • Letter: I

Question

I could use some help with this C++ program, I have some bugs in my code that I can't quite figure out.

Modify the selection sort function presented below so it sorts an array of C++ strings (std::string) instead of an array of ints.

It should take two parameters, an array of strings, and the size of the array. If you use the built-in string comparison, "Zebra" would come before "alligator" because in ASCII capital letters have lower numbers than lower-case letters.  Your sort, however, should be case-insensitive, so that "alligator" would come before "Zebra".

Hint: you can make your own string comparison function that uses toupper() together with the built-in string comparison, but don't change the original strings. Your function must be named stringSort.

The file must be called stringSort.cpp

Example to be modified:

/**************************************************************
* selectionSort *
* This function performs an ascending-order selection sort *
* on array. The parameter size holds the number of elements *
* in the array. *
************************************************************** /
void selectionSort(int array[], int size)
{
   int startScan, minindex, minValue;
for (startScan = O; startScan < (size - l); 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;
   }
}

Explanation / Answer

main.cpp
#include <iostream>


void stringSort(std::string[], int);

int main()
{

const int ARRAY_SIZE = 7;
std::string corndogs[ARRAY_SIZE];

corndogs[0] = "Zebrat";
corndogs[1] = "Beans";
corndogs[2] = "bacon";
corndogs[3] = "zebras";
corndogs[4] = "alligator";
corndogs[5] = "alligatorm";
corndogs[6] = "alligatorl";

stringSort(corndogs, ARRAY_SIZE);

for (int i = 0; i < ARRAY_SIZE; i++)
    {

      std::cout << corndogs[i] << ' ';
    }

return 0;
}


stringSort.cpp
#include <string>

bool stringLessThen(std::string, std::string);


void stringSort(std::string arrayIn[], int sizeIn)
{

int minIndex;
std::string minString;                                             // Declaring necessary variables

for (int startScan = 0; startScan < (sizeIn-1); startScan++)        // Loop through each element of string array
    {
      minIndex = startScan;                                           // Setting minimum string value to initial index of the
      minString = arrayIn[startScan];                                 // subarray we are analyzing

      for (int index = startScan + 1; index < sizeIn; index++)        // Iterates through the sub array and finds the lowest value
   {
      if (stringLessThen(arrayIn[index], minString))
        {
          minString = arrayIn[index];
          minIndex = index;
        }
   }
      arrayIn[minIndex] = arrayIn[startScan];                         // Swaps the lowest value with the first element of the sub
      arrayIn[startScan] = minString;                                 // array
    }
}

bool stringLessThen(std::string string1In, std::string string2In)
{
int length1,
    length2,
    counter=0;
bool returnBool = false,
    checkBool = true;                                                             // Declaring necessary variables

length1 = string1In.length();                                                   // Getting lengths of the strings
length2 = string2In.length();

while (checkBool && (counter < length1) && (counter < length2))                 // Looping through each character in the strings
    {                                                                             // until characters don't match, or reach end
      if ( toupper(string1In.at(counter)) < toupper(string2In.at(counter)))       // True if character in string 1 is less then
   {                                                                         // the character in string 2. Then sets return
      returnBool = true;                                                      // bool to true, and checkBool to false to exit
      checkBool = false;                                                      // the loop
   }
      else if ( toupper(string2In.at(counter)) < toupper(string1In.at(counter))) // True if string 2 character < string 1 character
   {                                                                         // Sets return bool to false, and checkBool to
      returnBool = false;                                                     // false to exit the loop
      checkBool = false;
   }
      counter++;
    }

return returnBool;                                                              // Returns true if string 1 < string 2 ignoring
                                                                                  // the case. False, otherwise.
}


sample output

alligator                                                                                                                                                   
alligatorl                                                                                                                                                  
alligatorm                                                                                                                                                  
bacon                                                                                                                                                       
Beans                                                                                                                                                       
zebras                                                                                                                                                      
Zebrat                                                                                                                                                      
Compile | Execute | Share Project

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote