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

C++ SORTING – BUBBLE SORT METHOD Use the below Text File and write code that sor

ID: 3818422 • Letter: C

Question

C++ SORTING – BUBBLE 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. Bubble

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

//Please copy this program and classes.txt file in same folder after that run program and copy all data in classes.txt file
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
using namespace std;

struct courseSection
{
int course;
int section;
};


void DisplayResult(vector <struct courseSection > &a,int counter)
{
int i;
//struct courseSection temp=a.at(i);

cout<<"course section"<<endl;
for(i=0;i<counter;i++)
{
struct courseSection temp=a.at(i);
cout<<temp.course<<" "<<temp.section<<endl;
}
}

void exitProgram()
{
exit(0);
}
void BubbleSort(vector <struct courseSection > &a,int s)
{
int I,J,Temp;
struct courseSection temp1,temp2;
for(I=0;I<s-1;I++)
{
for(J=0;J<(s-1-I);J++)
{

temp1=a.at(J);
temp2=a.at(J+1);
if(temp1.section>temp2.section)
{
//swapping
a.at(J)=temp2;
a.at(J+1)=temp1;
}
}
}
}


int main()
{
string option;
int firstData,secondData;
vector < struct courseSection > array(20);
ifstream infile("classes.txt");
int counter=0;
while(infile >>firstData>>secondData)
{
// cout<< firstData<<"seconddata="<<secondData<<endl;
struct courseSection temp;
temp.course=firstData;
temp.section=secondData;
array[counter]=temp;

counter++;
}
cout<<array.size();
while(1)
{
cout<<"*******************MENU***************"<<endl;
cout<<"A.BUBBLE"<<endl;
cout<<"B.EXIT"<<endl;
cout<<"C.DisplayResult"<<endl;
cin >>option;
switch(option[0])
{
case 'A':
BubbleSort(array,counter);
break;
case 'B':
exitProgram();
case 'C':
DisplayResult(array,counter);
default :
;   
}


}
return 0;
}

/*
20*******************MENU***************
A.BUBBLE
B.EXIT
C.DisplayResult
C
course section
801 1203
801 7023
801 3108
802 1205
802 1206
802 3110
816 3113
830 7011
832 7038
834 3115
*******************MENU***************
A.BUBBLE
B.EXIT
C.DisplayResult
A
*******************MENU***************
A.BUBBLE
B.EXIT
C.DisplayResult
C
course section
801 1203
802 1205
802 1206
801 3108
802 3110
816 3113
834 3115
830 7011
801 7023
832 7038
*******************MENU***************
A.BUBBLE
B.EXIT
C.DisplayResult


*/

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