Question: C++ Question. Problem 1: Read in a file Download a copy of A Modest Proposal by Jonathan Swift from Project Gutenberg as a text file
C++ Question.
Problem 1: Read in a file
Download a copy of A Modest Proposal by Jonathan Swift from Project Gutenberg as a text file
Create a new project with a new CPP file lab.cpp
Put the name of all your team members as comments in the CPP file.
Include the following libraries: vector, iostream, fstream, and string
Your main function will open the file and print out all the words one per line. It can be done as follows.
Create a new in file stream (ifstream) variable
Open the file you want using the file.open("File Name") command
Create a new string current_word to store the words as we read them
Read in each word and print it to the console. You can read words into the string using file >> current_word. If there are no words left, this will return false. You can read all the words in a file by using while(file >> current_word){ ...}
Remember to close your file.
Problem 2: Put the words into a vector
In this step we will revise the code to store the strings into a vector instead of printing them out.
Create a new vector of strings in main.
In your while loop, instead of printing the values use the push_back method of vectors to add the new words to the vector.
Print out the number of words you have in your vector.
Problem 3: Removing Duplicates
We have a problem, a word might have appears multiple times. If the word "cat" appears 100 times, we have it in our vector 100 times.
Create a function bool vector_contains(string s, vector
Update your main function to only add one copy of each word to the vector
Print out the number of words in your vector. (It should be less than in Problem 2.)
Problem 4: Swapping Elements
We need to be able to rearrange the elements if we want to sort the words.
Create a function void swap(vector
A[7]= 9; A[8]=10; swap(A,7,8); cout << A[7] << endl; //Should print 10 cout << A[8] << endl; //Should print 9
Test your code by printing out some values before and after swapping.
Problem 5: Sorting the Items
We now have a vector with all the words we found in the book. We also have a way to swap them. We can now sort the vector. The idea behind this sorting method is to compare two elements that are next to each other and swap them so they are sorted. The algorithm is described below. You will need to figure out how to implement it yourself.
Implement a sort function void sort(vector
For integer i from 1 to the size of the vector do the following:
Set integer j=i
Loop while both j>0 and the element at j-1 is larger than the element at j in the vector. When these are both true, we are looking at valid spaces in the array where the elements are not in order.
Swap the values at j and j-1 (These two elements are now right relative to each other)
Update j to be j-1
Print out the sorted words, one per line.
Problem 6: Output to File
Revise your program so that instead of printing the sorted word list to cout, it prints it to a text file named sorted_words.txt. Remember to close every file you open when you are done with it.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
