C++ SORTING – INSERTION SORT METHOD Use the below Text File and write code that
ID: 3818426 • Letter: C
Question
C++ SORTING – INSERTION SORT METHOD
Use the below Text File and write code that sorts it based on the users sort method selection. Please provide a separate .cpp file wit hthe code containing the sort method. The sorting method uses the text file below and sorts it accordingly. Seperate the sorting method into an additional C++ file.
*********************************
Text File
Below is a text file called classes.txt. This text file lists, by course number and section number a series of computer science classes. This is the class number followed by a space and then the section number for that class (ex: 801 1203)
The contents of the file are:
//Class Number Followed by Respective Class Section #
801 1203
801 7023
801 3108
802 1205
802 1206
802 3110
816 3113
830 7011
832 7038
834 3115
The Purpose of the program is to use the sorting method selected by the user to sort the above list by Section number (ex: 1203). This is the 4 digit number after the class number. IT should list the sort with the 4 digit number first followed by the 3 digit class number like the example below.
*********************************
How the Program Works
1. The program first displays a menu prompting the user to choose one of the following two sort method or exit (you can assume the user will choose a valid menu choice):
A. Insertion
B. Exit
2. If the user chooses Exit, the program ends. Otherwise, the program loads the classes.txt text file provided above.
3. The program then sorts and displays the courses in ascending order of section number using the method chosen by the user. Just an example of a display below.
//Example Output – Ascending Section # followed by respective Class #
1203 801
1205 802
1206 802
3108 801
3110 802
3113 816
3115 834
7011 830
7023 801
7038 832
4. The program re-displays the menu in step #1.
*********************************
Files
In addition to the driver/main file, put the above sort method in a separate file. For example, for insertion sort, there should be 1 .cpp file so on so forth. Should be a total of 2 files. 1 for the sort method and another for the main driver file.
Explanation / Answer
main.cpp
#include <iostream>
#include <fstream>
#include "sort.h"
using namespace std;
void sortOrExit()
{
struct classdata readClass[1000] ;
cout << "Please choose from given menu: " << endl;
cout << "A. Insertion" << endl;
cout << "B. Exit" << endl;
char choice;
cin >> choice;
if (choice == 'B')
{
return;
}
fstream myfile("classes.txt") ;
int i = 0;
while((myfile >> readClass[i].classNumber >> readClass[i].sectionNumber) && (i < 1000))
{
i++;
}
int size = i;
sort(readClass, size);
cout << "Sorted data: "<< endl;
for(int i = 0; i < size; i++)
{
cout << readClass[i].sectionNumber << " "<< readClass[i].classNumber << endl;
}
sortOrExit();
}
int main()
{
sortOrExit();
return 0;
}
sort.h
#ifndef __MY_HEADER__
#define __MY_HEADER__
struct classdata
{
int classNumber;
int sectionNumber;
};
// main function to do heap sort
void sort(struct classdata arr[], int n);
#endif
insertionSort.cpp
// C++ program for implementation of Heap Sort
#include <iostream>
#include "sort.h"
using namespace std;
void swap(struct classdata *p1, struct classdata *p2)
{
struct classdata temp = *p1;
*p1 = *p2;
*p2 = temp;
}
/* Function to sort an array using insertion sort*/
void sort(struct classdata arr[], int n)
{
int i, j;
struct classdata key;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j].sectionNumber > key.sectionNumber)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
compile using g++ main.cpp sort.h insertionSort.cpp
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.