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

#include <iostream> #include <string> using namespace std; /********************

ID: 3915912 • Letter: #

Question

#include <iostream>

#include <string>

using namespace std;

/**********************************************************                  

* Function: swapValues                                                      

* Description: Swaps two values                                             

**********************************************************/

void swapValues(int &v1, int &v2)

{

  int temp;

  temp = v1;

  v1 = v2;

  v2 = temp;

}

/**********************************************************                  

* Function: indexOfSmallest                                                 

* Description: Finds the smallest value in the array                        

*   and returns its index.                                                  

**********************************************************/

int indexOfSmallest(const int array[], int startIndex, int size)

{

  int min = array[startIndex];

  int indexOfMin = startIndex;

  for (int index = startIndex + 1; index < size; index++)

  {

    if (array[index] < min)

    {

       min = array[index];

       indexOfMin = index;

    }

  }

  return indexOfMin;

}

/***********************************************************                 

* Function: sort                                                            

* Description: Uses a selection sort to sort the array.                     

*                                                                           

* Source: Savitch, Absolute C++ 5th Edition, pp. 216-217                    

***********************************************************/

void sort(int array[], int size)

{

  int indexOfNextSmallest;

  for (int index = 0; index < size - 1; index++)

  {

    indexOfNextSmallest = indexOfSmallest(array, index, size);

    swapValues(array[index], array[indexOfNextSmallest]);

  }

}

const int SIZE = 5;

/********************************************************                    

* Function: doIntList                                                       

* Description: This function prompts the user for a list                    

*   of integers and then passes it to the sort function                     

*   above and finally displays it.                                          

*                                                                           

* You should not need to change this function.                              

********************************************************/

void doIntList()

{

  // You should not need to change this function                            

  int list[SIZE];

  cout << "Please enter " << SIZE << " ints. ";

  // prompt for the values                                                  

  for (int i = 0; i < SIZE; i++)

  {

    cout << "Enter value " << i + 1 << ": ";

    cin >> list[i];

  }

  // now we call the sort function                                          

  sort(list, SIZE);

  // finally we display it:                                                 

  for (int i = 0; i < SIZE; i++)

  {

    cout << list[i] << endl;

  }

  cout << endl;

}

/*******************************************************                     

* Function: main                                                            

* Description: This is the main driver for the program.                     

*   It calls functions to build, sort, and display ints,                    

*   floats, and strings.                                                    

********************************************************/

int main()

{

  doIntList();

  doFloatList();

  doStringList();

  return 0;

}

Instructions 1. The provided code has functions to sort a list. It performs a selection sort which finds smallest value in the list and moves it to the front. Then, it finds the next smallest item, and puts it in the next spot. 2. The complete logic of these functions is provided. You do not need to worry about figuring out how to implement a selection sort. 3. The provided code has three functions that build lists of integers, floats, and strings. The first of these works as is, but the floats and strings do not work yet, because the sort functions only work with integers. Your task is to convert them to function templates so they work for any data type 4. Begin by copying the starting file: /home/cs165new/check11a.cpp. This function contains the following functions swapValues-Currently swaps two integers. You need to change it to a function template to swap two of any kind of indexofSmallest- Currently finds the smallest value in a certain range of a list of integers. You need to change it to a function template that can find the smallest value in a list of any type sort -Currently takes a list of integers and calls the functions above to sort it. You need to change this to a function template so it can sort a list of any type - - doIntList - You do not need to change this. It creates a list of integers and passes it to the sort function and then displays the resulting list. doFloatlist -You do not need to change this. It builds, sorts, and displays a list of floats. This function currently does not work because there is no sort function for floats. Once you convert the other functions to functions templates, this will work correctly. doStringList -You do not need to change this. It is the same as the doFloatList function, except for strings main -You do not need to change this. It simply calls the functions above 5. Just a reminder. All you should need to to for this assignment is convert those three functions to function templates. This means putting the template line of code right before the function and changing some of the data types to "T" rather than int. Keep in mind, that not all the ints in those functions need to change to T's. The variables that hold indexes and sizes will stay integers, only the ones that refer to data should change to Ts

Explanation / Answer


Given below is the completed code for the question. In your code, I did not find the doFloatList() and doStringList() functions. So I added them.
Please do rate the answer if it was helpful. Thank you


#include <iostream>
#include <string>

using namespace std;

/**********************************************************

* Function: swapValues

* Description: Swaps two values

**********************************************************/
template <typename T>
void swapValues(T &v1,T &v2)
{

T temp;
temp = v1;
v1 = v2;
v2 = temp;
}

/**********************************************************
* Function: indexOfSmallest
* Description: Finds the smallest value in the array
* and returns its index.

**********************************************************/
template <typename T>
int indexOfSmallest(const T array[], int startIndex, int size)
{
T min = array[startIndex];
int indexOfMin = startIndex;

for (int index = startIndex + 1; index < size; index++)
{
if (array[index] < min)
{
min = array[index];
indexOfMin = index;
}
}

return indexOfMin;
}

/***********************************************************
* Function: sort
* Description: Uses a selection sort to sort the array.
*
* Source: Savitch, Absolute C++ 5th Edition, pp. 216-217
***********************************************************/
template <typename T>
void sort(T array[], int size)
{

int indexOfNextSmallest;

for (int index = 0; index < size - 1; index++)
{
indexOfNextSmallest = indexOfSmallest(array, index, size);
swapValues(array[index], array[indexOfNextSmallest]);
}
}

const int SIZE = 5;

/********************************************************
* Function: doIntList
* Description: This function prompts the user for a list
* of integers and then passes it to the sort function
* above and finally displays it.
*
* You should not need to change this function.
********************************************************/

void doIntList()
{

// You should not need to change this function
int list[SIZE];
cout << "Please enter " << SIZE << " ints. ";

// prompt for the values

for (int i = 0; i < SIZE; i++)

{

cout << "Enter value " << i + 1 << ": ";

cin >> list[i];

}

// now we call the sort function

sort(list, SIZE);

// finally we display it:

for (int i = 0; i < SIZE; i++)

{

cout << list[i] << endl;

}

cout << endl;

}


/********************************************************
* Function: doFloatList
* Description: This function prompts the user for a list
* of floating point numbers and then passes it to the sort function
* above and finally displays it.
*
* You should not need to change this function.
********************************************************/

void doFloatList()
{

// You should not need to change this function
float list[SIZE];
cout << "Please enter " << SIZE << " floats. ";

// prompt for the values

for (int i = 0; i < SIZE; i++)

{

cout << "Enter value " << i + 1 << ": ";

cin >> list[i];

}

// now we call the sort function

sort(list, SIZE);

// finally we display it:

for (int i = 0; i < SIZE; i++)

{

cout << list[i] << endl;

}

cout << endl;

}

/********************************************************
* Function: doIntList
* Description: This function prompts the user for a list
* of integers and then passes it to the sort function
* above and finally displays it.
*
* You should not need to change this function.
********************************************************/

void doStringList()
{

// You should not need to change this function
string list[SIZE];
cout << "Please enter " << SIZE << " strings. ";

// prompt for the values

for (int i = 0; i < SIZE; i++)

{

cout << "Enter value " << i + 1 << ": ";

cin >> list[i];

}

// now we call the sort function

sort(list, SIZE);

// finally we display it:

for (int i = 0; i < SIZE; i++)

{

cout << list[i] << endl;

}

cout << endl;

}

/*******************************************************

* Function: main

* Description: This is the main driver for the program.

* It calls functions to build, sort, and display ints,

* floats, and strings.

********************************************************/

int main()

{

doIntList();

doFloatList();

doStringList();

return 0;

}