Exclude any existing source code files that may already be in your IDE project and add a new one, naming it C1A3E3_main.
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Exclude any existing source code files that may already be in your IDE project and add a new one, naming it C1A3E3_main.
Hints for Exercise 3: First see notes 1.12 & 1.14 for information on doing hexadecimal I/O in C++. Then implement the optional algorithm below, which displays a user hexadecimal integer input value in words, one at a time moving left to right. There are no nested loops, part A is completed before part B begins, and part B is completed before part C begins. Only one instance of the code for each part is necessary. Testing whether an expression is true or false can be done in several different ways and the words If and Else do not necessarily refer to an actual "if" or "if-else" statement. Part A: A1. Prompt the user, get his/her input, and display the initial double quote of the output message. A2. If the user input value is negative change it to positive and display a minus sign. A3. Display the positive user input value (made positive by step A2 if necessary). A4. Display more of the output message up to the point where the first word of the value is needed. A5. If the original input value was negative display the word "minus", followed by a space. Part B ("for" loop is used): Find a power of 16 divisor that will produce the most significant digit (MSD) of the positive input value as follows: B1. Assign 1 to a divisor variable and the positive input value to a dividend variable. B2. If the value of the dividend is greater than 15: a. Multiply the divisor by 16; the product becomes the new divisor. b. Divide the dividend by 16; the quotient becomes the new dividend. } c. Repeat from step B2. Else Proceed to Part C below. Part C ("do" loop is used): The starting value for the divisor used in this part will be the value computed for it in Part B above. Part C will pick off the digits of the positive input value left to right and display them as words as follows: C1. Assign the positive input value to a dividend variable. C2. Divide the dividend by the divisor, which yields the MSD. Display it as a word using a 16-case switch statement (see below). C3. Multiply the MSD by the divisor and reduce the dividend's value by that amount. (This removes the dividend's MSD.) C4. Divide the divisor by 16; the result becomes the new divisor. C5. If the new divisor is not equal to 0, repeat from step C2. Else You are finished displaying the number in words! About the recommended "switch" statement... While the use of "magic numbers" is usually a bad idea, in some situations they are appropriate such as for the "cases" used in the "switch statement" recommended for this exercise. Specifically, each case represents a unique numeric value ranging from 0 through 15 (hex F). There is no underlying meaning to these values other than the values themselves, their purpose is obvious and unmistakable, there is no possibility that they might ever need to be changed, and there is no identifier (name) that would make their meaning any clearer. Thus, the literal values should be specified directly, as follows: switch (...) { case 0: case 1: case 2: etc. m