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 lab01p1.txt (the name stands for lab01, part 1), and do two things: upload it into WebCT, AND print a copy for your TA. (The upload into WebCT is so that I can take a look at your submission; the printed copy is the one that your TA will be grading.) 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 what I described in lecture this morning.) 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/cisc181h/04S/labs/lab01.
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.
When you have completed the program, please create a script file called lab02p2.txt (the name stands for lab01, 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, cd into there, and do
mkdir a34
ls of your directory, showing the source, object
files, and executable.
cat of each of the files
g++ -c chicago.cpp
g++ chicago.o indiana.o indy.o main.o
./a.out
Do your reading assignment (found on the Calendar in WebCT).
If you want to, think about how we might write a more elaborate converstion program; one that could do the full matrix between and among binary, hexidecimal, octal, and decimal. You may need to use strings, as well as a number of other tricks we haven't covered yet.