Question

In: Computer Science

For this assignment you will write a C++ program to read a list of words and...

For this assignment you will write a C++ program to read a list of words and sort them in alphabetical order.

Requirements:

  • You must use a bubble sort OR selection sort algorithm - your choice. You do NOT need to do both.
  • You must print the sorted list of words to the console standard out (cout).
  • You must use a vector object to store the list of words and you must sort the list "in-place" (meaning, do not create a new list.)
  • When reading the data, your program must ignore blank lines and any line that begins with the # symbol.

NOTES:

  • Part of the grade will be based on comments according to the style guide.
  • You can implement a class if you want to. Writing a class is not required for this assignment.
  • Don't make any assumptions about the input data. I may grade using a completely separate list of words. (if so, the format will be the same - no more then one word per line. But assume that blank lines and comments can occur anywhere in the file.)

word_list.txt

_____________________________________________________________________________________________________________________________________below:

# Read the list of words below and write them out in alphabetical order.
# Your program should skip over the blank lines. 
goal
interesting
successful
job
experience
career

manage
responsibility
technology
purpose
beautiful
wonder
opportunity
leader
quality

knowledge
victory
nature
ability
dream
understand
future
hope

Solutions

Expert Solution

#include <iostream>

#include <fstream>

#include <vector>

using namespace std;

// this function sorts given vector using bubble sort

void bubble_sort(vector<string> &words)

{

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

    {

        for (int j = 0; j < words.size() - i - 1; j++)

        {

            if (words[j] > words[j + 1])

            {

                // below 3 lines swaps j and j+1 word in vector

                string temp = words[j];

                words[j] = words[j + 1];

                words[j + 1] = temp;

            }

        }

    }

}

int main()

{

    ifstream input("word_list.txt");

    // file not found

    if (!input.is_open())

    {

        cout << "Can't open file\n";

        return 0;

    }

    // vector of words

    vector<string> words;

    string line;

    // read line till end

    while (getline(input, line))

    {

        // blank and lines starting with #

        if (line.size() == 0 || line[0] == '#')

            continue;

        words.push_back(line);

    }

    // sort using bubble sort

    bubble_sort(words);

    // print vector

    cout << "Sorted words:\n\n";

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

    {

        cout << words[i] << endl;

    }

    input.close();

}

Output:


Related Solutions

code in C Step 1 Write a program that will read in a list of addresses...
code in C Step 1 Write a program that will read in a list of addresses (100 maximum) from a file. The program should read records from a file until an EOF is found. The program must read one character at a time from the file using fgetc. For example: chrLetter = fgetc( pfilInput ); No partial addresses/records will be given. The format of the addresses is: RecordID, Full Name, Street, City, State, ZipCode Step 2 Store the addresses in...
Your assignment is to write a C++ program to read a text file containing the information...
Your assignment is to write a C++ program to read a text file containing the information of the employees of a company, load them into memory and perform some basic human resources operations. This assignment will help you practice: multiple file programming, classes, public and private methods, dynamic memory allocation, constructors and destructors, singly linked list and files. Implementation This lab assignment gives you the opportunity to practice creating classes and using dynamic memory in one of the required classes....
Write a C++ program to read in a list of 10 integers from the keyboard. Place...
Write a C++ program to read in a list of 10 integers from the keyboard. Place the even numbers into an array called even, the odd numbers into an array called odd, and the negative numbers into an array called negative. Keep track of the number of values read into each array. Print all three arrays after all the numbers have been read. Print only the valid elements (elements that have been assigned a value). a. Use main( ) as...
Assignment Description: Write a C++ program for keeping a course list for each student in a...
Assignment Description: Write a C++ program for keeping a course list for each student in a college. Information about each student should be kept in an object that contains the student id (four digit integer), name (string), and a list of courses completed by the student. The course taken by a student are stored as a linked list in which each node contain course name (string such as CS41, MATH10), course unit (1 to 4) and the course grade (A,B,C,D,F)....
C++ The program you write for this lab will read in the number of nodes and...
C++ The program you write for this lab will read in the number of nodes and a binary relation representing a graph. The program will create an adjacency matrix from the binary relation. The program will then print the following : 1. The adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists and said so. The sample run of the program is as follows. The output should just like...
C language you are to write a a program that will first read in the heights...
C language you are to write a a program that will first read in the heights and weights of a series of people from a file named values.dat. Create this file using your editor so that each line contains a height and corresponding weight. For instance, it might look likea: 69.0 125.0 44.0 100.0 60.0 155.0 49.0 190.0 65.0 115.0 50.0 80.0 30.0 129.0 72.0 99.0 68.0 122.0 50.0 105.0 and so on. The formula for the standard deviation of...
In this assignment, you shall create a complete C++ program that will read from a file,...
In this assignment, you shall create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for a student (first letter of their first name connected to their last name Next it will need to read three integer values that will represent the 3 exam scores the student got for the semester. Once the values are read and stored in descriptive variables it will then need to calculate a weighted course average for that student. Below...
Write a program using C to read a list of your friend names which ends by...
Write a program using C to read a list of your friend names which ends by the word end. The program builds a linked list using these names and prints the names in the order stored into the linked list The list can be created using insertion at the beginning or insertion at the end; Use switch case to select the type of insertion; Case 1:insertion in the beginning; Case2:insertion in the end. Once the list is printed after insertion;...
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT