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

C++ Template class sort from txt data Create a text file called classes.txt . Th

ID: 3811372 • Letter: C

Question

C++ Template class sort from txt data

Create a text file called classes.txt. This text file lists, by course number and section number. 10

The contents of the file could state:

801    7045   
801    1206   
801    3450   
802    1688   
802    3285   
808    3278   
(and so on)

Note that the courses are listed by course number. You can also list them in random order. However, do not list them by section number. That is what your program will do.

How the Program Works

The program first displays a menu prompting the user to choose one of the six sort methods covered in Section 10.1 of the book, or exit (you can assume the user will choose a valid menu choice):

A.Selection

B.Bubble

C.Insertion

D.Heap

E.Quick

F.Merge

G.Exit

If the user chooses Exit, the program ends. Otherwise, the program loads the classes.txt text file which you created.

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:

1121    839
1321    802     
3112    870  
3278    808
3285    802
3450    801
7045    801      

The program re-displays the menu in step #1.

Files

In addition to your driver file, put each of the 6 sort methods in a separate file or files. For example, for merge sort, you can have 1 .cpp file, or split that code into an .h and a .cpp file. Therefore, you'll have at least 7 code files, and possibly as many as 13. You also will have your classes.txt text file.

Explanation / Answer

Answer

main.cpp

#include <iostream>
#include <fstream>
#include "selection.h"
#include "bubble.h"
#include "insertion.h"
#include "heap.h"
#include "quick.h"
#include "merge.h"
using namespace std;
int course[1000],section[1000];
void display(int count)
{
int i;
for(i=0;i<count;i++)
{
cout<<" "<<section[i]<<" "<<course[i];
}
}
int main()
{
ifstream in_s;
char ch;
do
{
cout<<" -----------------SORT MENU--------------------";
cout<<" A. Selection Sort";
cout<<" B. Bubble Sort";
cout<<" C. Insertion Sort";
cout<<" D. Heap Sort";
cout<<" E. Quick Sort";
cout<<" F. Merge Sort";
cout<<" G. Exit";
cout<<" -> Enter Choice : ";
cin>>ch;
if(ch=='A'||ch=='a')
{
in_s.open("classes.txt");
static int count=0;
if(in_s.fail())
{
cout<<"Input file failed to open ";
}
else
{
while(!in_s.eof())
{
in_s>>course[count];
in_s>>section[count];
count++;   
}
cout<<" -------------CLASSES DATA Before Sort---------------";
display(count);
selectionSort(course,section,count);
cout<<" -------------CLASSES DATA After Sort---------------";
display(count);
}
in_s.close();
}
else if(ch=='B'||ch=='b')
{
in_s.open("classes.txt");
static int count=0;
if(in_s.fail())
{
cout<<"Input file failed to open ";
}
else
{
while(!in_s.eof())
{
in_s>>course[count];
in_s>>section[count];
count++;   
}
cout<<" -------------CLASSES DATA Before Sort---------------";
display(count);
bubbleSort(course,section,count);
cout<<" -------------CLASSES DATA After Sort---------------";
display(count);
}
in_s.close();
}
else if(ch=='C'||ch=='c')
{
in_s.open("classes.txt");
static int count=0;
if(in_s.fail())
{
cout<<"Input file failed to open ";
}
else
{
while(!in_s.eof())
{
in_s>>course[count];
in_s>>section[count];
count++;   
}
cout<<" -------------CLASSES DATA Before Sort---------------";
display(count);
insertionSort(course,section,count);
cout<<" -------------CLASSES DATA After Sort---------------";
display(count);
}
in_s.close();
}
else if(ch=='D'||ch=='d')
{
in_s.open("classes.txt");
static int count=0;
if(in_s.fail())
{
cout<<"Input file failed to open ";
}
else
{
while(!in_s.eof())
{
in_s>>course[count];
in_s>>section[count];
count++;   
}
cout<<" -------------CLASSES DATA Before Sort---------------";
display(count);
//heapSort(course,section,count);
cout<<" -------------CLASSES DATA After Sort---------------";
display(count);
}
in_s.close();
}
else if(ch=='E'||ch=='e')
{
in_s.open("classes.txt");
static int count=0;
if(in_s.fail())
{
cout<<"Input file failed to open ";
}
else
{
while(!in_s.eof())
{
in_s>>course[count];
in_s>>section[count];
count++;   
}
cout<<" -------------CLASSES DATA Before Sort---------------";
display(count);
//quickSort(course,section,count);
cout<<" -------------CLASSES DATA After Sort---------------";
display(count);
}
in_s.close();
}
else if(ch=='F'||ch=='f')
{
in_s.open("classes.txt");
static int count=0;
if(in_s.fail())
{
cout<<"Input file failed to open ";
}
else
{
while(!in_s.eof())
{
in_s>>course[count];
in_s>>section[count];
count++;   
}
cout<<" -------------CLASSES DATA Before Sort---------------";
display(count);
//mergeSort(course,section,count);
cout<<" -------------CLASSES DATA After Sort---------------";
display(count);
}
in_s.close();
}
else if(ch=='G'||ch=='g')
{
cout<<" Exiting from program..!";
}
else
{
cout<<" Incorrect choice..! Try again";
}
}while(ch!='G'&&ch!='g');
cout<<endl;
return 0;
}

classes.txt

801 7045
801 1206
801 3450
802 1688
802 3285
808 3278

selection.h

void selectionSort(int course[],int section[],int count)
{
int i,j,pos_min,temp;//pos_min is short for position of min
for (i=0; i < count-1; i++)
{
pos_min = i;//set pos_min to the current index of array
for (j=i+1; j < count; j++)
{
if (section[j] < section[pos_min])
pos_min=j;
//pos_min will keep track of the index that min is in, this is needed when a swap happens
}
//if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
if (pos_min != i)
{
int temp = section[i];
section[i] = section[pos_min];
section[pos_min] = temp;
  
temp = course[i];
course[i] = course[pos_min];
course[pos_min] = temp;
}
}
}

bubble.h

void bubbleSort(int course[],int section[],int count)
{
int p,j;
for(p=0;p<count-1;p++) // Loop for Pass
{
for(j=0;j<count-1;j++)
{
if(section[j]>section[j+1])
{
int temp=section[j]; // Interchange Values
section[j]=section[j+1];
section[j+1]=temp;
  
temp=course[j]; // Interchange Values
course[j]=course[j+1];
course[j+1]=temp;
}
}
}
}

insertion.h

void insertionSort(int course[],int section[],int count)
{
int key1,key2,i,j;
for(j=0;j<count;j++)
{
key1=section[j];
key2=course[j];
i=j-1;
while((i>-1)&&(section[i]>key1))
{
section[i+1]=section[i];
course[i+1]=course[i];
i=i-1;
}
section[i+1]=key1;
course[i+1]=key2;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote