CS 16, Winter 2018

Lab08:
C++ Structures


Goals for this lab

By the time you have completed this lab, you should be able to

Step by Step Instructions

Step 0: Fill out TA evaluation

This part is worth 10 points (out of 50 total Lab08 points). It is important to us, and you can only earn the points by attending your lab session on Wednesday, March 7, and only if you arrive in time to complete it. The forms will be distributed no later than 5 minutes past the hour that begins your lab session. Don't be late!

Step 1: Get together with your lab partner, and create a lab08 directory

This lab's first pilot should log in, create ~/cs16/lab08/ and make that your current directory.

Step 2: Practice using a struct at the ch prompt

Start ch. Then define the following structure to represent a playing card:

ch> struct Card { char suit; int face; };

When not using ch, we usually define structs over several lines like this:

struct Card {
    char suit;
    int face;
};  // btw, forgetting the semicolon at the end is a common error

An object of this type will hold a char variable named suit to represent the card's suit (assumed to be 'C' for Clubs, 'D' for Diamonds, 'H' for Hearts and 'S' for Spades), and an int variable named face to represent its face value (2-10 for two through ten, 11 for jack, 12 for queen, 13 for king and 14 for ace).

Create a Card object named c, and set its fields to represent the ace of Spades. Then type the object's name by itself to see how ch displays structs:

ch> Card c;
ch> c.suit = 'S';
S
ch> c.face = 14;
14
ch> c
.suit = S
.face = 14

Create a pointer to a Card object named cp, set it to the address of c:

ch> Card *cp = &c;

Now cp can be used as another way to access the fields of c. To do so you must first dereference the pointer, *cp, and then apply the dot operator to access the field. Since the dot operator "binds more tightly" (has higher precedence) than the dereference operator, the first part has to be inside parentheses, and because that syntax is so awkward, the language authors provided '->' as a shortcut pointer operator. Practice both at the ch prompt: use the awkward syntax to display c's suit, and use the pointer operator to display c's face:

ch> (*cp).suit
S
ch> cp->face
14

Create another struct card object named c2, but also use an "initializer list" to set this card's fields to represent the queen of Hearts:

ch> Card c2 = {'H', 12};
ch> c2
.suit = H
.face = 12

Notice that you cannot use relational operators to compare entire structures like Card objects. Instead you must compare individual fields:

ch> c2 < c
ERROR: invalid operands for less than operator <
ch> c2.face < c.face
1

It is appropriate to use relational operators for comparing pointers to structures though: both == and != any time, and the other operators when the pointers both refer to the same array. (a) Create another Card pointer named cp2, and assign the address of c2 to it. (b) Then use == to compare cp to cp2. (c) Aim cp2 at c. (d) Compare cp to cp2 again:

ch> Card *cp2 = &c2;
ch> cp2 == cp
0
ch> cp2 = &c;
0x92db4f0
ch> cp2 == cp
1

Understand these results before proceeding to Step 3.

Step 3: Write and test functions that process structs

First: switch roles between pilot and navigator if you did not already do that.

Exit ch.

Study the completed parts of this cards.cpp skeleton.

Download a copy of the skeleton program to your lab08 directory.

After all of printCard, printCards and cmpCards are completed, your results should match the following:

-bash-4.1$ make cards
g++     cards.cpp   -o cards
-bash-4.1$ ./cards
printing third card: 
seven of Diamonds

printing first two cards:
ace of Spades
queen of Hearts

printing all cards, unsorted:
ace of Spades
queen of Hearts
seven of Diamonds
seven of Clubs
jack of Spades

printing all cards, sorted:
seven of Clubs
seven of Diamonds
jack of Spades
queen of Hearts
ace of Spades

Open cards.cpp in an editor, and make the following changes. But don't do them all at once - instead test each part before continuing to the next part. You can even compile/run the original version - it just doesn't do anything:

-bash-4.1$ ./cards
printing third card: 

printing first two cards:

printing all cards, unsorted:

printing all cards, sorted:
  1. Type your name(s) in the comment at the top. Then scroll down past the main function to edit the ones below it.
  2. Implement printCard to neatly print the face and suit of one Card object, and then a newline character. See the sample results to know exactly how it should work. Did you study the global array named faces and the function named suit at the top of the program? You will find that printCard is very easy to do if you use these features properly.
  3. Implement printCards to neatly print the first n elements of the array cards, each on a separate line. Of course you should use printCard to simplify this part.
  4. Implement cmpCard to compare two Card objects. A return of value of true means that c1 is less than c2, first with regard to its face value, and in the case of equal face values then secondly by the alphabetical order of the two suits (C before D before H before S).

Remember to save the file and test after completing each function. Don't wait to test them all at once.

Step 4: Submit your revised cards.cpp

You can use the following command from the CSIL prompt:

~submit/submit -p 966 cards.cpp
Or you can use the submit.cs interface at https://submit.cs.ucsb.edu/ from your web browser - submit cards.cpp for Lab08. A perfect score is 40/40 points. All parts must be work exactly right to earn any points from the submit.cs test.

If you are working with a partner, be sure that both partners' names are in a comment at the top of the source code file, and be sure to properly form a group for this project in the submit.cs system.


Evaluation and Grading

Each student must accomplish the following to earn full credit [50 total points] for this lab:

If you finish before the end of your lab period, consider helping other students who might be struggling. You may also work on PA6 or the optional challenges below.


Optional Extra Challenge


Prepared by Michael Costanzo.