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
  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • Write the program in C The following statistics for cities of the Vege country are stored...

    Write the program in C The following statistics for cities of the Vege country are stored in a file: Name: a string of characters less than 15 characters long. Population: an integer value. Area: square miles of type double Write a program that: asks and gets the name of the input file from the user. Read the contents of the file into an array length 10 of structures -ask the user for the name of a city (less than 15...

  • Basic C program Problem: In this assignment, you have to read a text file (in.txt) that...

    Basic C program Problem: In this assignment, you have to read a text file (in.txt) that contains a set of point coordinates (x,y). The first line of the file contains the number of points (N) and then each line of the file contains x and y values that are separated by space. The value of x and y are an integer. They can be both negative and positive numbers. You have to sort those points in x-axis major order and...

  • This lab will combine reading data from a file and searching the array to find a...

    This lab will combine reading data from a file and searching the array to find a specific value. Assignment Write a program that reads in a file full of strings into an array, and prompts the user for a string to find in the array. The program should loop until a sentinel value (such as -1) is entered. After looping in main() for the input, write a search function, with the following prototype: int findWord(string [], int, string); with arguments...

  • Python Programming Write a program that counts how often a word occurs in a text file....

    Python Programming Write a program that counts how often a word occurs in a text file. Input: Ask the user for the name of an ASCII text file. Output: Display the numbers of top frequently appeared words and their frequencies. Pseudocode: 1) Read the file 2) Split the file into words 3) Count each word 4) Sort the words by frequencies, starting with the most frequent ones 5) Internally you should use some sort of data structure to keep track...

  • DYNAMIC ALLOCATION and SORTING INPUT: (2) READ IN THE SIZE OF THE ARRAY THAT YOU WANT...

    DYNAMIC ALLOCATION and SORTING INPUT: (2) READ IN THE SIZE OF THE ARRAY THAT YOU WANT from the CONSOLE. (2) READ IN THE CONTENTS OF THE ARRAY from a FILE THIS PROGRAM SHOULD BE ABLE to DYNAMICALLY ALLOCATE an ARRAY the SIZE requested, and then,  SORT in ASCENDING ORDER. Note: YOU MAY USE A SECOND ARRAY TO STORE and REORDER DATA. OUTPUT: (1) The ORIGINAL ARRAY (2) The Sorted Array

  • Serve the user in two phases: In C++ Phase A: (1) Read all input records one...

    Serve the user in two phases: In C++ Phase A: (1) Read all input records one by one into arrays in main memory. (2) Display all the records on the screen in the order they are read from the input data file. (3) Do a Selection Sort (must be implemented as a function) to sort the records in ascending order on STUDENT_ID and display all the full records correctly in the correct order after sorting. Phase B: (1) Then, the...

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