CSCI 1300 - Exercise 11: Using and Designing structs
What You'll Get From This Exercise
The purpose of this lab is to give you some more practice with structs, both using and designing them.
We will have another lab quiz on structs next week, so take this
opportunity to practice and figure out any questions you might have. Preliminaries
Download the file lab11.cxx and put it in a CLR Project. This file contains two structs. The first is a
Character which contains strings, a data type you have not seen before. The second has been left empty for you to fill in.
Strings
Strings are a C++ data type capable of storing multiple characters (not
to be confused with Characters); a string could be a word, a phrase, or
even a sentence. In order to use strings, you must include the
<string> library, but once you have done that, strings can be
used much the way other data types are:
A value can be assigned to a string using the = operator.
Two
strings can be compared using the standard operators (==, !=, >,
<, >=, <=). The < and > operators work by looking at the
strings character by character and comparing their ASCII values
(assuming the two strings have the same capitalization scheme, this
will be alphabetical order).
They can be passed as arguments to functions, and a function can return a value that is a string.
For more about strings, you can look at chapter 8.2 of your book.
Using the Character struct
Write a function which takes an array of the Character type and its size and returns the Character which would be last if they
were put in alphabetical order by last name. Ties should be broken by looking at the first name, then the middle initial.
For example, if you had:
Emerson, Amelia P.
Emerson, Walter R.
you should return Emerson, Walter R.
Also if you had:
Smith, John A.
Smith, John B.
Smith, John C.
you should return Smith, John C.
Note that this is the first time you have seen a function that returns a struct. This is legal and works much like a
function that returns a simple data type such as an int or a double.
Creating the Shoe struct
Create a struct to hold a Shoe data type. This struct needs to contain at least the following:
the size of a shoe (including possible half sizes)
whether it is a left or a right shoe.
There is no one right way to do this (I can think of at least six possible implementations), but
whichever way you choose, you must make sure that you get all of the information.
Initializing the Shoe struct
After you have designed your struct, create a function to initialize your shoes. Notice that the parameter list was left blank.
This isn't because I think that you won't need any parameters, but because you should decide what parameters your function will
need.
When you decide, don't forget to add the parameters to both the
function header and the function prototype at the top of the function!
If you are stuck, you can look at the function init_Character to get
ideas on how a function of this type might work.
Once you have written your function, go to the main program and call it
on the four Shoe variables: evil1, evil2, ultimate_evil, and
not_quite_evil_enough. Give them the values mentioned in the comments.
Working with the Shoe struct
Write a Boolean function that takes two shoes and returns true if they
are a pair. Two shoes are a pair if they have the same size, but one is
a left shoe and the other is a right shoe.
Look Ahead
If
you finish with these, start looking at Homework 5. This will be a long
assignment, and it is a good idea to start brainstorming early!