Question

In: Computer Science

Selection sort on fstream data C++: Sort the given student information data inside a text file...

Selection sort on fstream data C++:

Sort the given student information data inside a text file

The format of the student data inside the text file is:

Student Number, Surname, First Name, Middle Name, Birthday, Course, Year, School Year

(The list can be updated over time so there is no limit for the size of the data)

But I need to automatically sort the given the data in ascending alphabetical order based on their surnames when displaying the list. I don't know how, please help me. Thank you :)

Solutions

Expert Solution

Here is the answer for your question in C++ Programming language.

What this code does?

1. Here I have given you a code that reads a file "student.txt" which contains columns as you specified.

2.Stores the surname column in a vector (as you mentioned size can be anything I used vectors)

3. Sorts the surnames column in alphabetical order and returns the vector.

4. Searches for every surname from starting in the vector with the lines in the file from starting and if founds any match displays the particular line of the file.

5. Hence the names which are in alphabetical order in the vector are used to display their lines associated with them.

#############################################################################################

CODE:(cpp file)

//Including required header files
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include <bits/stdc++.h>

//using standard namespace
using namespace std;


//function that compares the alphabetical order of the string
bool compare(string word1,string word2)
{
   return word1<word2;
}
//method that starts sorting and takes each word in the vector as input
vector<string> alphasort(vector<string> word)
{
   int n = word.size();
   //sort method that actually does sorting
   sort(word.begin(),word.end(),compare);
   //returning the particular word
   return word;
}
//main method where compilation starts
int main()
{
   //String variable to store data of each column in text file except surname
   //These are used for just storage while reading file and we no more use them again in the program
   string sno , first , middle , bday , course , year ,schl_year;
  
   //Vector to store the surnames column in the file
   vector<string> surnames;
   //variables to store lines of the file and to store the surnames
   string line , name;
   //Counters are used as eof() function reads last line twice
   //To avod that counters are used counter 1 counts number of lines
   //Counter 2 takes the loop to repeat through the same number of lines
  
   int counter1 =0 , counter2 = 0;
  
  
   size_t position;
   //Object of ifstream
   ifstream Input ;
   //filename of the string
   string filename = "birthday.txt";
   //Opening the file
   Input.open(filename.c_str());
   //While lop to calculate number of lines in the file
   while(getline(Input,line))
   {
       counter1++;
   }
   //After while loop as the pointer lies at end we have to close and reopen the file to start from beginning again
   Input.close();
   Input.open(filename.c_str());
   //Second while loop that iterates through each word of the file
   while(!Input.eof())
   {
       //Each word in every line is being stored in the respected variable name
       Input >> sno >> name >> first >> middle >> bday >> course >> year >> schl_year;
       //Pushig=ng only surnames to vector as they are only to be sorted
       surnames.push_back(name);
       //To avoid while loop to repeat last line twice
       if(counter2==(counter1-1))
       {
           break;
       }
       counter2++;
   }
  
   Input.close();
   Input.open(filename.c_str());
   //calling alphasort function ans replacing the original vector with sorted vector
   surnames = alphasort(surnames);
   //Printing statements for understanding
   cout << "StudentNo " << " Surname " <<" FirstName " <<" Middle " <<" BDay " <<" Course " << " Year " <<" School "<< endl;
   cout << "---------------------------------------------------------------------------------------"<<endl;
   //loop that iterates throuugh the size of the vector
   for(int i =0;i<surnames.size();i++)
   {
       //At each iteration it takes current surname in vector
       //while loop to iterate through the lines of file and reading every line each time in string line
       while(getline(Input,line))
       {
           //The current surname is checked to be in currnet line of the file
           position = line.find(surnames[i]);
           //if yes prints the line
           if(position!=string::npos)
           {
               cout << line << endl;
           }
           //if not checks the next line of the file
       }
       //For every while loop iteration the file needs to be closed and opened as every iteration ends at the end of the file
   Input.close();
   Input.open(filename.c_str());
  
   }
   return 0;
}

student.txt

1 Johnes Fedrick Richard 06 CSE 2019 2013
2 Nicholas Rathon Freck 18 EEE 2018 2012
3 Coprus Jackson Rone 23 ECE 2016 2010
4 Hindle Ackson John 21 CSE 1997 1991
5 James Rathod Zafer 30 EEE 1998 1992
6 Hadrick Nelson Koman 15 CSE 2000 1994

SCREENSHOTS

Please see the screenshots for the indentations of the code.

If below is the text file (Example values taken )

Then the OUTPUT will be (sorting surnames in alphabetical order)

If you want to store the lines in the same order you can store it in another file and use them for further process.


Related Solutions

This is c++ code. Create a file sort.cpp. to mix functions with the selection sort algorithm:...
This is c++ code. Create a file sort.cpp. to mix functions with the selection sort algorithm: ·Write a function int least(vector<string> strs, int start)to return the index of the smallest value in the vector. You are to assume there is at least one value in the vector. ·Write a function void selsort(vector<string> & strs) to use selection sort to sort the vector of strings. It is a worthwhile experiment to try leaving out the & and seeing that the vector...
Given some data in a text file, the task is to scramble the text and output...
Given some data in a text file, the task is to scramble the text and output in a separate text file. So, we need to write a Python program that reads a text file, scrambles the words in the file and writes the output to a new text file. Rules to be followed: Words less than or equal to 3 characters need not be scrambled. Don’t scramble first and last char, so Scrambling can become Srbmnacilg or Srbmnailcg or Snmbracilg,...
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
The C++ problem: Student marks are kept in a text file as a single column. Each...
The C++ problem: Student marks are kept in a text file as a single column. Each student may have a different number of assessments and therefore scores. The data recorded in the file for each student start with the number of scores for the student. This is followed by the student id and then several marks student scored in various assessments, one score per line. A small segment of the file might look like the following: (file name is marks.txt)...
C++ Data Structures: Use Huffman coding to encode text in given file (Pride_and_Prejudice.txt). TO_DO: Define a...
C++ Data Structures: Use Huffman coding to encode text in given file (Pride_and_Prejudice.txt). TO_DO: Define a struct for Huffman tree node. This struct contains links to left/right child nodes, a character, and its frequency.Define a function for file reading operation. This function should take in a filename (string type) as parameter and return a proper data structure object that contains characters and their frequencies that will be used to generate Huffman tree nodes.The construction of Huffman tree requires taking two...
Develop a C++ program that looks for a given value in a text file full of...
Develop a C++ program that looks for a given value in a text file full of integer values Prompt the user for a value to search for in the file No input validation is required Open the accompanying text file named numbers.txt Search the contents of the text file You may not "hard-code" the quantity of values found in the file... Use a loop of some sort until the end of the text file is reached Maintain how many many...
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort,...
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort, or bubble sort) and one faster sort of Big O(n * log n) (mergesort or quicksort). Count the number of moves (a swap counts as one move). With mergesort, you can count the range of the part of the array you are sorting (i.e. last-first+1). Use the code from the textbook (copy from the lecture notes) and put in an extra reference parameter for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football...
In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football League list of current players. It's going to be a big list(over 1000 names). Please identify, in comments, which part of the code is for the Selection Sort and which part of the code is for Insertion Sort. It must have both. Inputting data from a text file named "Players.txt" Only want the name of the player(first name then last name), their team name,...
c++ For your program, you will choose either the Selection Sort algorithm or the Insertion Sort...
c++ For your program, you will choose either the Selection Sort algorithm or the Insertion Sort algorithm and create a recursive implementation of that algorithm. Your program should: Randomly generate an array of at least 20 values. Display the contents of that (unsorted) array. Use the recursive implementation of either Selection or Insertion Sort to sort the values. Display the contents of the now sorted array, to demonstrate the success of the algorithm.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT