|
|
|
|
|
|
|
As you can see from the bar graph, each star represents $500 in sales. No stars are printed if a department's sales are less than or equal to $250. |
|
|
|
|
|
|
|
|
Input: Two data files (store1 and store2), each containing |
|
|
|
|
|
|
|
|
Department ID number (int)
Number of business days (int)
Daily sales (several float values)
repeated for each department. |
|
|
|
|
|
|
|
|
Output: A bar graph showing total sales for each department. |
|
|
|
|
|
|
|
|
Discussion: Reading the input data from both files is straightforward. We need to open the files (let's call them store1 and store2) and read a department ID number, the number of business days, and the daily sales for that department. After processing each department, we can read the data for the next department, continuing until we run out of departments (EOF is encountered). Because the process is the same for reading store1 and store2, we can use one function for reading both files. All we have to do is pass the file name as a parameter to the function. We want total sales for each department, so this function has to sum the daily sales for a department as they are read. A function can be used to print the output heading. Another function can be used to print out each department's sales for the month in graphic form. |
|
|
|
|
|
|
|
|
There are three loops in this program: one in the main function (to read and process the file data), one in the function that gets the data for one department (to read all the daily sales amounts), and one in the function that prints the bar graph (to print the stars in the graph). The loop for the main function tests for EOF on both store1 and store2. One graph for each store must be printed for each iteration of this loop. |
|
|
|
|
|
|
|
|
The loop for the GetData function requires an iteration counter that ranges from 1 through the number of days for the department. Also, a summing operation is needed to total the sales for the period. |
|
|
|
|
|
|
|
|
At first glance, it might seem that the loop for the PrintData function is like any other counting loop, but let's look at how we would do this process by hand. Suppose we wanted to print a bar for the value 1850. We first would make sure the number was greater than 250, then print a star and subtract 500 from the original value. We would check again to see if the new value was greater than 250, then print a star and subtract 500. This process would repeat until the resulting value was less than or equal to 250. Thus, the loop requires a counter that is decremented by 500 for each iteration, with a termination value of 250 or less. A star is printed for each iteration of the loop. |
|
|
|
|
|