Search code examples
c++functionvectorselection-sort

What variable name should I pass to my function for sorting?


Edit: I realized that one of my problems was that I was trying to get my sorting function to sort a string, which obviously wouldn't work.

I need to sort data from a file that I organized into two vectors. So I am using a function that will sort my data in the order I need, however, I can't figure out what variables I should have in the () of the call and of the function definition. I know I need to pass names and score in order for the function to sort it, but I don't know if I need to say (string name, int score) or (vector, vector

//Name
//This program will read and sort names and grades from a file using functions and vectors
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;

//Function prototype
void selectionSort(vector<int>& vector_values);

int main()
{
    ifstream infile;
    infile.open("student.txt");

    if (infile.fail() == false)
    {
        vector<string> all_names;
        vector<int> all_scores;
        string name;
        int score;
        while (infile >> name >> score) // read one name and one score
        {
            all_names.push_back(name); // add that name to vector
            all_scores.push_back(score); // add that score to vector
            selectionSort();
            cout << name << " "<< score<<", ";
        }

    }
    else
    {
        cout << "Could not open the file." << endl;
    }
return 0;
}

void selectionSort(vector<int>& vector_values)
{
    for (unsigned pass = 0; pass < vector_values.size(); pass++)
    {
        int minimum = vector_values[pass];
        int minimum_index = pass;
        for (unsigned index = pass + 1; index < vector_values.size(); index++)
        {
            if (minimum > vector_values[index])
            {
                minimum = vector_values[index];
                minimum_index = index;
            }
        }

        int temp = vector_values[minimum_index];
        vector_values[minimum_index] = vector_values[pass];
        vector_values[pass] = temp;
    }
}


Solution

  • You need to read all the values first and then sort them after the loop.

    The sort function is declared as:

    void selectionSort(vector<int>& vector_values);
    //                 ~~~~~~~~~~~~
    

    which means that it can accept an std::vector<int> by reference. So, with reference to your code, you need to pass all_scores because it is of the same type that is required by the sort function.

    So, your code would look like this:

    // Read values into the vectors
    while (infile >> name >> score)
    {
        all_names.push_back(name);
        all_scores.push_back(score);
    }
    
    // Values are populated, now sort here
    selectionSort( all_scores );
    

    If you are required to use an std::vector and also want to retain the relationship between the names and their associated scores, an alternative solution could be an std::vector<std::pair<std::string, int>> or an std::vector of an aggregate type i.e. struct or class.

    With two vectors' restrictions, you need to pass both to the sort function; and, swap names when you swap scores to retain their relationship.

    For future reference, you might want to look at the associative containers such as std::map to approach such problems.