Practice with Streams

Preamble

We are piloting optional practice problems to give you practice with lecture material before the assignments. This is our first iteration so please give us feedback! Are the problems too simple or complex? Not enough practice? Too much practice? Worded poorly? Typos, bugs, errors? Or just let us know if you like what we're doing so we know to keep on doing it!

There are two problems in this handout. This first problem is a relatively simple problem that shouldn't take very long. The second problem is a more complex problem. We used to released it as assignment 1 in earlier iterations of 106L but we did some reworking this quarter. You will not have trouble with this quarter's assignment 1 if you don't do this problem but it is good practice with streams!

Problem 1: The More Simple Problem

Problem

Imagine you have a super exciting book that contains a lot of words, and you want to know which words are the most common in the book. Can you write a program in C++ that reads the book, counts the number of occurrences of each word, and prints the results to the console, so you can check which words are the most common in your book?

Input

A text file called "words.txt" that contains multiple lines of text from your super exciting book.

Output

A list of words and their respective number of occurrences in the book, one word per line.

Note

The output should be case-sensitive, words in uppercase and lowercase should be treated as distinct words.

With this question prompt, you are expected to use std::ifstream, std::getline, and std::stringstream to read the book, extract each word, and count the occurrences of each word. Have fun with it!

We set up an online IDE for you to solve the problem. Here's a link to the practice environment and here's a link to the solution. Click fork this to create a copy of the file that you can make changes to. Make sure to try the problem out before looking at the solution!

Problem 2: The More Complex Problem

Introduction and Problem Goals

It's undeniable: human beings are obsessed with finding patterns. Whether it's in the mysteries of language, the beauties of art, or the depths of strategic games, finding patterns is built into our DNA. In fact, some biologists believe that finding patterns is what sets us apart as a species.

One interesting place to find interesting patterns is Wikipedia. For example, we can play a game called WikiRacer, where we try to move from one article to another with the fewest number of clicks. Try a round online before you move on!

Aside: did you know that if you click the first link on any Wikipedia page repeatedly, you'll eventually always end up at Philosophy 97% of the time?

For your first assignment, you will build a standard C++ program that plays WikiRacer! Specifically, it will find a path between two given Wikipedia articles in the fewest number of links. We'll refer to this path as a "ladder."

To simplify assignment 1, we removed the user-facing part of Wikiracer and instead are making it available as an optional practice to get experience with streams. To simplify the implementation and allow for some testing, we will do this in two parts (A1 and A2). You will have access to a function findWikiLadder that finds ladders between Wikipedia topics for you (you'll implement the core of this interesting search algorithm that finds the ladder in A1). In this practice you're in charge with writing parts of a main() function that will take a user-inputted file name, read in the file, find Wiki ladders between the start and end destinations in the file, and print out the ladder. Let's get started!

Download And Set Up Assignment

If you haven't already, please follow the instructions on this page before proceeding. Please do both the one-time instructions and the instructions for editing each assignment. Since we're working on assignment1 (this is a practice now but still follow the instructions verbatim. We used to required this practice), you can follow the instructions exactly. If you have any questions at all, please don't hesitate to send us an email!

Assignment Details

If you've already completed the Assignment Setup and downloaded, and set up Assignment 1, you are all set to start coding! In this assignment, you'll only be editing main.cpp. The assignment comes with sample files you can test your programs on in the "res" folder. We'd recommend exploring them, as well as adding your own test cases in random.txt.

  1. Your first task is to implement file reading for main.cpp. This should take roughly 10 lines of code. We have already provided the code to read in a filename from the user. Specifically, your task is to create a filestream from the filename (check out std::ifstream), and then process the file data appropriately for the given program as follows.
    • The input files are formatted as follows: the first line contains the number of input pairs in the file, and each subsequent line consists of one input pair (i.e. two words, a start_page and end_page, separated by a space.) See input-small.txt or input-big.txt for an example. You can assume files will be formatted correctly.
    • For each input pair, parse out the start_page and end_page into strings. If you're using getline, you will need to do string processing on the return value of std::getline. If you're using std::ifstream, then you can use the > > operator to set the start_page and end_page strings. Next, once you have the two strings that represent the starting page and ending page on each line, then call findWikiLadder and append its result to the outputLadders vector. To see its function signature (and thus what parameters the findWikiLadder function takes), check out wikiscraper.h!
  2. Your second and final task will be to print out the contents of outputLadders, however you may like!
We have implemented (and hidden) findWikiLadder for you! It will give you real Wiki ladders! Stay tuned for the next assignment, where you implement the core of this function yourself!

Implementation tips:

  • Remember to avoid mixing cin and getline!
  • Depending on how you implement reading in values, you may end up needing to convert a string to an integer. To do so, you can use the stoi(line) function, which takes in a string line and returns the integer it represents.
  • In order to check your code, you can print out each input pair before it gets passed into findWikiLinks. The starter code already prints out filenames upon which to test your code.

For this assignment, you don’t need to handle the case that the user inputs an invalid filename. (Since you are your own user in this case, make sure that the filenames you type in are valid!)