1 of 14

Our team name is...

Name

Time joined

call

Roles

(groups of 3)

Roles

(groups of 4)

Roles

(groups of 5)

Person A

Scribe Questioner

Coder 2

Scribe

Scribe

Person B

Coder 1

Questioner

Googler

Coder 2

Googler

Coder 2

Person C

Leader

Googler

Coder 1

Coder 1

Person D

Leader

Questioner

Person E

Leader

Staff

Coach

Lab 11: Structs with Vector Review on the Side

Due Saturday Nov 14th, 2020 at 11:59PM EDT on Gradescope. NO EXCEPTIONS! Your lab MUST be submitted to Gradescope by the deadline or you will not receive credit! This lab is worth 40 points and is graded for completion. However, earning a grade depends on attendance and participating during the lab - this will be verified by the staff member joining your meeting.

Lab meetings last 90-120 minutes. You work as a group for the first 60 minutes and a member of the course staff will join your call for the next 30 minutes to check-in and answer questions. The remaining 30 minutes are there if you need them.

Introductions

Welcome to Lab! Before jumping in to the worksheet, encourage your team to warm up by each taking a turn giving your response to the following question to the group:

When was the last time you drove a car?

REMINDER: If you get stuck on one of the exercises, feel free to move on and come back to it after checking in with your Coach. We are happy to answer any questions!

HEADS UP: If a question says Person D or E is the discussion leader, but you don't have that many members, the Leader should lead the discussion for that question.

NOTE: Coder 1 will be be the coder for the warmup (if needed) and Exercise 1 and coder 2 will be the coder for Exercise 2. For Exercise 2, Coder 2 can give their previous responsibilities to Coder 1.

2 of 14

Warmup: Tracing

This first exercise is meant to help visualize vectors, which will pop up continuously until the semester ends. For this exercise, we will give you blocks of code that manipulate a vector vec, and your task is to record the values of certain variables at each iteration of a loop.

To create a table to represent vec, use Insert → Table and then drag it to the proper location. You can then right click in the table and insert rows/columns as needed.

Here is an example of the format you should use for the drawing:

Example

int N = 5;

vector<int> vec(N);

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

vec[i] = i * i;

//Record the values of i and vec at

this point in the loop

}

i

vec

0

1

2

3

4

0

0

1

0

1

4

0

1

4

9

0

1

4

9

16

3 of 14

Person A (and D) lead(s)

vector<int> vec;

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

vec.push_back(i);

//Record the values of i and vec at

This point in the loop

}

Person B (and E) lead(s)

Recall: The .begin() function refers to the first element in the vector. When using the erase function, you have to specify indices relative to .begin() – it’s just how the erase() function works.

vector<int> vec{1,2,3,4};

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

if (vec.at(i) % 2 == 0) {

vec.erase(vec.begin() + i);

--i;

}

//Record the values of i and vec at

This point in the loop

}

i

vec

0

[0]

1

[0,1]

2

[0,1,2]

3

[0,1,2,3]

4

[0,1,2,3,4]

i

vec

0

[1,2,3,4]

0

[1,3,4]

1

[1,3,4]

1

[1,3]

4 of 14

Person C leads

vector<int> vec{10,20,30,40};

int value = 30;

int index = -1;

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

if (vec.at(i) == value) {

index = i;

}

//Record the values of i and index at

this point in the loop

}

Everyone

vector<int> vec{8, 5, 3, 2, 1};

for(int i = vec.size(); i >= 0; --i) {

cout << vec[i] << ‘\n’ << vec[i] << endl;

}

This code will NOT successfully print:

Why?

i

index

0

-1

1

-1

2

2

3

2

1

1

2

2

3

3

5

5

8

8

The for loop starts at index vec.size() when it should start at vec.size() -1.

5 of 14

Exercise 1: Teacher’s Pet (Estimated Time: 40 min)

Download the class_info.txt and teacherspet.cpp files for this exercise.

Open the class_info.txt file. You will see that there are a number of fields on each given line. The input file contains a list of students and corresponding attributes about them. Each line of the input file is formatted as follows:

<first_name> <last_name> <gpa> <favorite_color> <is_in_state_student (0 or 1)>

Your job is to create a struct called Student to group all the above information in a single data type. The struct should have 5 different properties for each of the 5 different fields on each line.

After you create the struct, you will declare a vector that holds type Student and read the contents of the file to form a vector of students (i.e. each element in the vector is a Student. You must output the answer to the following questions to the terminal:

  1. What is the first name of the student with the highest GPA?
  2. How many in-state students are there?
  3. What is the average GPA of all students?
  4. Are all the students with the favorite color of red out-of-state students? (Print 1 for true and 0 for false)

DO NOT HARDCODE THE CORRECT ANSWER INTO A cout STATEMENT

The correct output is:

Q1: Susie

Q2: 511 students

Q3: 2.52315

Q4: 0

Hints

  • Remember that Boolean values print out as a 1 or a 0 in C++
  • Remember to include the libraries and to write int main()
  • Don’t forget the semicolon after the closing “ } ” of your struct

6 of 14

Copy and paste in the code for your struct definiton below:

Copy and paste in the code for your readData function below:

7 of 14

Copy and paste in the code for your main below:

8 of 14

  • dictionary.h
  • dictionary.cpp
  • spellcheck.cpp
  • words.txt
  • essay.txt

Exercise 2: Writing a Spellcheck (Estimated Time: 30 min)

Coder role switches: Coder 2 now share your screen

Let's write a spellcheck program. The basic idea is to build onto a dictionary library. If a string represents a word in the dictionary, we consider it correctly spelled. Download the starter files:

Recall the general file structure pattern we covered in Runestone with the encryption example.

Take a look at the .h and .cpp files (Coder 2 shares screen) and answer the following questions:

Discussion Leader

Question

Answer

1

PERSON A

What is the role of the dictionary.h file, and where does it need to be included?

2

PERSON B

What is the role of the spellcheck.cpp file?

3

PERSON C

This lab exercise involves a Dictionary struct. Where is its definition located in the files you just downloaded? Why is it there?

9 of 14

Fixing some issues

Go ahead and try compiling the program with the following command (make sure to cd to the correct directory). You'll see some errors.

g++ spellcheck.cpp -o spellcheck

Discussion Leader

Question

Answer

4

PERSON D

Briefly describe what the compiler is having trouble with. What is it looking for that it can't find?

5

PERSON E

What line needs to be added in spellcheck.cpp to fix the errors? Where do you need to add it?

Go ahead and add the required line to fix those errors. Now, try compiling again with:

g++ spellcheck.cpp -o spellcheck

This time, you get some linker errors that say something like "undefined reference" to loadDictionary. Previously, your errors said "not declared".

Discussion Leader

Question

Answer

6

PERSON A

Briefly describe what the compiler is having trouble with. What is it looking for that it can't find?

7

PERSON B

In "compiler-speak", the loadDictionary function is declared, but not defined. What do you think that means?

8

PERSON C

What file is missing from the g++ compile command that must be added to fix the error?

10 of 14

Adding a Function

We would like to be able to look up a word in a dictionary and see if it's there. That seems like reasonable functionality to add to the dictionary library files, so let's do it with a containsWord function. The function should take in a Dictionary struct and the word to search for. Here's an example of how we might use the function:

// Create a Dictionary struct variable and load words from words.txt

Dictionary dictionary;�ifstream fin("words.txt");�loadDictionary(dictionary, fin);

// Check to see whether the dictionary

// contains the word "narwhal"

if (containsWord(dictionary, "narwhal")) {

cout << "I guess they are real after all!" << endl;

}

Dictionary.h - Prototype

Based on the example above and general best practices for parameter passing, write a prototype (just the function header) for containsWord. Add this to dictionary.h and paste a copy below.

Dictionary.cpp - Implementation

Write the full code for containsWord in dictionary.cpp and paste a copy below.

11 of 14

Complete spellcheck.cpp

To put it all together, let's turn to spellcheck.cpp. The code there:

  1. Creates a Dictionary Struct
  2. Reads information into the Dictionary with a call to loadDictionary()
  3. Opens an input stream source for the original essay.txt and an output stream output to write to essay_checked.txt.
  4. Uses the "read until the end" pattern to read words from the source input stream. For each word, we check whether it is correctly spelled using containsWord() with our dictionary. The word gets written to the output stream, surrounded by * if it's misspelled.

However, step 4 above is incomplete. Find a section of commented code near the end of main(). Uncomment it and fill in the _______ blanks to complete the code.

Compile and run it with:

g++ spellcheck.cpp dictionary.cpp -o spellcheck� ./spellcheck

The output to essay_checked.txt should be:

*I* once had a *dreem* that the floor and the *cieling* were the other way from how they *usualy* are and i was pretty happy about it the end

Paste the completed code below (just the part you worked on, not all of main()):

12 of 14

Discussion Leader

Suspicious Word

Best guess - real or fake?

1

PERSON D

unputdownable

2

PERSON E

crapulent

3

PERSON A

spondulicks

4

PERSON B

everywhen

Scrabble (We promise this will be relevant in a moment...)

In the game Scrabble, players attempt to create words from tiles containing individual letters. Sometimes disputes arise when someone creates an obscure word and others suspect it's not legit. For example, consider these words:

It's all fun and games until someone loses an "I". One strategy for making games more civil is to use a dictionary to look up words that are challenged by another player.

Checking if a word is contained in a dictionary? This sounds like a job for our Dictionary library - all we need to do is create a new driver program to layer on top that handles the loops and I/O slightly differently than our original spellcheck program. One of the benefits of organizing your code into modules/libraries is that you can create reusable components that might fit into several different programs.

Write a new driver program (create a new .cpp file) called scrabble.cpp that uses the Dictionary library to:

  1. Creates a Dictionary Struct
  2. Reads information into the Dictionary with a call to loadDictionary()
  3. Indefinitely reads words from cin. After each word is read, it is checked with containsWord(). If the word is in the dictionary, "real" is printed to cout, otherwise "fake" is printed. Then, the program waits for a new word from cin.

Note the first two steps are quite similar to the original driver. Feel free to copy those portions. Compile and run the program with:

g++ scrabble.cpp dictionary.cpp -o scrabble� ./scrabble

To end the program, you may type ctrl-c at the terminal, which forces the program to stop.

13 of 14

Paste your finished scrabble.cpp in the box below:

14 of 14

Time with course staff

The Questioner is responsible for recording any questions the team has for the staff member. Make sure you write down your questions even though you're asking them out loud so that we can tell from your worksheet that the team was actively thinking of questions to ask.

Submit worksheet

Submit the lab worksheets to Gradescope by Saturday at 11:59PM. We strongly recommend submitting your worksheet the same day that you meet as a group! Your lab MUST be submitted to Gradescope by the deadline or you will not receive credit! NO EXCEPTIONS!

The Scribe is responsible for making the final submission to Gradescope. You need to add your other group members to the submission after it is uploaded using this link:

Your team's question

Summary of staff answer