Lab two has three parts.
Parts 1 and 2 involve writing programs that were discussed in lecture, and mainly test your ability to work with problem solving, input output of simple variables, and control structures (if, if/else, switch, for loops, while loops).
Part 3 involves some work from your Anderson textbook, chapter 34, that introduces functions, and the idea of separate compilation. Functions are discussed in Deitel chapter 3, but separate compilation is a topic you will need to get primarily from your Anderson textbook.
Separate compilation is an important idea to understand as you move from smaller programs into working with larger ones. Nearly all "real world" programs are built with separately compiled object files, so this is an important "real world" topic.
The first part requires you to write a program that will convert from binary (base 2) to decimal (base 10.) You will prompt the user to enter an integer in base 2. This integer may be either positive (e.g. 10010) or negative (e.g. -1001).
You will read the number entered into a regular int variable.
Note that when you do, the computer will treat the number entered as a decimal
number. However you are going to interpret the digits as binary digits.
This is an important point, and a bit of further explanation may be helpful,
so read on.
Consider the following example run:
Please enter a binary number > 10011 The decimal equivalent of 10011 is 19.When you enter the number 10011 and store it in an
int variable,
as far as the computer is concerned, the value you stored represents ten thousand,
and eleven. But we are going to treat that value as if it represented 10011
in binary, which is 19 is decimal (16 + 2 + 1).
The suggested way to accomplish this is to use the modulus operator (%) and
integer division, dividing and "modding" by 10 each time, to strip off each
digit of the number. A while loop can be used to repeat the process
until all digits are added up.
Your user might input digits that are not valid for binary numbers. As you
strip off each digit, you can test for this. If you find a digit other than
a 0 or a 1, your program should print an error message on cerr
and exit immediately. You can use the exit(-1); function to do
this, or just return -1 as the result of the main function.
Your program should handle negative numbers. It may be easier to design an algorithm if you treat that as a special case. You can detect that the number is negative, remember that fact somehow by setting a variable, then make the number positive, compute the decimal equivalent, and then set the result to be negative at the end of the program.
Be sure that your program contains appropriate comments, and that you follow some reasonable convention about indenting your code to reflect the control structures (if/else, while, etc.) Once you choose a convention, be consistent, at least within each single file.
When you have completed the program, please create a script file called lab02p1.txt (the name stands for lab02, part 1), upload it into WebCT. If your TA requests it, also print a paper copy and submit it to him/her. What should your script file have in it? Well, read over the next section.
./a.out.)
(See instructions below on how many times is necessary.)
Be sure that your name and your section number appears in a comment inside each of your C++ programs! (But... don't EVER include your student number in anything you submit; this leaves you open for identity theft! Just your name and your section number is sufficient.)
When demonstrating runs of a program for this class, or any computer science class, keep the following in mind.
If the program has different kinds of output for different runs, be sure you include several runs that test the different kinds of output.
An example is in your conversion program: you need to show positive and negative binary numbers, and also a few different kinds of invalid input. It would be a good idea to test for boundary cases such as 0 and 1 also.
In this and all future labs, figuring out how many runs to do is part of the assignment. Points may be deducted if you don't show sufficient runs. Ask your instructor or your TA if you are not sure.
Note that you also need to keep the amount of output reasonable; showing 20 runs just to be "safe" when three would suffice is not reasonable.
The game is simple (though just slightly more elaborate than the version of the game we played in lecture. Here are the rules for how two humans might play the game. (After that, I'll explain how to adapt this for the computer.)
Player 2 might seem to have an unfair advantage; he/she can always make the tie breaker come out in his/her favor. However, consider that if she/he uses that strategy consistently, then that gives Player 1 an advantage, because it allows Player 1 to rule out half the numbers (all the odd ones, or all the even ones) right from the beginning.
For the part where the computer guesses the number, you'll need some code that returns a random number. You can look this topic up in your textbook, and/or you can copy some sample code from this link:
http://copland.udel.edu/~pconrad/cisc181/04S/labs/lab02.
For this assignment, you do not need to take the odd/even rule into account when devising a strategy for the computer's guessing algorithm; a straightforward binary search is suggested. (However, its interesting to consider how the odd/even rule changes the strategy.) Also, the computer's number should be truly random (well, pseudo-random, anyway. What I mean is: don't program some "strategy" into the computer's choice of number; just use the "rand" function to choose a random integer between 1 and n.)
When you have completed the program, please create a script file called lab02p2.txt (the name stands for lab02, part 2), and do two thingsl: upload it into WebCT, AND print a copy for your TA.
For example,
mkdir cisc181_a34
Or, if you already have a cisc181 subdirectory under your main directory, cd into there, and do
mkdir a34
pwd command, illustrating which directory you are in
ls of your directory, showing the source files (.cpp
files), object files (.o files), and executable.
cat of each of the source files (.cpp files)
g++ -c chicago.cpp CC -c chicago.cpp
g++ chicago.o indiana.o indy.o main.o
./a.out
Do your reading assignment (found on the Calendar in WebCT) and complete the "prelab exercises" in the Lab manual for Chapter 3 before coming to lab next week (3/3/04.) Note that this week you do not have to do ALL of the pre-lab exercises. Look at the following link for guidance (click on "pre03.html") for instructions for "pre lab, chapter 3").
http://copland.udel.edu/~pconrad/cisc181/04S/hwk
You can also get to this link from the "hwk" link on the main CISC181 web page:
http://copland.udel.edu/~pconrad/cisc181
From now on, get in the habit of looking at this page for guidance as to which pre-lab exercises to complete each week.