In: Computer Science
Hometown City USA is analyzing traffic patterns to determine
whether traffic lights are necessary at tracked intersections. Each
tracked intersection has a data file where each line contains the
number of cars passing through that intersection on a given day.
The data collected can cover any number of days; there can be any
number of lines per file. Design an algorithm solution to read a
given data file, calculate, and display the following:
number of days monitored at the intersection
maximum number of cars passing through the intersection on a
tracked day
average cars per day passing through the intersection
Include the following requirements in your algorithm design:
Use a starting main module to create needed variables, read data
from file, and display count and average of data in the file.
Use only simple data types, not arrays or lists in your
algorithm.
In a single loop to read from the file, use an accumulator and
counter to keep track of data read from file for calculating the
average. Do not display data contained in the file; only calculate
and display the number of days, highest number of cars for a day,
and average number of cars per day contained in the file.
Remember to close the file when reading is complete
1. Begin
2. Open the input file in read mode.
// count is for counting the number of days
//sum is for adding the total number of cars passed through that intersection
//high is set to -1 for finding the highest number of cars per day
// marker is set to True
3. Initialize count =0 and sum=0, high=-1, marker=True
4. While (marker) //Loop when True
// reads the record one by one to variable f
5. Read record from file to f.
6. IF EOF Then
7. exit from While
8. ELSE
9. count=count+1 // Increment the count
10. sum=sum+f //sum is added with the number of cars passed on that day
11. IF(f>high) Then // If f is a bigger value, replace high with new value of f
12. high=f
13. End IF
14. End IF
15 End While
16. Close the Input File
17. Calculate average=sum/count // getting the average number of cars per day
18. Print ("Number of days=",Count)
19. Print ("Highest number of cars for a day=",high)
20. Print("Average number of cars for a day=",average)
21. End