030_Problem_Set_0_Solutions

# 030_Problem_Set_0_Solutions - CS106L Fall 2009 Handout#03...

This preview shows pages 1–3. Sign up to view the full content.

CS106L Fall 2009 Handout #03 October 27, 2009 Problem Set 0 Solutions _________________________________________________________________________________________________________ This handout contains sample solutions to the problems on Problem Set 0. There are many solutions to these problems, so don't worry if your answers don't precisely match the ones outlined here. Even if you think you're on top of the material, I recommend looking over these answers since they might illustrate an entirely different approach to solving the problems. Problem 0 sample.cpp is a program that implements the classic “guess the number I'm thinking of” game. The program picks a random number between 0 and 99, inclusive, then invites you to guess what it is. At every step, the program will indicate whether your guess was higher or lower than the computer's number. As an interesting exercise, you might want to see if you can create an “Evil Guessing” program that, like Evil Hangman, actively dodges the user's guesses. That is, instead of picking a number between 0 and 99, instead you maintain a range of values then gets progressively whittled down based on the user's guesses. Problem 1 Here's a working version of GetReal : double GetReal() { while(true) { stringstream converter; converter << GetLine(); // Assume this is defined. double result; converter >> result; if(converter.fail()) cout << "Please enter a real number ." << endl; else { char leftover; converter >> leftover; if(converter.fail()) return result; else cout << "Unexpected character: " << leftover << endl; } cout << "Retry: "; } } There is very little code that needs to be changed from GetInteger to convert it into GetReal . We only need to change the return type, the definition of the result variable, and text “Please enter an in- teger” and we have a completely working input function. Isn't the streams library great? - 1 -

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Problem 2 There are many ways that we can encode all of the necessary information about directions. The following is, in my opinion, the simplest. First, we create a file called directions.h and encode it as follows: /* directions.h: Represents a set of directions as calls to macros * DEFINE_DIRECTION where the arguments are * * DEFINE_DIRECTION(direction, opposite, dx, dy) */ DEFINE_DIRECTION(North, South, 0, 1) DEFINE_DIRECTION(South, North, 0, -1) DEFINE_DIRECTION(East, West, 1, 0) DEFINE_DIRECTION(West, East, -1, 0) We can then automatically generate the enum directionT as follows: enum directionT { #define DEFINE_DIRECTION(dir, opp, dx, dy) dir, #include "directions.h" #undef DEFINE_DIRECTION }; DirectionToString can be implemented using an approach similar to that covered in lecture or in the
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 7

030_Problem_Set_0_Solutions - CS106L Fall 2009 Handout#03...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online