Question
C++(Code::Block or Visual Studio) You are given a txt file called word.txt with unordered words. You are asked to write a program that would create
C++(Code::Block or Visual Studio)
You are given a txt file called word.txt with unordered words. You are asked to write a program that would create a new file with the same words but sorted alphabetically.
You must have 4 functions other than main.
1. int getNumLines(const string & inputFName)
This function receives the name of the file, opens it, and returns the number of lines in the file.
2. string* createArr(const string & inputFName, )
This function receives the name of the file and creates an unordered string array in the heap. It returns a pointer to the created array.
3. void insertionSort(string arr[], int size)
This function receives the array and its size and sorts the array in ascending order.
4. void writeToFile(string arr[], int size, const string &outputFName)
This function receives the array, the size, and string. It creates a new file with the words after they are sorted alphabetically.
5. main()
- Prompts the user to enter the names of the two files.
- Calls getNumLines
- Calls createArr
- Calls insertionSort.
- Calls writeToFile.
- Calculates the execution time the insertion sort takes to sort the array.
- Use the following:
clock_t start = clock(); //start timer
//call the insertionSort
double endTime = static_cast(clock() - start) / CLOCKS_PER_SEC; // ending time.
Sample run:
enter the name of the file to read from:words.txt
enter the name of the file to write to after sorting:input.txt
Insertion sort took: 105.567 seconds
Press any key to continue . . .
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started