Question

C++ Lab 1. Read in the contents of a text file up to a maximum of...

C++ Lab

1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc.

2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function.

3. Search any item input by user in your sorted list using the Binary Search algorithm implemented in its own function.

4. Use string comparisons as taught in CIS 22A for comparing/ ordering words, i.e. words starting with numbers sort lower than words starting with uppercase letters which are lower than words starting with lowercase letters. So a word appearing once with one set of case is different than its second appearance with a different set of case, e.g. 'Do' and 'do' are not the same. You also do not need to remove non-alphanumeric characters from the words, e.g. '$12.34' is a valid 6-character word.

5. If a word appears twice using exactly the same case, it can be stored twice side-by-side in the array and either index can be returned in the search.

6. Your program will:

  • first ask the user for a location+name from where to read the file and location+name where to save the output file,
  • read the contents into an array, ignoring single character words,
  • sort the contents of the array in alphabetically ascending order and
  • then start a loop to allow the user to search for one or more words in the array - your loop should have an appropriate exit condition. If the word is found, the program should output which array location the word was found in, if not found then it should output an appropriate message.
  • Provide clear prompts as necessary for good user interactivity.

7. Your output should be sent to both screen and an output file concurrently. Screen output should contain the entire user interaction. The file output should contain all the user interaction that went to the screen as well as the listing of the sorted array.

8. User interactivity should be limited to your main, input and/or output functions only - what that means is your cin/cout should only be in those 3 functions.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include <string>

#include <iostream>

#include <fstream>

using namespace std;

void SwapFunc(string * wordArray, int i, int j) {

string word = wordArray[i];

wordArray[i] = wordArray[j];

wordArray[j] = word;

}

void SelectionSort(string * wordArray, int size) {

int ind;

for (int i = 0; i < size - 1; ++i) {

    ind = i;

    for (int j = i; j < size; ++j) {

      if (wordArray[j] < wordArray[ind]) {

        ind = j;

      }

    }

    SwapFunc(wordArray, i, ind);

}

}

void PrintArray(ostream & out, string * wordArray, int size) {

out << "Sorted wordArrayay is: " << endl;

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

    out << wordArray[i] << endl;

}

out << endl;

}

void ReadFile(string fileName, string * wordArray, int & count) {

ifstream in (fileName.c_str());

count = 0;

string word;

while ( in >> word) {

    if (word.length() > 1) {

      wordArray[count++] = word;

    }

} in .close();

}

int BinarySearch(string * wordArray, string word, int min, int max) {

if (min > max) return -1;

int mid = (min + max) / 2;

if (wordArray[mid] == word) {

    return mid;

} else if (wordArray[mid] > word) {

    return BinarySearch(wordArray, word, min, mid - 1);

} else {

    return BinarySearch(wordArray, word, mid + 1, max);

}

}

int main() {

string inputFile, outputFile, wordArray[1024], word;

int count, index;

cout << "Enter input file name: ";

cin >> inputFile;

cout << "Enter ouput file name: ";

cin >> outputFile;

ofstream out(outputFile.c_str());

ReadFile(inputFile, wordArray, count);

SelectionSort(wordArray, count);

PrintArray(out, wordArray, count);

while (true) {

    cout << endl << "Enter a word to search for(-1 to exit): ";

    out << endl << "Enter a word to search for(-1 to exit): ";

    cin >> word;

    out << word << endl;

    if (word == "-1") break;

    index = BinarySearch(wordArray, word, 0, count - 1);

    if (index == -1) {

      cout << word << " is not found" << endl;

      out << word << " is not found" << endl;

    } else {

      cout << word << " is found at index " << index << endl;

      out << word << " is found at index " << index << endl;

    }

}

out.close();

return 0;

}

input.txt:

output.txt:

Output:

Add a comment
Know the answer?
Add Answer to:
C++ Lab 1. Read in the contents of a text file up to a maximum of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT