Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Homework 4
- Your manager has forgotten everything that s/he knew about base conversions, but needs a simple way to perform base-10 (decimal) to pick-a-base conversions. Your mission is to build a small, friendly application to help them out.
- You shall use the division method of base conversions with the algorithm provided in the presentation on Canvas (CmpE30_Lec03a_BaseConversion).
- You shall prompt the use for a value, base 10, to convert ranging from 0 -> 1024.
- Please enter a value to convert (0 -> 1024, -1 to Exit):
- Upon entry of a -1 as a value to convert, the application will exit. Otherwise, it will continue. After the conversion, the user will be prompted, once again, for a value to convert.
- You shall prompt the user for base to use in conversion ranging from 2 -> 16.
- Please enter a base to use in conversion (2 -> 16):
- As you progress through the algorithm, you shall collect up the remainders into an array of characters that can hold, at least, 11 individual digits. [It takes 11 digits to hold the maximum value to convert, 1024, using the smallest base, 2.]
- You should display a message to the user indicating what is happening. You shall display the final result as a string.
- Value [100] converted to base [16]:
- 4
- 6
- The resulting conversion string is 64.
- If you convert values using a base greater than 10 (for instance, hexadecimal), you shall be printing out the appropriate single digit e.g. ‘A’ for 10, ‘F’ for 15.
- Value [161] converted to base [16]:
- 1
- A
- The resulting conversion string is A1.
- The following shall be how you print out your conversion string:
- printf(“The resulting conversion string is %s.\n”, yourConvName );
- Your application should be tolerant of miscues in the entry of information and reprompt the user, as needed. For example:
- Please enter a value to convert (0 -> 1024, -1 to Exit): 2048 <ENTER>
- *** Sorry, the value of 2048 is outside my limit of 0 to 1024. ***
- Please enter a value to convert (0 -> 1024, -1 to Exit): 512 <ENTER>
- Test Cases that you should demonstrate:
- Conversion 800 Base 2
- Conversion 800 Base 16
- Conversion 1024 Base 2
- Conversion 1024 Base 16
- Conversion 2048 Base 8
- Converison 200 Base 16
- Conversion 200 Base 5
- Conversion -1
- Possibly helpful hints:
- 1) One can use scanf(“%s”, &strArray[0]) to read in a string from the user and, then, utilize the string conversion function atoi(), defined in stdlib.h, to convert the value to integer.
- 2) One can use the knowledge than an ASCII ‘0’ (0x30) plus an integer 1 will be 0x31 or ASCII ‘1’.
- a) Additionally, ASCII ‘A’ (0x41) plus an integer 1 will be 0x42 or ASCII ‘B’.
- b) Use the int2hex() in bwh_strings.cpp function or create one of your own.
- 3) You may find strlen(), defined in string.h, useful in determining the length of your conversion string.
- 4) Use the string reverse snippet from bwh_strings.cpp or create one of your own.
- 5) Do not forget to initialize your conversion string to NULLs (0x00) before using it.
- 6) You may want to create a function, int getConvValue(), that prompts the user for the value to convert. It will return a value from -1 to 1024.
- 7) You may want to create a function, int getBaseValue(), that prompts the user for the base to use. It will return a value from 2 to 16.
- //
- //
- #include <stdio.h>
- //
- // define your "constant" up here
- // e.g. #define MACRONAME (MACROVALUE)
- // const type varname = value;
- #define CONV_LEN (11)
- int main( void )
- {
- //
- // declare your variables here
- //
- int myConversion;
- char yourConversion[CONV_LEN+1];
- //
- // provide the user the name of your application
- //
- printf(“ *** YourName Base Conversion Application ***”);
- do {
- // prompt user for value to convert and base to use
- // initialize the char array before you do work
- //
- if ((myConversion >= 0) && (myConversion <= 1024)) {
- //
- // perform your calculations here required
- //
- //
- // perform your output here
- //
- }
- } while ( myConversion > 0 );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement