Week 8 - Concerts Overview
This hands-on lab allows you to follow and experiment with the critical steps of developing a program including the program description, analysis, test plan, and implementation with C code. The example provided uses sequential, repetition, selection statements, functions, strings and arrays.
Program Description
Compute the value of the ticket sales for concerts and the total sales for a series of concerts. Then display the concert information in a sorted list.
Technologies used in this project:
Outline:
Notes:
Analysis
Pseudo-code:
Global variables:
Pseudo-Code:
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.
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:
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 <string.h> strcpy (group [0], "one"); fans [0][0] = 3; fans [0][1] = 12; fans [0][2] = 33; sales [0] = 23.23; |
8 |
printArray |
Call this function from main. Focus on getting this function working Check the output for each step. Lots of steps:
|
9 |
More test code |
Create another set of code like step 7 to test printArray more carefully |
10 |
Call getData in main |
Start with a printf statement saying “got here” in getData |
11 |
Print prompt |
For ticket prices of 3 categories, remove “got here” message |
12 |
Get ticket prices |
Use for loop to get prices into sales array Add for loop to print the values in that array – temporary code |
13 |
Get group loop |
For loop to get group name (index is i) Read group [i] Add test for name is period and break input loop if so TEST the period |
14 |
Get fan numbers |
Nested for loop over the number of categories (index is j) Read fans [i][j] Increment count since this should be a valid group Temporary: set sales [i] to 44.56 or some other random number |
15 |
Fix main |
Comment out the test code from steps 7 and 9 Insert the appropriate calls in main: getData (); printArray (); Test the code with real data! |
16 |
computeSales |
Get this code working, using a loop Add the call to this function to main |
17 |
Sorting |
Get the call to sorting active Add the print statement to say the following is sorted Print the “sorted” array (not sorted yet) |
18 |
findMinSales |
Get this code working – starting at an index, find the index of the smallest sales in the rest of the list Use print statements to make sure this is working |
19 |
switchRows |
Get this working – switch group name, the fans and the sales values |
20 |
Final check |
Try a number of different data sets here. |
C Code
The following is the C Code that will compile in execute in the online compilers.
// C code // This code will compute the values of the sales ticket sales for concerts // and sort the entries by those values // Developer: Faculty CMIS102 // Date: Jan 31, XXXX #include < stdio.h> #define MAXN 100 // max characters in a group/concert name #define MAXG 50 // max concerts/groups #define MAXC 3 // max categories char group [MAXG][MAXN]; int fans [MAXG][MAXC]; float prices [MAXC]; float sales [MAXG]; int count = 0; void printArray () { printf ("%15s%5s%5s%5s%10s\n", "Concert", "s1", "s2", "s3", "Sales"); 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; } // 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) switchRows (i, target); } // for each concert } // end function sortBySales void getData () { // 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; }
Learning Exercises for you to complete
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.
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.
Explain the purpose of each test case, and check your code against each of those cases.
Make those changes, test your code and confirm that it is working correctly.
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 |
Assignment Writing Help
Engineering Assignment Services
Do My Assignment Help
Write My Essay Services