CS201 – Spring 2001
Prof. Hartman - Homework #9
Due Friday, April 20th by 4:00
pm
Recommended Self-Review problems: 6.1 – 6.2
Homework to be turned in: (remember that all programs must
follow the guidelines from handout #3.)
const int g = 10;
const int *x = &g;
int *y = x;
(*y) = 0;
3. (5 pts) Written: Do the following snippets of code have something syntactically wrong with them? If so, how would you correct them? Assume both snippets occur in a program that has the following morsel of code preceding them:
struct Something {
int weird1;
int weird2;
};
Something *smthgPtr;
smthgPtr->weird1 = 1;
smthgPtr->weird2 = 2;
3a) cout <<
*smthgPtr.weird1;
3b) cout
<< smthgPtr->weird2;
A very basic sentence can be of the form: <article> <noun> <verb> <preposition> <article> <noun> <ending-punctuation>.
Create five arrays of character pointers that hold articles, nouns, verbs, prepositions, and ending-punctuation. Create a random sentence by randomly selecting words from the arrays in the order specified above for sentences. Concatenate these words into a large sentence array using strcat(). Make sure your sentence has a capital letter in the beginning. (Be careful when using strcat()that the string you are adding to has enough space to hold everything!)
A starting list that you can use for the articles, nouns, etc. is given below. Embellishing the list is encouraged.
· Nouns: Cat, dog, boy, girl, horsey.
· Verbs: walked, ran, skipped, fell, jumped.
· Articles: The, a, one, some, any.
· Prepositions: to, from, over, under, on, after, before.
· Ending-punctuation: ?, . , !
5.
(15 pts) Programming: Write a class Rectangle. It should have two data members, length and width. It
should have a constructor that takes length and width as optional parameters
with default values of one. It should have member functions that return the
perimeter and area, and set and get functions for both the length and the
width. The set function should check that both length and width are positive
and less than 20. You should keep the declaration and implementation in
separate files (such as rectangle.h and rectangle.cpp). Write and use a short driver program
to test everything your class can do.