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

Output: James Q2Write a C++ program that reads a list of words from a text file

ID: 3710598 • Letter: O

Question

Output:

James

Q2Write a C++ program that reads a list of words from a text file into a vector of strings, sorts the words in descending alphabetical order, and then writes the sorted list to another text file. Assume that both the input and output files are formatted with only one word on each line. An example file input/file output pair is provided below: Input file: Output file: Jim Ted Ted Raachel Paula Paula Helen Jim Rachel Helen Hint: You may use bubble sort (from lecture or selection sort (from homework 3). However, you must rewrite the sorting algorithm to work on a vector of strings. Note: You must write your code in a source file named sort list.cpp. In your code, you must read from a text file named list in.txt and write to a text file named list_out.txt. You are provided an example input text file to test your code. Use the string and vector types that are accessible when you include and , respectively. Use file input and output streams to read from and write to the files (make sure to include ).

Explanation / Answer

The required code is provided below. Loaded a list of words from list_in.txt to a vector, sorted it using bubble sort algorithm in descending order, and saved the sorted list in list_out.txt file. Thanks

//sort_list.cpp

#include<iostream>

#include<fstream>

#include<string>

#include<vector>

using namespace std;

int main(){

                string str;

                char ifname[]="list_in.txt";

                char ofname[]="list_out.txt";

               

                ifstream ist(ifname);

                if(!ist){

                                //file not found/couldnt read

                                cout<<"Input file not found!"<<endl;

                                return 1;

                }

                ofstream ost(ofname);

                vector<string> strv;

                //adding all words to the vector

                while(ist>>str){

                                strv.push_back(str);

                }

                //sorting using bubble sort algorithm

                for(int i=0;i<strv.size();i++){

                                for(int j=0;j<strv.size()-1;j++){

                                                if(strv[j]<strv[j+1]){

                                                                //swapping elements

                                                                string temp=strv[j];

                                                                strv[j]=strv[j+1];

                                                                strv[j+1]=temp;

                                                }

                                }

                }

                //printing the results to output file

                for(int i=0;i<strv.size();i++){

                                ost<<strv[i]<<endl;

                }

                cout<<"Sorted and saved to "<<ofname<<endl;

               

}

//list_in.txt

James

Ann

Jane

Viola

William

Shawn

Vincent

Will

Nick

Matt

Zhang

Wei

//list_out.txt after running the program [output]

Zhang

William

Will

Wei

Viola

Vincent

Shawn

Nick

Matt

Jane

James

Ann

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