Exclude any existing source code files that may already be in your IDE project and add a new one, naming it C1A3E3_main.

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
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.

Post by answerhappygod »

Exclude Any Existing Source Code Files That May Already Be In Your Ide Project And Add A New One Naming It C1a3e3 Main 1
Exclude Any Existing Source Code Files That May Already Be In Your Ide Project And Add A New One Naming It C1a3e3 Main 1 (79.95 KiB) Viewed 43 times
Exclude any existing source code files that may already be in your IDE project and add a new one, naming it C1A3E3_main.cpp. Write a program in that file to convert an arbitrary user-entered hexadecimal integer value into words based solely upon its numeric value, not the individual characters entered. If the value is negative the word minus must be first. If you are not familiar with the hexadecimal number system see note C.1. Here are some sample input values and the expected words: Input 00000 5a3 -AbD -500 -000500 Words zero five A three minus A B D minus five zero zero minus five zero zero Your program must interpret all integer input and display all integer output as hexadecimal (notes 1.14 and 1.12, respectively). Your program must: • prompt the user to enter any hexadecimal integer value and use cin >> to read the entire value at once into a type int variable. • use cout << as often as necessary to display the variable's value and the equivalent words in the format below, placing double quotes around both for readability and using single letters as the words for the 6 hex letters. For example, if the user input is -999999999999999999999992aBc58 the following would get displayed: "-2abc50" in words is "minus two A B C five zero" • not declare variables or use casts that are not type int or type bool. • not use anything involving floating point types (the pow function, math.h, type double, etc.). • not use arrays or recursion; recursion occurs when a function is called before a previous call to that same function has returned, for example, when a function calls itself. • not use nested loops - they are unnecessary. • not use any separate code to handle a user input of zero. Uppercase/lowercase does not matter for the 6 hex letters. Manually re-run your program several times, testing with at least the following 6 input values: 3-1aB 0 1010 -1010-000000
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
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply