Assignment 04

HOMEWORK #4

CS 53

Due: Friday, Sept. 21, 2012, at noon, 100 points

For this assignment, you will submit a single C++ compilable file containing a program written in C++. Of course, you will submit in the usual (correct) way.

Background: Krusty's no mathematical genius. He didn't finish high school nor junior high school nor primary school. In fact, he was kicked out of kindergarten for smoking. The only school he completed was Klown Skool. Now he has to bring himself into the modern age of digital computing, and he finds that he occasionally needs to convert decimal (representations of) numbers to binary (representations) and vice versa. He doesn't have any troubles with integers (whole numbers), but the fractional part (to the right of a decimal point) gives him headaches. He wants you to code up a program that will do this for him ... and more.

Specifications: Your program is to present the user with a main menu that should appear as follows with these choices:

MAIN MENU 1. Enter a positive number less than one 2. display the Binary equivalent of this decimal input 3. display the Decimal equivalent of this binary input 4. display the Largest digit of the input 5. Quit

Now, in order for you to code this, you need to understand how to convert a base 10 decimal to a base 2 decimal (we'll call it a "becimal"). Also, you need to know how to convert from becimal to decimal. Here's how it works:

  • decimal to becimal: Let N be the decimal number. Now,

    • N10 = .abcdef2, where a,b,c,d,e, and f are the digits of the base 2 representation of N e.g. .687510 = .1011002 and so a = 1, b = 0, c = 1, d = 1, e = 0, and f = 0

  • So, you want to find out what a, b, c, d, e, and f are. Well, with

    • 2N = a.bcdef

  • we see that a = integer part of 2N. If you let F = fractional part of 2N, then you can start the whole process over again and find b, c, .... Here's an example worked out:

    • N = .42 Let F1 = 2N = .84 a = int_part(F1) = 0 and Let R1 = fract_part(F1) = .84 Let F2 = 2R1 = 1.68 b = int_part(F2) = 1 and Let R2 = fract_part(F2) = .68 Let F3 = 2R2 = 1.36 c = int_part(F3) = 1 and Let R3 = fract_part(F3) = .36 . . . etc. You should get that .4210 = .0110102

  • becimal to decimal: I think this is conceptually easier; not necessarily easier to program. Let me show you by example:

    • N = .1101012 = 1 x 1/2 + 1 x 1/4 + 0 x 1/8 + 1 x 1/16 + 0 x 1/32 + 1 x 1/64 = 1 x 1/21 + 1 x 1/22 + 0 x 1/23 + 1 x 1/24 + 0 x 1/25 + 1 x 1/26

Details:

    1. Option 1 should "range check" the input, i.e. if the number entered is bigger than 1 or non-positive, an error message should be output and the menu re-displayed.

    2. The menu should be displayed after any display of computation or error message until the quit option is chosen.

    3. Your program should give an (appropriate) error message if the user chooses options 2, 3, or 4 before choosing option 1.

    4. If the user enters a number that has digits other than 0 or 1 in it, then option 3 should generate an appropriate error message (because the input is not becimal (binary fraction)).

    5. Use a float to store the user input. You will recall that a float will give you 6 significant digits of precision. Then, when options 2,3, or 4 are chosen, you can use 6 iterations for your computations. But remember, if in the future you wish to change this, you want to make it easy (hint hint). Thus, when your code will change a decimal value to binary, it will have to truncate the computation in some cases; you don't have to worry about rounding. That's life in the digital lane.

    6. Use a switch-case statement to implement your menu choice handling code.

    7. Clearly, you will use loops to do the computation outlined above. Be sure to use the proper kind of loops.

Remember: When writing your code, be sure to:

    • Use meaningful variable names.

    • Use proper spacing for indentations.

    • Use constant variable declarations where appropriate.

    • Include the comment block at the head of your file.

    • Comment code that needs it.

    • Be literate in your welcoming/signing-off messages and prompts and output.

    • Make the output pretty and user-friendly.

Note: Don't underestimate the time that it will take to write this program!

When you submit the submit script will compile and execute your program in the process. This means that you will be the "user" of the program for that few minutes. Now, in order that the grader isn't driven crazy by a multitude of inputs/outputs, you will ALL input the same values so that he has uniform output to grade. They are:

    • choose option 2

    • choose option 1 and enter .354

    • choose option 2

    • choose option 4

    • choose option 3

    • choose option 1 and enter .10101

    • choose option 3

    • quit

1 Note: Because of the differing natures of decimal and binary number representations, your conversions will not be exact when you iterate only 6 times in the above methods. Don't worry about that for this assignment.

As always, if you have questions, don't hesitate to ask your instructor or the LEAD tutors.