Question: This assignment can be done without using the Vector class, but it is OK to use it. For your second programming assignment you will be
This assignment can be done without using the Vector class, but it is OK to use it.
For your second programming assignment you will be writing a C++ program with the following functions:
| int bin_to_dec(string s) |
| INPUT(S): an UNsigned binary number as a string (e.g., 10110001) |
| RETURN(S): the binary number converted to the equivalent decimal number |
| PURPOSE: Converts any UNsigned binary number to decimal |
| string dec_to_bin( int n ) |
| INPUT(s): A non-negative decimal number (e.g, 15) |
| RETURN(S): The decimal number converted to the equivalent binary number (as a string) |
| PURPOSE: Converts any non-negative decimal number to binary |
| int hex_to_dec(string s) |
| INPUT(S): an UNsigned hexadecimal number as a string (e.g., FABC) |
| RETURN(S): the hexadecimal number converted to the equivalent decimal number |
| PURPOSE: Converts any UNsigned hexadecimal number to decimal |
| string dec_to_hex( int n ) |
| INPUT(s): A non-negative decimal number (e.g, 15) |
| RETURN(S): The decimal number converted to the equivalent hexadecimal number (as a string) |
| PURPOSE: Converts any non-negative decimal number to hexadecimal |
NOTE: A major component of this assignment is being able to convert between type char and type int, since binary/hexadecimal numbers are stored as strings, whereas decimal numbers are stored as ints.
For example, we know from the ASCII code table that the character 0, represented as 0, has ASCII code 48. The character A has ASCII code 65, and so on. You have to handle converting 0 9 differently from converting A F.
Here are some examples to show the point:
Example 1: convert any character 0 9 to the corresponding number 0 - 9 char char1 = 0; // the ASCII code for 0 is 48
int num1 = char1 48; // the character 0 is converted to the number 0 Example 2: convert any number 0 9 to the corresponding character 0 9 int num1 = 0; char char1 = num1 + 48; // the number 0 is converted to the character 0
Example 3: convert any character A F to the corresponding number 10 - 15
char char1 = A; // the ASCII code for A is 65 int num1 = char1 55; // the character A is converted to the number 10
Example 4: convert any number 10 15 to the corresponding character A F
int num1 = 10; char char1 = num1 + 55; // the number 10 is converted to the character A
Link to the ASCII code table (bookmark or memorize this link): http://www.asciitable.com/
Sample Run:
Here is a sample run of the program. After you finish implementing all four functions, run the program with these test cases (which Ive provided to you in the 260_assign2.cpp file) and make sure you get the same results. The test cases also show you how to properly call each function. As mentioned previously, you will also be coming up with and running five (5) of your own test cases.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
