By the time you have completed this lab, you should be able to
Carefully choose the first pilot. Let the person who has been the pilot the least amount of time so far be the pilot first this time. As usual, switch roles after awhile - you don't have to wait for us to tell you!
If your assigned partner is more than 5 minutes late, ask the TA to pair you with someone else for this week.
This lab's pilot should log in. You will (both) work in this account for the rest of the lab.
Create a ~/cs16/lab03 directory and make it your current directory:
-bash-4.2$ mkdir ~/cs16/lab03 -bash-4.2$ cd ~/cs16/lab03
Start ch (notice how kyledewey simplified the ch prompt):
-bash-4.2$ ch Ch Professional edition, version 6.1.0.13751 (C) Copyright 2001-2009 SoftIntegration, Inc. http://www.softintegration.com /cs/student/kyledewey/cs16/lab03> _prompt = "ch> " ch>
Then accomplish the following steps in order at the ch prompt:
guess == target
at the prompt:
ch> guess == target 1 ch>Remember that 1 signifies true, so that means guess is equal to target in this case.
guess != target
and verify that
0 results, because 0 signifies false in C.ch> if (guess == target) printf("success\n"); success ch>And type it again with the condition falsified (remember you can repeat commands with the up arrow, and use arrow keys to edit), and verify that nothing prints:
ch> if (guess != target) printf("success\n"); ch>
ch> if (guess != target) ch> printf("success\n"); success ch>Ooops, but that's not right! The problem is that ch considers that to be two separate statements, not one compound if statement. This is a problem at the ch command prompt only. We can take advantage of the C "continuation character" to get around it. In C, a backslash character (
'\'
) can always be used at the end of a line to
indicate that it continues on the next line. So let's try again with the continuation
character this time:
ch> if (guess != target)\ ch> printf("success"); ch>Okay! It's awkward, but it works.
ch> if (guess == target)\ ch> printf("success\n");\ ch> else\ ch> printf("fail\n"); success ch>
The rand function is defined in the <stdlib.h> library. It is readily available
in ch, but you must include the library to use it in a Standard C program. This
function returns a pseudorandom integer between 0 and RAND_MAX
(which
is also defined in <stdlib.h>, but apparently is not automatically available to ch).
You can find out the "default" first pseudorandom value by typing the following at
the ch prompt:
ch> rand() 1804289383 ch>
Type it again and again to get the next two values in the default sequence:
ch> rand() 846930886 ch> rand() 1681692777 ch>
Two reasons they are called "pseudorandom" numbers: (1) eventually the sequence will repeat; and (2) the sequence is determined by a numerical seed value. By default, this seed equals 1. So that means if you don't set the seed to something else, then the same sequence will occur every time you run the program; and it also means if you reset the seed to its original value, then this sequence will repeat.
Pass an unsigned integer to the function srand to set the seed. For example, notice how passing the value 1 will restart the default sequence:
ch> srand(1) ch> rand() 1804289383 ch>
Of course, using a different value as the seed will result in a different sequence:
ch> srand(25) ch> rand() 2017252061 ch>
Many programmers use the system time to seed the random number generator. In Standard C you must include the <time.h> library, but that is not necessary at the ch prompt. The following line prints the current system time (executed Monday, April 16 at 12:52:25 PDT 2012):
ch> time(NULL) 1334605941 ch>
[Note: the constant NULL
is defined in <stdlib.h> like rand
and srand.] Here is how the system time can be used to seed the random generator,
and start a new and seemingly unpredictable pseudorandom sequence:
ch> srand(time(NULL)); ch> rand(); 96975394 ch>
The trick used to find a random value in a certain range involves the modulus operator.
For example, rand()%10
produces a value in the range 0 to 9 (the
remainder after dividing by 10). In general, to produce a random value in the range
a to b, use the following formula:
a + rand() % (b-a+1)
Of course you can simplify the formula if a is 0 or 1. For example, you can use a simpler formula to find a random value between 1 and 3 inclusive. Do that now, and enter it several times until you are satisfied that your formula works - meaning that all of 1, 2 and 3 can occur, but never anything less than 1 or greater than 3.
First: switch roles between pilot and navigator if you did not
already do that.
Always stay logged into the original pilot's account.
Save a copy of this program skeleton as guess.c in the original pilot's lab03 directory. You can cp a copy from the instructor's account as follows:
cp ~kyledewey/cs16/lab03/guess.c ~/cs16/lab03/
The goal of this step is to modify guess.c so that it behaves like our solution, as demonstrated by the following runs (after guess.c was compiled):
-bash-4.2$ ./guess Guess a number between 1 and 3: 1 Correct! -bash-4.2$ ./guess Guess a number between 1 and 3: 1 Wrong. Target is 3. -bash-4.2$ ./guess Guess a number between 1 and 3: 1 Correct! -bash-4.2$ ./guess Guess a number between 1 and 3: 1 Wrong. Target is 2. -bash-4.2$
Add the following features to guess.c using emacs (or another editor of your choice). Then save, and quit the editor.
YOUR NAME(S)
" at the top with your names, and generally
follow any instructions highlighted by upper case letters in the file.You can use ch to test your program without starting a ch session. Here is a test of our solution that way:
-bash-4.2$ ch ./guess.c Guess a number between 1 and 3: 1 Wrong. Target is 3.
If errors occur: (a) don't panic, and (b) don't immediately ask the TA for help! Read each error message. Look at the line number it refers to in your program, as that is likely where the problem is.
Try to fix it on your own before asking the TA for help.
When you are satisfied it works, make an executable version of it to run for the TA:
-bash-4.2$ make guess cc guess.c -o guess
Submit the lab with the turnin program. You MUST have both your name and your partner's name in the file in order to receive credit. Remember that the original pilot needs to do this step, since that is whose account you have been using in Cooper Lab.
Bring up a terminal window on either CSIL or the Cooper lab, and cd into the original pilot's cs16 directory, and cd again into the lab03 directory. Then type the following:
turnin lab03@cs16 guess.c
Respond "yes" when the program asks if you want to turn in (be sure to read the list of files you are turning in), and then wait for the message indicating success.
Optional Extra Challenge
- Did you use the symbolic constant MAX in your solution to Step 4? If so, then it should be very easy to change the program to let the user guess a number between 1 and any value > 1, like 2 or 100 or whatever. Change a copy of the program now (save it as guess2.c for example) to simplify the user's task by letting them guess a number between 1 and 2:
-bash-4.2$ ./guess2 Guess a number between 1 and 2: 1 Correct!- With a larger value of MAX, you can still give the user a better chance to guess the answer by allowing two or more guesses. For example, if the user's first guess is wrong, then you can let them choose one more time. Change another copy of the program (say guess3.c) to have a MAX value of 5. If the user chooses the correct value the first time, just print "Correct" as before. Otherwise, say "Wrong. Try again." Then read the second guess and process it the way you processed the first guess in the original program.
- You can improve the user's chances even more by giving better hints. For example, you could tell the user their guess is "Too high" or "Too low" instead of just "Wrong." Improve a copy of guess3.c to work that way.
- If you still have time to work, then give the user a third chance, and then a fourth, and consider increasing the MAX value to keep the user interested.
Adapted by Kyle Dewey from a lab prepared by Michael Costanzo.