-1

DISCLAIMER: I know it's not practical to use two vectors, but that's our assignment.

Here's my prompt:

In this program, we are going to input the name and score of 100 students from a file named student.txt. This file has been provided to you. You have to use two vector variables, one to store the student names, and another to store the student scores. Further, modify the selectionSort function to sort the student information based on the score in ascending order. Finally, display the sorted student information on the screen by using cout. For example, let us assume, the following is the content of the student.txt file (in this case, we have only 4 value pairs).

Jeff 77

Charles 99

Richard 67

Sina 79

Then, the output of the program would be

Charles 99

Sina 79

Jeff 77

Richard 67

Here's my code:

//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



            int count = 0;
            const int max = 1;
            selectionSort(all_scores);
            while (count < max)
            {

                count++;
                cout << name << " " << score << endl;
            }
        }


    }
    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;
    }
}

Here's my problem:

The code compiles just fine and shows the names and their corresponding score, however, it seems that my sorting function is doing absolutely nothing. The names are displaying in the same order they do in the file originally. I have placed the call in every location that I thought it would go, and nothing changed. I am now concerned that the placement of the call isn't the issue, but the entire function.

yellogs
  • 13
  • 3
  • 1
    Does your sort function only taking a single vector not seem strange to you? Also, why are you calling the sort function in a loop that runs once for each of the students? See https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. – eesiraed Apr 30 '20 at 23:02
  • Actually you could have many vectors and still be able to sort using only one vector, and all the other vectors will come out sorted. The trick is to *not* sort the data in the vectors, but to sort a single vector of integers that serve as indices. [See this](https://stackoverflow.com/questions/46382252/sort-array-by-first-item-in-subarray-c/46382976#46382976). Read the answer and the footnote in the answer. Sound familiar? Your assignment falls into the category of "parallel array sorting", and that can be done with a single, auxiliary, index array. – PaulMcKenzie May 01 '20 at 01:36

1 Answers1

0

This statement

cout << name << " " << score << endl;

just prints the two values that you just read. It doesn't matter whether you sorted anything in between.

But you're not sorting the all_names array either. And sorting after adding each item is highly inefficient.

gnasher729
  • 51,477
  • 5
  • 75
  • 98