Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.89 KB | None | 0 0
  1. Penny Lab
  2.  
  3. Implement the Penny Lab in Chapter 8 of Stegman (p. 195), but modified as decribed in following instructions. The output format should be as shown in the example output below. Don't open the file "csis.txt". Also, your program should take as input the amount of money in dollars. This is different from the version in Stegman, which calculates based on a fixed amount of money, namely, one million dollars.
  4.  
  5. Use the following program structure and main function:
  6.  
  7. #include <stdio.h>
  8.  
  9. void outputheader( )
  10. {
  11. /* add code to output the table header */
  12. /* --> between here */
  13.  
  14. /* <-- and here */
  15. }
  16.  
  17. int generatetable( /* add parameter here */ )
  18. {
  19. /* add declarations and code to generate the table */
  20. /* (based on the parameter value) */
  21. /* and return the number of days required to accumulate the specified amount */
  22. /* --> between here */
  23.  
  24. /* <-- and here */
  25. }
  26.  
  27. int main( void )
  28. {
  29. /* add declarations and code to input the amount of money, */
  30. /* call the function to output the table header, */
  31. /* and call the function to generate the table, */
  32. /* and print the number of days required */
  33. /* --> between here */
  34.  
  35. /* <-- and here */
  36.  
  37. while( 1 ) getchar( );
  38. return 0;
  39. }
  40.  
  41.  
  42. Make sure that the structure of the code reflects all of the following discipline.
  43.  
  44. Look closely at the columns in the output using the cursor to count the column widths. For example, the first column under the header “DAY” has exactly 10 spaces. Between the first column and the second column, allow one space to unconditionally separate the columns. There should be a space in the format strings for this space. Then count the remaining spaces to determine the width of the second column. For the third column, repeat what was done for the second column. Make sure every format string for the output lines of the header and the output lines of the table consistently respect these column widths and the spaces separating columns.
  45.  
  46. Please model the account deposit and balance situation to reflect reality as closely as possible. For example, to print the first line, one might be tempted to start the balance at 0.01. But please don’t do that. A bank balance (of the type in this problem) doesn’t automatically start with a non-zero balance. Banks in this problem don’t give away any free money. The balance must start at 0.00, and the only way the balance increases is by deposits. So, at the beginning of day one, the balance is 0.00. Although that 0.00 is not printed in the output, it should be reflected by the value of the variable representing the balance. Then, on day 1, a deposit of 0.01 is made. There should be a variable representing the deposit amount, and for the first day, that variable is equal to 0.01, and that gets printed on the first line of the table. The new balance at the end of the day (0.01), which also gets printed on the first line of the table, is equal to the balance at the beginning of the day (0.00) plus the amount of the deposit on that day (0.01). There should be an assignment statement that models the equality described in the previous sentence.
  47.  
  48. Please note that the problem states that we are trying to find the number of days required to have a balance that is at least the amount of money specified in the input. “At least” means “greater than or equal to”. So, in the first example output below, on day 16, we have a balance of 999999.99. Since that is not greater than or equal 1000000.00, we must keep going and consider another day. On day 17, the balance is greater than or equal to 1000000.00, and day 17 is the first such day. So that’s the last day in the output, and the function called generatetable returns 17.
  49.  
  50. However, if on day 16 the balance had been one penny more, i.e., 1000000.00 (instead of 999999.99), then day 16 would have been the last day. One of your test cases should test this situation of exact equality. For example, on day 8, the balance should be 2.55. You could try specifying 2.55 as an input value indicating the amount of money we are trying to accumulate.
  51.  
  52. Please re-read and study Chapter 6 for examples on functions and passing arguments to functions. As stated in the Stegman instructions for the Penny Lab, the generatetable function should be passed an input parameter of type double that indicates how much money to accumulate. This information must be passed using an argument or parameter. It must not be passed as a global variable, which is a variable that is declared outside of a function.
  53.  
  54. The generatetable function returns the number of days required to accumulate the specified amount of money. This is natural because as generatetable goes through the accumulation, day by day, this function can take note of exactly when specified amount of money has been accumulated and how many days this required. When the function main calls generatetable, the function main must actually make use of the return value from generatetable. Typically, this value would be saved in a variable. This is how main knows the number of days. Do not try to avoid all of this by having generatetable print out how many days are required. A key part of this lab assignment is having generatetable return the number of days and having main use that returned value.
  55.  
  56. Follow best practices. Best practice rules applicable to this lab include:
  57. No global variables. Pass values between functions using arguments/parameters.
  58. Check for compiler warnings and, if present, resolve them.
  59. Be conscious of possible implicit type conversions and maintain disciplined control of type conversions. No type mismatches on any operator, including assignment operators, comparison operators, arithmetic operators, etc. Don't allow the compiler to add implicit type conversion operators. Avoid type conversions by using constants of the correct type (e.g., use a double constant to compare or assign to a double variable). Otherwise, if type conversion is truly needed, use type casts so that the conversion is explicit.
  60.  
  61. Test your program on three different inputs (i.e., three different amounts of money). The first one should one million dollars (1000000.00), which is the fixed amount of money from Stegman. The second and third amounts should be different and something that you create to show additional interesting test cases. Fill in the input and resulting output below.
  62. General rules: No unused variables. At every point a variable is used, make sure the variable has been defined previously. Fully parenthesize expressions. Don’t use features from later chapters (after chapter 8) or features that have not been introduced. No arrays. No global variables. Use the exact formulas given here. Don’t use formulas found elsewhere.
  63. Cardinal rules: No tabs. No backspaces. No long lines. No modifying the format string based on the data. No data type mismatches. Line up the decimal points.
  64. After you have added your C program and the program’s input and output (below, on the next two pages), complete the checklist and submit this word document via Canvas.
  65.  
  66. Submissions without a completed checklist will not be graded.
  67. Place your C program here, in place of the example program below:
  68.  
  69. #include <stdio.h>
  70.  
  71. void outputheader( )
  72. {
  73. /* add code to output the table header */
  74. /* --> between here */
  75.  
  76. /* <-- and here */
  77. }
  78.  
  79. int generatetable( /* add parameter here */ )
  80. {
  81. /* add declarations and code to generate the table */
  82. /* (based on the parameter value) */
  83. /* and return the number of days required to accumulate the specified amount */
  84. /* --> between here */
  85.  
  86. /* <-- and here */
  87. }
  88.  
  89. int main( void )
  90. {
  91. /* add declarations and code to input the amount of money, */
  92. /* call the function to output the table header, */
  93. /* and call the function to generate the table, */
  94. /* and print the number of days required */
  95. /* --> between here */
  96.  
  97. /* <-- and here */
  98.  
  99. while( 1 ) getchar( );
  100. return 0;
  101. }
  102.  
  103. For your first test case, use the input data below:
  104. 1000000.00
  105.  
  106. Place the output of your C program for the first test case here, in place of the example output below:
  107.  
  108.  
  109.  
  110. DAY DEPOSIT BALANCE
  111. --- ------- -------
  112. 1 0.01 0.01
  113. 2 0.02 0.03
  114. 3 0.04 0.07
  115. 4 1.08 0.15
  116. 5 0.16 0.31
  117. 6 0.32 0.63
  118. 7 0.64 1.27
  119. 8 1.28 2.55
  120. 9 2.56 5.11
  121. 10 5.12 10.23
  122. 11 10.24 20.47
  123. 12 20.48 40.95
  124. 13 555.55 666.66
  125. 14 6666.66 7777.77
  126. 15 77777.77 88888.88
  127. 16 888888.88 999999.99
  128. 17 999999.99 1234567.89
  129.  
  130. It took 17 days to accumulate at least $1000000.00.
  131.  
  132.  
  133. d
  134. Place the input for your second test case here, in place of the example input data below:
  135. 1000000.00
  136.  
  137. Place the output of your C program for the second test case here, in place of the example data below:
  138. DAY DEPOSIT BALANCE
  139. --- ------- -------
  140. 1 0.01 0.01
  141. 2 0.02 0.03
  142. 3 0.04 0.07
  143. 4 1.08 0.15
  144. 5 0.16 0.31
  145. 6 0.32 0.63
  146. 7 0.64 1.27
  147. 8 1.28 2.55
  148. 9 2.56 5.11
  149. 10 5.12 10.23
  150. 11 10.24 20.47
  151. 12 20.48 40.95
  152. 13 555.55 666.66
  153. 14 6666.66 7777.77
  154. 15 77777.77 88888.88
  155. 16 888888.88 999999.99
  156. 17 999999.99 1234567.89
  157.  
  158. It took 17 days to accumulate at least $1000000.00.
  159.  
  160.  
  161.  
  162. Place the input for your third test case here, in place of the example input data below:
  163. 1000000.00
  164.  
  165. Place the output of your C program for the third test case here, in place of the example data below:
  166. DAY DEPOSIT BALANCE
  167. --- ------- -------
  168. 1 0.01 0.01
  169. 2 0.02 0.03
  170. 3 0.04 0.07
  171. 4 1.08 0.15
  172. 5 0.16 0.31
  173. 6 0.32 0.63
  174. 7 0.64 1.27
  175. 8 1.28 2.55
  176. 9 2.56 5.11
  177. 10 5.12 10.23
  178. 11 10.24 20.47
  179. 12 20.48 40.95
  180. 13 555.55 666.66
  181. 14 6666.66 7777.77
  182. 15 77777.77 88888.88
  183. 16 888888.88 999999.99
  184. 17 999999.99 1234567.89
  185.  
  186. It took 17 days to accumulate at least $1000000.00.
  187.  
  188.  
  189. CHECKLIST
  190.  
  191. For each rule, check whether or not the rule is satisfied, and then enter OK or NOT OK in the status column.
  192.  
  193. Rule # Description Status
  194. 1 A newline is required at the end of every output line. Check the last line of output. The cursor should not be at the end of the last line of output. It should be at the beginning of the next line.
  195. 2 Don’t initialize variables in the declaration. (Exception discussed later.)
  196. 3 All declarations at the top of the function.
  197. 4 No type mismatches.
  198. 5 Use the most natural data type. Use int for things that are naturally an integer. Don’t use double as an upper limit for an integer for loop. Use double for things that are naturally floating-point.
  199. 6 Use the most natural variable name. Use integer names for integer variables. Don’t use integer names for floating-point variables. The name of the variable must accurately describe the meaning of the variable. The variable naming scheme should be applied as consistently as is reasonably possible.
  200. 7 One meaning, one variable. One variable, one meaning.
  201. 8 Don’t use float. Use double instead.
  202. 9 Don’t modify the format string based on the data value.
  203. 10 No manual editing of the code and the output once copied into the Word doc. Copy the exact code and output of the program into the Word doc. The output shown in the Word doc must match exactly the actual output of the program.
  204. 11 Font and line spacing of the code and input/output must match the original font and line spacing in the assignment doc.
  205. 12 Copy the complete code into the Word doc. For example, no missing #includes at the top and no missing braces at the end.
  206. 13 The code must be separated from adjacent paragraphs as in the original assignment doc.
  207. 14 No Visual Studio compiler warnings or errors. All warnings must be resolved prior to submitting the code.
  208. 15 Don’t use programming language features that have not been covered in the reading and not discussed in class.
  209. 16 No tabs in the output. Don’t try to output tabs to keep columns lined up.
  210. 17 No backspaces. Output the correct characters the first time. Don’t output something that might be incorrect and then try to fix it by outputting backspaces.
  211. 18 No long lines in the source code. Break long lines into multiple lines.
  212. 19 The only numbers that should appear in the code are basic facts (such as 365 days in a year, or 31 days in January) or conversion factors (such as 1600 meters per mile).
  213. 20 For numbers that do appear, it is usually preferable to give them a descriptive name using a constant variable with an initializer (e.g., int meters_per_mile = 1600;) or a #define constant (e.g., #define METERS_PER_MILE 1600), and the use that name in place of the number.
  214. 21 Don’t modify the input variables. Don’t use input variables for any other purpose.
  215. 22 No uninitialized variables. At every point where a variable is used, make sure you are clear about what previously executed statement assigned a value to that variable.
  216. 23 No unused variables. At every point where a variable is assigned to, make sure you are clear about where that value will be used in the later statements that will be executed.
  217. 24 If code does not work, say so, and describe the problem in detail. If code does work, say so.
  218. 25 Read between the lines of the previous rules. Do high-quality, disciplined work.
  219. 26 I worked independently to produce this code. The entire code is my individual work product, and I can explain how the code works. I have not taken any part of the code from any other source, including other students or the internet. (Taking code from the Stegman book as described in the assignment is ok.)
  220.  
  221.  
  222.  
  223. Add any needed explanation here.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement