what inside of list_in.txt is: James Ann Jane Viola William Shawn Vincent Will N
ID: 3820865 • Letter: W
Question
what inside of list_in.txt is:
James
Ann
Jane
Viola
William
Shawn
Vincent
Will
Nick
Jalil
Shuanshuan
Zahra
Matt
Wei
Explanation / Answer
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
void bubbleSort(vector<string> &v) {
int n = v.size();
string temp = "";
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(v[j-1] > v[j]){
temp = v[j-1];
v[j-1] = v[j];
v[j] = temp;
}
}
}
}
int main() {
vector<string> v;
ifstream inputFile;
inputFile.open("list_in.txt");
string word;
if (inputFile.is_open()) {
while (!inputFile.eof()) {
inputFile >> word;
v.push_back(word);
}
}
bubbleSort(v);
ofstream outputFile;
outputFile.open ("list_out.txt");
for(int i=0; i<v.size(); i++){
outputFile <<v[i]<<endl;
}
cout<<"File has been generated"<<endl;
outputFile.close();
inputFile.close();
return 0;
}
Output:
h-4.2$ g++ -std=c++11 -o main *.cpp
sh-4.2$ main
File has been generated
list_out.txt
Ann
Jalil
James
Jane
Matt
Nick
Shawn
Shuanshuan
Vincent
Viola
Wei
Will
William
Zahra
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.