Question

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

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//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\t\tsection"<<endl;
for(i=0;i<counter;i++)
{
struct courseSection temp=a.at(i);
cout<<temp.course<<"\t\t"<<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


*/

Add a comment
Know the answer?
Add Answer to:
C++ SORTING – BUBBLE SORT METHOD Use the below Text File and write code that sorts...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements...

    The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable objects in ascending order. In this exercise, you will use the Sorting class to sort several different types of objects. 1. The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to...

  • Write a program in Java according to the following specifications: The program reads a text file...

    Write a program in Java according to the following specifications: The program reads a text file with student records (first name, last name and grade on each line). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "print" - prints the student records (first name, last name, grade). "sortfirst" - sorts the student records by first name. "sortlast" - sorts the student records by last name. "sortgrade" - sorts the...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to...

    I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to guess its because I'm not using the swap function I built. Every time I run it though I get an error that says the following Traceback (most recent call last): File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 146, in <module> main() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 122, in main studentArray.gpaSort() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py",...

  • Using C programming For this project, you have been tasked to read a text file with student grade...

    Using C programming For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th...

  • In C++ First create the two text file given below. Then complete the main that is given. There ar...

    In C++ First create the two text file given below. Then complete the main that is given. There are comments to help you. An output is also given You can assume that the file has numbers in it Create this text file: data.txt (remember blank line at end) Mickey 90 Minnie 85 Goofy 70 Pluto 75 Daisy 63 Donald 80 Create this text file: data0.txt (remember blank line at end) PeterPan 18 Wendy 32 Michael 28 John 21 Nana 12...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • Write a program named text_indexing.c that does the following: Reads text and stores it as one...

    Write a program named text_indexing.c that does the following: Reads text and stores it as one string called text. You can read from a file or from the user. (In my implementation, I read only one paragraph (up to new line) from the user. With this same code, I am able to read data from a file by using input redirection (executable < filename) when I run the program. See sample runs below). You can assume that the text will...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT