Write a program, called sort_file, which takes a text file as input and produces
ID: 3668154 • Letter: W
Question
Write a program, called sort_file, which takes a text file as input and produces an output file which has all the original lines in alphabetical order. The program should do the following: Prompt the user for the names of the input and output files. You can prepare an input file with appropriate unsorted data. A simple file with one word per line will suffice. Declare and define three functions to perform the following tasks: Read the data from the input file into an array. Sort the array using the bubble sort algorithm. Write the sorted data array to the output file.
Explanation / Answer
// header files
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std ;
/* Prototypes.
-----------------------------------------------------------------------------*/
void Read (string[], int&) ;
void Swap (int&, int&) ;
void BubbleSort (string[], int) ;
void Output (string[], int) ;
const int SIZE = 50 ;
int main() {
string Names[SIZE] ;
int n ;
/* Read The Names from the file.
-----------------------------------------------------------------------------*/
Read(Names, n) ;
/* Display the original array of names.
-----------------------------------------------------------------------------*/
cout << " <<< Elements of the Array >>> "
<< "------------------------------------- " ;
Output(Names, n) ;
/* Display the ordered array of names.
-----------------------------------------------------------------------------*/
BubbleSort(Names, n) ;
Output(Names, n) ;
return EXIT_SUCCESS ;
}
/* Read Definition.
-----------------------------------------------------------------------------*/
void Read(string Names[], int& n) {
ifstream Ifile ;
string filename ;
// File name input. ----------------------------------------------------------*
do {
Ifile.clear() ;
cout << " Enter the input filename ---> " ;
cin >> filename ;
Ifile.open(filename.c_str()) ;
// Input validation for file. ------------------------------------------------*
if (Ifile.fail()) {
cout << "The file '" << filename << "' does not exist! " ;
}
} while (Ifile.fail());
// Transfer file contents. ---------------------------------------------------*
n = 0 ;
while (Ifile.good()) {
getline(Ifile, Names[n]) ;
n++ ;
}
Ifile.close() ;
return ;
}
/* Swap Definition.
-----------------------------------------------------------------------------*/
void Swap(string& x, string& y) {
string temp = x ;
x = y ;
y = temp ;
}
/* Bubble Definition.
-----------------------------------------------------------------------------*/
void BubbleSort(string Names[], int n) {
bool done = false ;
for (int pass = 1; pass < n && !done; pass++) {
for (int compa = 0; compa < (n - pass); compa++) {
if (Names[compa] > Names[compa + 1]) {
Swap(Names[compa], Names[compa + 1]) ;
done = false ;
}
}
}
cout << " <<< Elements of the Array ( ORDERED ) >>> "
<< "-------------------------------------- " ;
}
/* Output Definition.
-----------------------------------------------------------------------------*/
void Output(string Names[], int n) {
ofstream Ofile ;
Ofile.open("results.txt") ;
Ofile << " <<< Elements of the Array ( ORDERED ) >>> "
<< "------------------------------------------------------- " ;
for (int i = 0 ; i < n ; ++i) {
cout << setw(3) << (i+1) << ") "
<< setw(6) << Names[i] << endl ;
Ofile << setw(3) << (i+1) << ") "
<< setw(6) << Names[i] << endl ;
}
Ofile.close() ;
}
data.txt
Marcus Mcgee
Dave Banks
Dana Howell
Domingo Ortega
Toni Valdez
Vernon Peters
Nathaniel Cain
Malcolm Bass
Omar Ramsey
Winifred Parker
Marianne Young
Bob Silva
Alton Pratt
Lana Gomez
Amanda Andrews
Shaun Hughes
Shepard Stone
Fred Fintstone
Cosmos Jones
Oscar Wilde
sample output
Enter the input filename ---> data.txt
<<< Elements of the Array >>>
-------------------------------------
1) Marcus Mcgee
2) Dave Banks
3) Dana Howell
4) Domingo Ortega
5) Toni Valdez
6) Vernon Peters
7) Nathaniel Cain
8) Malcolm Bass
9) Omar Ramsey
<<< Elements of the Array ( ORDERED ) >>>
--------------------------------------
1) Alton Pratt
2) Amanda Andrews
3) Bob Silva
4) Cosmos Jones
5) Dana Howell
6) Dave Banks
7) Domingo Ortega
8) Fred Fintstone
9) Lana Gomez
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.