Advertisement
arthurtung

Untitled

Jul 22nd, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. Homework 4
  2.  
  3. 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.
  4.  
  5. You shall use the division method of base conversions with the algorithm provided in the presentation on Canvas (CmpE30_Lec03a_BaseConversion).
  6.  
  7. You shall prompt the use for a value, base 10, to convert ranging from 0 -> 1024.
  8. Please enter a value to convert (0 -> 1024, -1 to Exit):
  9.  
  10. 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.
  11.  
  12. You shall prompt the user for base to use in conversion ranging from 2 -> 16.
  13. Please enter a base to use in conversion (2 -> 16):
  14.  
  15. 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.]
  16.  
  17. You should display a message to the user indicating what is happening. You shall display the final result as a string.
  18. Value [100] converted to base [16]:
  19. 4
  20. 6
  21. The resulting conversion string is 64.
  22.  
  23. 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.
  24. Value [161] converted to base [16]:
  25. 1
  26. A
  27. The resulting conversion string is A1.
  28.  
  29. The following shall be how you print out your conversion string:
  30. printf(“The resulting conversion string is %s.\n”, yourConvName );
  31.  
  32.  
  33.  
  34.  
  35. Your application should be tolerant of miscues in the entry of information and reprompt the user, as needed. For example:
  36. Please enter a value to convert (0 -> 1024, -1 to Exit): 2048 <ENTER>
  37. *** Sorry, the value of 2048 is outside my limit of 0 to 1024. ***
  38. Please enter a value to convert (0 -> 1024, -1 to Exit): 512 <ENTER>
  39.  
  40. Test Cases that you should demonstrate:
  41. Conversion 800 Base 2
  42. Conversion 800 Base 16
  43. Conversion 1024 Base 2
  44. Conversion 1024 Base 16
  45. Conversion 2048 Base 8
  46. Converison 200 Base 16
  47. Conversion 200 Base 5
  48. Conversion -1
  49.  
  50.  
  51. Possibly helpful hints:
  52. 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.
  53.  
  54. 2) One can use the knowledge than an ASCII ‘0’ (0x30) plus an integer 1 will be 0x31 or ASCII ‘1’.
  55. a) Additionally, ASCII ‘A’ (0x41) plus an integer 1 will be 0x42 or ASCII ‘B’.
  56. b) Use the int2hex() in bwh_strings.cpp function or create one of your own.
  57.  
  58. 3) You may find strlen(), defined in string.h, useful in determining the length of your conversion string.
  59.  
  60. 4) Use the string reverse snippet from bwh_strings.cpp or create one of your own.
  61.  
  62. 5) Do not forget to initialize your conversion string to NULLs (0x00) before using it.
  63.  
  64. 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.
  65.  
  66. 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.
  67. //
  68. //
  69. #include <stdio.h>
  70.  
  71. //
  72. // define your "constant" up here
  73. // e.g. #define MACRONAME (MACROVALUE)
  74. // const type varname = value;
  75. #define CONV_LEN (11)
  76.  
  77.  
  78. int main( void )
  79. {
  80. //
  81. // declare your variables here
  82. //
  83. int myConversion;
  84. char yourConversion[CONV_LEN+1];
  85.  
  86. //
  87. // provide the user the name of your application
  88. //
  89. printf(“ *** YourName Base Conversion Application ***”);
  90.  
  91. do {
  92. // prompt user for value to convert and base to use
  93.  
  94. // initialize the char array before you do work
  95.  
  96. //
  97. if ((myConversion >= 0) && (myConversion <= 1024)) {
  98. //
  99. // perform your calculations here required
  100. //
  101.  
  102. //
  103. // perform your output here
  104. //
  105. }
  106. } while ( myConversion > 0 );
  107.  
  108. return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement