Problem statement: Create a program, using the bubble sort algorithm, to sort an
ID: 3770923 • Letter: P
Question
Problem statement:
Create a program, using the bubble sort algorithm, to sort an array of integers
Code:
* Must use only the bubble sort
* Modify it to stop as soon as the array is sorted.
* The array is sorted if there are no swaps on a pass through an array
* Prompt user for the fully qualified name of the file. A sample file is
attached ( see file num.txt containing 500 integers. )
* The sorted array must be printed to a file, user choses name, with a single
integer on each line.
* The last line of the output file must contain the number of times integer
values were swapped.
* open any file requested
* use follwing functions: - get_input_fileName, get_output_file_Name,
bubble_sort_method, display_sorted array.
You are free to make additional functions for your code.
You are to create a project/file for this lab named Lab10 for the project name and lab10_driver.cpp for the driver.
A sample text file is provided ( “num.txt” ). Check email attachment to see the file your program will read. Download and copy the sample text file in your hard drive.
Open the file in a function – error check
num.txt
15437 5579 30403 20723 15051 1275 31856 3192 20446 26400
2346 1804 20246 10454 24880 5574 5060 26748 22640 28586
8296 26589 3486 19567 21101 16655 23428 24710 32276 25244
29849 23127 1711 5856 23764 17614 18191 6834 13762 29462
24127 1863 4311 22948 13427 4946 15362 30840 19515 28528
15491 15007 13308 16836 27649 28413 11169 1246 27607 15945
21134 19938 25264 16985 29141 15846 14419 8864 13997 8859
17344 23029 28960 2608 8059 2453 11011 20083 6184 32108
11051 23354 26968 17808 14558 13313 15523 2319 13192 31956
7927 30186 21167 2672 24623 29281 8745 15781 4327 29553
3725 11774 4751 7324 5607 26532 28095 3304 28848 16512
25290 32042 18173 2208 26774 4996 17910 6620 3499 4141
10634 2745 28553 14192 3681 2189 15423 7416 8210 7396
29241 13386 30615 25123 21082 19091 15870 27499 14411 19876
26464 25536 19730 1083 6679 10347 18406 7044 5676 25244
12609 11743 12559 25684 23608 9956 14731 19586 2568 10404
22520 13933 27633 22066 16051 30801 30535 10083 16075 13645
16326 10705 5373 18044 19494 21286 739 16227 6383 18583
2152 4491 15759 16302 20863 4974 26537 23629 11285 28312
8369 12272 6723 14598 4534 15922 21705 15826 14718 10899
20920 14610 18648 29560 9070 2813 6446 24179 17351 9807
29018 31173 8082 15855 3258 22305 30274 30159 6279 25523
25062 6932 7010 353 7004 10104 31536 17745 20161 5100
5416 32683 5163 16001 7210 28832 24731 26504 12627 20139
813 2552 27342 534 658 4881 15597 14785 28352 15679
31096 15647 31464 4673 22853 8065 29609 22666 17416 14487
25987 28648 19053 1587 1934 24931 1636 23377 15980 9832
21948 85 19324 27431 8948 10387 31824 20537 12366 24852
28258 28550 6436 23703 15106 10342 32127 16599 8083 30470
3046 22796 30199 23310 23924 20916 4424 4480 22574 24979
21193 9699 2963 1017 2844 7842 15220 10307 31669 18607
3333 31396 3214 26219 2613 10790 2588 17866 28475 20459
32643 434 30321 23481 262 29349 5734 10678 31425 4768
26158 9583 22426 22595 7165 7711 5237 19761 23029 12335
5031 25530 15675 26617 17581 20408 25424 465 29727 26294
22943 27462 22495 19288 11090 27045 11158 26615 362 2495
29025 31883 2319 15596 10951 17358 5156 18256 7663 29598
10271 25437 31503 7331 28440 16025 30248 11571 16159 13218
10481 7026 11219 25967 20752 31422 22065 8152 16016 74
28329 8537 1680 15112 7543 12819 20024 717 17712 11283
16882 25264 21403 22743 3286 691 5315 18446 25701 21669
15434 928 31867 2118 6400 17646 30591 2733 9908 3931
9949 5351 29259 21515 12214 3553 1039 747 26582 17287
17481 11505 32486 16174 3601 7077 18402 14639 25701 480
14030 28629 32121 10707 26277 25978 6222 17484 15251 18924
20614 15669 30051 4886 25719 14685 17979 2268 8574 8572
16775 18502 27285 24177 14131 481 3368 10957 20253 30562
22565 7573 30922 31885 10579 21060 856 32597 25828 21008
29107 23365 2893 8814 24332 31774 24917 7384 1927 5058
24172 29646 18740 26448 21823 2331 12609 30609 2697 31065
C++
Explanation / Answer
lab10_driver.cpp
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
char* get_input_fileName();
char* get_output_file_Name();
long bubble_sort_method(long myArray[]);
void display_sorted_array(long myArray[]);
int main(){
long myArray[500];
int i,x;
long swapCount;
char* inpFile = new char[50];
char* outFile = new char[50];
/*Read From Input File*/
inpFile = get_input_fileName();
ifstream myFile;
myFile.open(inpFile);
i=0;
if(myFile.is_open()){
while(myFile >> x){
myArray[i++] = x;
}
myFile.close();
}
else{
cout << "Can't open the specified file: " << inpFile << endl;
exit(1);
}
/*Sort*/
swapCount = bubble_sort_method(myArray);
/*Display*/
display_sorted_array(myArray);
/*Write to output File*/
outFile = get_output_file_Name();
ofstream myOutputFile;
myOutputFile.open(outFile);
if(myOutputFile.is_open()){
for(int i=0; i<500; i++){
myOutputFile << myArray[i] << endl;
}
myOutputFile << swapCount;
myOutputFile.close();
}
else{
cout << "Can't create a file name" << outFile << endl;
exit(1);
}
return 0;
}
char* get_input_fileName(){
char* name = new char[500];
cout << "Enter a fully qualified file name to take input from: ";
cin >> name;
return name;
}
char* get_output_file_Name(){
char* name = new char[500];
cout << endl;
cout << "Enter a fully qualified file name to put output: ";
cin >> name;
return name;
}
void display_sorted_array(long myArray[]){
int i;
cout << endl;
for(int i=0; i<500; i++){
cout << myArray[i] << " ";
}
}
long bubble_sort_method(long myArray[]){
int i,x,swapCount;
bool swap;
long temp;
swap=false;
swapCount = 0;
do{
swap=false;
for(int i=0; i<500; i++){
if(myArray[i] > myArray[i+1]){
temp = myArray[i+1];
myArray[i+1] = myArray[i];
myArray[i] = temp;
swap=true;
swapCount++;
}
}
}while(swap==true);
return swapCount;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.