1 Learning Outcomes In this assignment you will demonstrate your understanding o
ID: 3884781 • Letter: 1
Question
1 Learning Outcomes
In this assignment you will demonstrate your understanding of loops and if statements by writing a program that sequentially processes a file of input data. You are also expected to make use of functions and arrays.
2 The Story...
VicRoads, the road and traffic authority in Victoria, has released a set of traffic accident records for the last five years1 . This data set allows us to analyse traffic accidents based on a variety of factors such as location, time, weather condition, road condition, road users, etc. The data provide insights for improving road safety, such as whether to redesign road networks, relocate traffic cameras, adjust speed limits, etc.
Below is a sample list of traffic accident records, where the six columns represent unique record ID, accident location longitude, accident location latitude, accident date (day/month/year), accident time of day (hour), and accident day of week.
2693452 145.060689 -37.810373 26/5/12 11 Saturday
2693453 144.991172 -37.883156 6/6/12 15 Wednesday
2693454 145.009458 -37.826952 24/5/12 09 Thursday
2693455 145.134597 -37.841545 6/6/12 17 Wednesday
2693456 145.294599 -37.888597 22/5/12 16 Tuesday
2693458 145.008044 -37.783914 6/6/12 18 Wednesday
2693459 145.125164 -37.659316 6/6/12 15 Wednesday
2693460 145.211208 -37.982087 5/6/12 17 Tuesday
2693461 145.110783 -37.872920 6/6/12 16 Wednesday
2693462 144.960667 -37.827123 6/6/12 17 Wednesday
For example, the first line of the list represents traffic accident #2693452. The traffic accident happened at <145.060689, 37.810373>, at 11 in the morning of Saturday, May 26, 2012.
3 Your Task
In this assignment, your task is to analyse the spatial and temporal distributions of traffic accidents. You will be given a list of traffic accident records like the one shown above. Particularly, each line of the list contains the information of a traffic accident separated by spaces (‘ ’), including the unique ID (a 7-digit integer), the accident location longitude (a real number with 6 digits after the decimal point), the accident location latitude (a real number with 6 digits after the decimal point), the accident date (three integers in the format of day/month/year, between 1/1/2012 and 1/8/2017). the accident time of day (an integer between 0 and 23), and the accident day of week (a string in {“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”}). You may assume that the list will always be correctly formatted and contain at least 1 and at most 99 records.
Your task is to write a program that reads the list and output statistics calculated on it. The assignment consists of the following three stages
3.1 Stage 1 - Processing the First Record
You can complete this stage without using strings. However, you may also use strings if you are confident that you will be proceeding to Stage 3. Write a program that reads the first line of the input data, and prints out for the first record: the unique ID, the longitude, the latitude, the date, the time, and the distance (in kilometres, 2 digits before and after decimal point) between the accident location and a reference point with a coordinate of <144.963123, 37.810592>^2
Given the coordinates of two points p1 = <long1, lat1> and p2 = <long2, lat2>, where long(i) and lat(i) (i = 1, 2) represent the longitude and latitude, the distance (in kilometres) between p1 and p2, represented by dist(p1, p2), is calculated based on the haversine formula as follows.
dist(p1, p2) = 6371 · angle distance, where
angle distance = 2 · atan2(sqrt(chord length), sqrt(1 chord length)),
chord length = sin^2 (toRadian(lat2 lat1)/2) + cos(toRadian(lat1)) · cos(toRadian(lat2)) · sin^2 (toRadian(long2 long1)/2)
In the equation above, toRadian(x) = x · (3.14159/180) is a function that converts a longitude or latitude coordinate x to its radian value. You need to implement this function and define the constants properly in your code. Note that you should not use the constant M_PI provided by the math.h library as it is not supported by the assignment submission server under the assignment compilation settings.
The functions atan2(), sqrt() sin(), and cos() are provided by the math.h library. If you use this library, make sure to add the “-lm” flag to the “gcc” command at compilation. The output of this stage given the above sample input should be (where “mac:” is the command prompt):
mac: ./assmt1 < test0.txt
Stage 1 ==========
Accident: #2693452
Location: <145.060689, -37.810373>
Date: 26/5/12
Time: 11
Distance to reference: 08.57
Here, 08.57 is the distance between the location of the first traffic accident, <145.060689, 37.810373>, and the reference point, <144.963123, 37.810592>, as calculated using the haversine formula above. As this example illustrates, the best way to get data into your program is to save it in a text file (with a “.txt” extension, jEdit can do this), and then execute your program from the command line, feeding the data in via input redirection (using <).
In your program, you will still use the standard input functions such as scanf() or getchar() to read the data. Input redirection will direct these functions to read the data from the file, instead of asking you to type it in by hand. This will be more convenient than typing out the test cases manually every time you test your program. Our auto-testing system will feed input data into your submissions in this way as well. To simplify the assessment, your program should not print anything except for the data requested to be output (as shown in the output example).
Note that we will provide a sample test file “test0.txt” in LMS. You may download this file directly and use it to test your code before submission. This file is created under MacOS. Under Windows systems, the entire file may be displayed in one line if opened in the Notepad app. You do not need to edit this file to add line breaks. The ‘ ’ characters are already contained in the file. They are not displayed properly but the scanf() and getchar()functions in C can still recognise them correctly.
3.2 Stage 2 - Processing the Rest of the Records
You can complete this stage without using strings. However, you may also use strings if you are confident that you will be proceeding to Stage 3.
Now modify your program so that the distance to the reference point <144.963123, 37.810592> for all of the input accident data are computed and visualised. You may assume that the distance values are within the range of (0, 100). On the same sample input data, the additional output for this stage should be:
Stage 2
==========
Accident: #2693452, distance to reference: 08.57 |---------
Accident: #2693453, distance to reference: 08.44 |---------
Accident: #2693454, distance to reference: 04.46 |-----
Accident: #2693455, distance to reference: 15.45 |---------+------
Accident: #2693456, distance to reference: 30.37 |---------+---------+---------+-
Accident: #2693458, distance to reference: 04.94 |-----
Accident: #2693459, distance to reference: 22.05 |---------+---------+---
Accident: #2693460, distance to reference: 28.94 |---------+---------+---------
Accident: #2693461, distance to reference: 14.70 |---------+-----
Accident: #2693462, distance to reference: 01.85 |--
You need to work out how the visualisation works based on this example. Note that you should write functions to process this stage where appropriate.
3.3 Stage 3 - Overall Reporting
To complete this stage you will need to use strings to store the accident days. If you have completed Stages 1 and 2 without strings, you should save a copy of that version of your program into a separate file, as an insurance policy that can be submitted again later if you are unable to get your Stage 3 solution operational.
The additional output from this stage is the total number of accident records, the day of week with the most accidents, and the corresponding number of accidents on that day of week. In the case of ties, the day earlier in the week should be output (“Monday” is considered the first day in a week and “Sunday” is considered the last day in a week).
Stage 3 ==========
Number of accidents: 10
Day of week with the most accidents: Wednesday (6 accident(s))
Hint: You may need to use an array of strings and an array of integers to keep track of the accident count for each day. Wherever appropriate, code should be shared between the stages through the use of functions. In particular, there should not be long stretches of repeated code appearing in different places in your program. Further examples showing the full output that is required are provided on the LMS.
You will be given a sample test file test0.txt and the sample output test0-output.txt. You can test your code on your own machine with the following command and compare the output with test0-output.txt:
mac: ./assmt1 < test0.txt /* Here ‘<’ feeds the data from test0.txt into assmt1 */
Note that we are using the following command to compile your code on the submission server. gcc -Wall -lm -std=c99 -o assmt1 assmt1.c
The flag “-std=c99” enables the compiler to use a more modern standard of the C language – C99. To ensure that your submission works properly on the submission server, you should use this command to compile your code on your local machine as well.
Explanation / Answer
Accident: #2693452, distance to reference: 08.57 |---------
Accident: #2693453, distance to reference: 08.44 |---------
Accident: #2693454, distance to reference: 04.46 |-----
Accident: #2693455, distance to reference: 15.45 |---------+------
Accident: #2693456, distance to reference: 30.37 |---------+---------+---------+-
Accident: #2693458, distance to reference: 04.94 |-----
Accident: #2693459, distance to reference: 22.05 |---------+---------+---
Accident: #2693460, distance to reference: 28.94 |---------+---------+---------
Accident: #2693461, distance to reference: 14.70 |---------+-----
Accident: #2693462, distance to reference: 01.85 |--
You need to work out how the visualisation works based on this example. Note that you should write functions to process this stage where appropriate.
3.3 Stage 3 - Overall Reporting
To complete this stage you will need to use strings to store the accident days. If you have completed Stages 1 and 2 without strings, you should save a copy of that version of your program into a separate file, as an insurance policy that can be submitted again later if you are unable to get your Stage 3 solution operational.
The additional output from this stage is the total number of accident records, the day of week with the most accidents, and the corresponding number of accidents on that day of week. In the case of ties, the day earlier in the week should be output (“Monday” is considered the first day in a week and “Sunday” is considered the last day in a week).
Stage 3 ==========
Number of accidents: 10
Day of week with the most accidents: Wednesday (6 accident(s))
Hint: You may need to use an array of strings and an array of integers to keep track of the accident count for each day. Wherever appropriate, code should be shared between the stages through the use of functions. In particular, there should not be long stretches of repeated code appearing in different places in your program. Further examples showing the full output that is required are provided on the LMS.
You will be given a sample test file test0.txt and the sample output test0-output.txt. You can test your code on your own machine with the following command and compare the output with test0-output.txt:
mac: ./assmt1 < test0.txt /* Here ‘<’ feeds the data from test0.txt into assmt1 */
Note that we are using the following command to compile your code on the submission server. gcc -Wall -lm -std=c99 -o assmt1 assmt1.c
The flag “-std=c99” enables the compiler to use a more modern standard of the C language – C99. To ensure that your submission works properly on the submission server, you should use this command to compile your code on your local machine as well.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.