akosiraff

Download CMIS 102 Hands-On Lab Week 8 – Concerts

Mar 17th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.65 KB | None | 0 0
  1.  
  2. Download: https://solutionzip.com/downloads/cmis-102-hands-on-lab-week-8-concerts/
  3. Page 1 of 12
  4. CMIS 102 Hands-On Lab
  5. Week 8 – Concerts
  6. Overview
  7. This hands-on lab allows you to follow and experiment with the critical steps of developing a program
  8. including the program description, analysis, test plan, and implementation with C code. The example
  9. provided uses sequential, repetition, selection statements, functions, strings and arrays.
  10. Program Description
  11. Compute the value of the ticket sales for concerts and the total sales for a series of concerts. Then
  12. display the concert information in a sorted list.
  13. Technologies used in this project:
  14. ? strings stored as char arrays
  15. ? 2 dimensional char and int arrays
  16. ? getting data from the user into the various arrays
  17. ? using only part of an array for valid data
  18. ? computing array arithmetic – vector inner products to compute a value
  19. ? sorting using selection sort
  20. ? displaying data in arrays
  21. Interaction
  22. Outline:
  23. 1. Get the ticket price for each 3 categories.
  24. a. float float float
  25. 2. Get the band name and numbers of fans for each category of each concert.
  26. a. String int int int
  27. b. NOTE: spaces are not allowed in the band names, so use underscores instead of spaces.
  28. c. Period will end the input.
  29. 3. Internal computations:
  30. a. Compute the value of the sales for each concert.
  31. b. Compute the total value of the ticket sales.
  32. c. Sort the concerts by value of the ticket sales.
  33. 4. Display the resulting data in a nice format.
  34. Notes:
  35. 1. This program is not expected to behave nicely if the input does not follow the specifications
  36. above.
  37. 2. All numbers this program will handle should be of moderate size. The exact meaning of
  38. moderate is made precise by the data types, print statements, and array sizes.
  39. Page 2 of 12
  40. Analysis
  41. 1. Since we are going to do sorting, we will need to hold the data in arrays.
  42. 2. We will use functions to simplify the code, and reflect a modular approach to the design of the
  43. project.
  44. 3. We will use global variables to reduce the amount information transferred in parameter lists.
  45. 4. We will use a fixed number of categories, but let the number of concerts by determined by the
  46. user at run time, up to a maximum value.
  47. 5. The user will end inputting the group information with a flag value, a period.
  48. 6. Basic data types
  49. a. ticket prices – float
  50. b. name of concert/band – char array
  51. c. number of concerts and number of fans in each category at each concert – int
  52. d. number of concerts – int.
  53. e. value of tickets – float
  54. 7. We will use nested loops to handle most operations:
  55. a. reading the ticket prices
  56. b. reading the group name and attendance
  57. c. computing the value of the ticket sales
  58. d. printing the array of data
  59. e. sorting the arrays by the value of the ticket sales
  60. Page 3 of 12
  61. Pseudo-code:
  62. Global variables:
  63. ? Constants using define declarations
  64. o MAXN – the maximum number of characters in a band name
  65. o MAXG – the maximum number of concerts/bands/groups
  66. o MAXC – the maximum number of categories
  67. ? Arrays:
  68. o char group [MAXG][MAXN] – the name of each group
  69. o int fans [MAXG][MAXC] – the number of fans at a concert by category
  70. o float prices [MAXC] – the ticket price of each category
  71. o float sales [MAXG] – the total value of the tickets for each concert
  72. ? int count
  73. o the actual number of active concerts/bands/groups
  74. o this number is determined at run time in the getData function
  75. o used throughout the rest of the program to only look at data in the group, fans and sales
  76. arrays that is actually active.
  77. Pseudo-Code:
  78. 1. main
  79. a. getData – get ticket prices by category & get data for the concerts
  80. b. computeSales – compute sales for each concert
  81. c. printArray – print raw concert data
  82. d. sortBySales – sort the concerts by value of sales, use selection sort
  83. i. findMinSales – find min sales in unsorted rest of array
  84. ii. switchRows – as needed
  85. e. printArray – print sorted concert data
  86. 2. getData
  87. a. prompt for ticket prices
  88. b. get ticket prices into prices array
  89. c. prompt for band name and fans in each category at each concert
  90. d. insert data into appropriate arrays, ending with a period
  91. i. loop on concert
  92. 1. get group name
  93. 2. loop inside on number of categories
  94. 3. computeSales
  95. a. for each group
  96. i. sales for that group = 0
  97. ii. for each category
  98. 1. sales = sales + price of category * number of fans in that category
  99. Page 4 of 12
  100. 2.
  101. 4. printArray
  102. a. print column headers
  103. b. for each group
  104. i. print the group name
  105. ii. for each category
  106. 1. print the number of fans in that category
  107. iii. print the sales for that concert/group
  108. 5. sortBySales
  109. a. COMMENTS: for each row (group + fans + sales)
  110. i. for each row index, starting at index 0
  111. ii. the rows that have indices less than the current index are sorted, and are
  112. already the smallest sales elements in the entire data set.
  113. iii. thus, the next step is to find the smallest sales row in the remaining elements
  114. and switch that row with the current index row
  115. b. for index = 0 to number of active rows, step index by 1
  116. i. target = findMinSales (index)
  117. ii. if target not equal to index, switchRows (target, index)
  118. 6. findMinSales (index)
  119. a. set target as index
  120. b. min
  121. c. for the rest of the array, starting at index+1, indexing by i
  122. i. if sales [i] < min 1. target = i 2. min = sales [target] d. return target 7. switchRows (m, n) a. Temporary locations i. char tc ii. int ti iii. float v b. switch group names by stepping through all the letters (i) in each char array: i. tc = group [m][i] ii. group [m][i] = group [n][i] iii. group [n][i] = tc c. switch fan entries, stepping by (i) over the fans array: i. ti = fans [m][i] ii. fans [m][i] = fans [n][i] iii. fans [n][i] = ti d. switch sales: i. v = sales [m] ii. sales [m] = sales [n] iii. sales [n] = v Page 5 of 12 Test Plan To verify this program is working properly a variety of test cases should be used. The following is just one test case, which only tests a very few aspects of the program. You should be thinking about other data sets to test other aspects of the program, and also create more realistic test sets. Test Case Input Actual Output using repl.it – user input in red: 1 1 2 3 a 1 2 3 b 3 3 1 c 5 3 1 d 3 3 5 e 1 1 2 f 9 4 3 g 4 5 6 . Enter ticket prices in each of 3 cateogories: 1 2 3 -- Enter group and fans in 3 categories . to finish entries: a 1 2 3 b 3 3 1 c 5 3 1 d 3 3 5 e 1 1 2 f 9 4 3 g 4 5 6 . Concert s1 s2 s3 Sales a 1 2 3 14.00 b 3 3 1 12.00 c 5 3 1 14.00 d 3 3 5 24.00 e 1 1 2 9.00 f 9 4 3 26.00 g 4 5 6 32.00 --- Sorted --- Concert s1 s2 s3 Sales e 1 1 2 9.00 b 3 3 1 12.00 c 5 3 1 14.00 a 1 2 3 14.00 d 3 3 5 24.00 f 9 4 3 26.00 g 4 5 6 32.00 ... bye ... Page 6 of 12 Coding Plan These are the steps taken to develop the base code of this project. You should start with this code to address the Learning Exercises below. This is a rather complex project, and it should be developed in SMALL steps, making SMALL changes to the code and testing each change carefully before moving on to the next step. Since we have a pretty good pseudo-code representation of this project, and a good test plan, we should be able to develop the steps we think this project will require. We are guided here by the requirements of our environment, particularly getting the C compiler to accept our code at each step without error messages. It is also helpful to have some idea of how long each step should take, so that if our actual time trying to complete a step is getting out of hand, we know it is time to change our strategy for that step by, for example: ? taking a break, ? looking for more specific information (don’t take too long here either!), ? asking someone (your instructor is the best choice) for help! Page 7 of 12 Task Description Notes 1 Hello world Get the basic environment working Get main method started Even change the message to Bye 2 #define stuff These tokens will be used in the array declarations, and throughout the code Getting the syntax of these declarations correct can be a challenge 3 Declare the arrays And count The global variables this program will use. Get this syntax to work, and work with the define declarations 4 getData function Just get the declaration to compile correctly 5 Call getData in main Making sure we have the call syntax correct 6 Other function declarations As specified in the pseudo-code. Again, we want to check with the compiler that we are using the correct syntax 7 TEST CODE: count = 1 values to one group Give values for one concert in main: count = 1; // following requires #include
  123. strcpy (group [0], “one”);
  124. fans [0][0] = 3;
  125. fans [0][1] = 12;
  126. fans [0][2] = 33;
  127. sales [0] = 23.23;
  128. 8 printArray Call this function from main.
  129. Focus on getting this function working
  130. Check the output for each step.
  131. Lots of steps:
  132. ? header line
  133. ? nested loops: loop over all groups,
  134. ? the group name, loop over the fan counts, sales
  135. 9 More test code Create another set of code like step 7 to test printArray more
  136. carefully
  137. 10 Call getData in main Start with a printf statement saying “got here” in getData
  138. 11 Print prompt For ticket prices of 3 categories, remove “got here” message
  139. 12 Get ticket prices Use for loop to get prices into sales array
  140. Add for loop to print the values in that array – temporary code
  141. 13 Get group loop For loop to get group name (index is i)
  142. Read group [i]
  143. Add test for name is period and break input loop if so
  144. TEST the period
  145. 14 Get fan numbers Nested for loop over the number of categories (index is j)
  146. Read fans [i][j]
  147. Increment count since this should be a valid group
  148. Temporary: set sales [i] to 44.56 or some other random number
  149. Page 8 of 12
  150. 15 Fix main Comment out the test code from steps 7 and 9
  151. Insert the appropriate calls in main:
  152. getData ();
  153. printArray ();
  154. Test the code with real data!
  155. 16 computeSales Get this code working, using a loop
  156. Add the call to this function to main
  157. 17 Sorting Get the call to sorting active
  158. Add the print statement to say the following is sorted
  159. Print the “sorted” array (not sorted yet)
  160. 18 findMinSales Get this code working – starting at an index, find the index of the
  161. smallest sales in the rest of the list
  162. Use print statements to make sure this is working
  163. 19 switchRows Get this working – switch group name, the fans and the sales values
  164. 20 Final check Try a number of different data sets here.
  165. Page 9 of 12
  166. C Code
  167. The following is the C Code that will compile in execute in the online compilers.
  168. // C code
  169. // This code will compute the values of the sales ticket sales for concerts
  170. // and sort the entries by those values
  171. // Developer: Faculty CMIS102
  172. // Date: Jan 31, XXXX
  173. #include
  174. #define MAXN 100 // max characters in a group/concert name
  175. #define MAXG 50 // max concerts/groups
  176. #define MAXC 3 // max categories
  177. char group [MAXG][MAXN];
  178. int fans [MAXG][MAXC];
  179. float prices [MAXC];
  180. float sales [MAXG];
  181. int count = 0;
  182. void printArray () {
  183. printf (“%15s%5s%5s%5s%10s\n”,
  184. “Concert”, “s1”, “s2”, “s3”, “Sales”);
  185. for (int i = 0; i < count; i++) { printf ("%15s", group [i]); for (int j = 0; j < MAXC; j++) { printf ("%5d", fans[i][j]); } // end for each category printf ("%10.2f\n", sales [i]); } // end for each group } // end function printArray void computeSales () { for (int i = 0; i < count; i++) { sales [i] = 0; for (int j = 0; j < MAXC; j++) { sales [i] += prices [j] * fans [i][j]; } // end for each category } // end for each group } // end function computeSales void switchRows (int m, int n) { char tc; int ti; float v; // printf ("Switching %d with %d\n", m, n); for (int i = 0; i < MAXN; i++) { tc = group [m][i]; group [m][i] = group [n][i]; group [n][i] = tc; } // end for each character in a group name for (int i = 0; i < MAXC; i++) { ti = fans [m][i]; fans [m][i] = fans [n][i]; fans [n][i] = ti; Page 10 of 12 } // end for each fan category v = sales [m]; sales [m] = sales [n]; sales [n] = v; } // end switch int findMinSales (int m) { float min = sales [m]; int target = m; for (int i = m+1; i < count; i++) if (sales [i] < min) { min = sales [i]; target = i; } // end new max found return target; } // end function findMinSales void sortBySales () { int target; for (int i = 0; i < count; i++) { target = findMinSales (i); if (target > i)
  186. switchRows (i, target);
  187. } // for each concert
  188. } // end function sortBySales
  189. void getData () {
  190. // for (int i = 0; i < MAXG; i++) sales [i] = 0; printf ("Enter ticket prices in each of %d cateogories: ", MAXC); for (int i = 0; i < MAXC; i++) scanf ("%f", &prices [i]); printf ("-- Enter group and fans in %d categories\n", MAXC); printf (" . to finish entries:\n"); for (int i = 0; i < MAXG; i++) { scanf ("%s", &group[i]); if (group [i][0] == '.') break; count++; for (int j = 0; j < MAXC; j++) scanf ("%d", &fans[i][j]); } // end for each group } // end function getData int main(void) { getData (); computeSales (); printArray (); printf ("\n --- Sorted ---\n"); sortBySales (); printArray (); printf("... bye ...\n"); return 0; } Page 11 of 12 Learning Exercises for you to complete 1. Demonstrate you successfully followed the steps in this lab by preparing screen captures of you running the lab as specified in the Instructions above. a. Add a function to print welcome information, including your name and an introduction to the project, and, of course, call this function as the first instruction in main. NOTE: It is convenient to put each test case into its own data file so you don’t have to type a rather large set of input over and over again. Then you can just copy/paste the data into the interaction panel of the IDE you are using. 2. Modify the program to add a function to compute and display the total sales for all the concerts. Support your experimentation with screen captures of executing the new code. 3. Enhance the program to allow the user to enter 4 categories. Explain the changes you made to the code, and create appropriate test data files. Support your experimentation with screen captures of executing the new code. 4. Prepare a new test table with at least 2 more test cases listing input and expected output for the new code you created, supporting 4 categories. 5. Create a variety of test cases focusing on the sorting algorithm, such as the final element is the smallest, the entire set is already sorted, etc. Explain the purpose of each test case, and check your code against each of those cases. 6. Try using different inputs: a. What changes would you suggest to handle larger, more realistic numbers? b. What happens if any of the numbers, such as the ticket prices, are negative? c. What are your recommendations concerning negative input values? 7. What changes should be made to the code if the customer wished to sort on the number of fans in category 1, the first of the three (or four) categories? Make those changes, test your code and confirm that it is working correctly. Page 12 of 12 Grading guidelines Exercise Submission Points 1 Demonstrates the successful execution of this Lab within an online compiler. Adds and calls a function to display introductory information. Provides supporting screen captures. 10 2 Modifies the code to add a function to sum each concert. Supports your experimentation with screen captures of executing the new code. 10 3 Modifies the code to handle 4 categories, with explanation. 10 4 Prepares at least 2 additional test cases for 4 categories. 10 5 Prepares test cases focusing on sorting algorithm. Supports your experimentation with screen captures of executing the new code. 15 6 Documents experiments with unusual but valid inputs. 10 7 Modifies the code to sort on category 1. Creates appropriate test cases. Documents results showing correctness of modifications. 15 8 Document is well-organized, and contains minimal spelling and grammatical errors. 20 Total 100
  191. Download: https://solutionzip.com/downloads/cmis-102-hands-on-lab-week-8-concerts/
Add Comment
Please, Sign In to add comment