Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP20005 Engineering Computation

Semester 2, 2023

Assignment 2

Due: 4:00 pm Friday 20th October 2023

Version 1.0

1    Learning Outcomes

In this assignment, you will demonstrate your understanding of arrays and structures, and use them together with functions. You will further practise your skills in program design and debugging.

2    The Story...

Ongoing noise can cause stress and health problems. The National Code of Practice for Noise Management 1 requires that noise at the work environment is within 85 dB (deined below) on average, while the peak noise should not exceed 140 dB. Your task in this assignment is to design and implement a program that models the noise patterns across a given region (e.g., around the Metro Tunnel station on campus), and calculate any zones that perceive an excessive level of noise.

You do not  need to be a sound engineer to carry out this assignment.  However, you do need the following background information. The noise level is usually measured in a unit called decibel  (dB)2. When measuring the noise level of a sound source, the distance needs to be taken into consideration. A distance of one metre (1 m) from the source is a frequently used default distance. Typically, measured at one-metre distance, a pin dropping generates a 10 dB noise; a jack hammer generates a 100 dB noise (L = 100 and r = 1); ajet engine generates a 150 dB noise.  According to the inverse proportional law, when a noise level L is measured at distance r, then the noise level Lat distance rbecomes3 :

L= L + 20 log10  ( , dB           (r 0)                                                    (1)

For example, if measured at 100 metres (r = 100), then the noise level of a jack hammer becomes 60 dB (L = 60).   Note  that  a  noise  level  value  must  not  be  a  negative  number.    If Equation  1  yields  a  negative number,  then Lshould simply  be  made  0.

University M would like a tool that allows it to model the noise level on their campus.  The tasks described below lead you to the desired program.  Note that the input format for diferent stages difers slightly, but that all lines in the input ile will start with an uppercase letter.  You must read the uppercase letter of each line, and process the line accordingly.  Some stages only need some of the lines.   You  may  assume  that between  1  and  99  noise  sources  will  be  specifed;  that  between  1  and  99  observation  points  will  be  specifed; and that between 1  and 99 sensor locations will be specifed.  Like  in Assignment  1,  no input validity checking is required.

3    Your Task

3.1    Stage 1 - Arrays and Structs (6 Marks)

Write a program that reads a sequence of ID (a 3-digit unique integer), x (a real number), y (a real number), and noise level (a real number) values from the standard input into an array of structures.  Each relevant input line will start with an S (for noise source) in the irst character position. For example,

S  101  41.0  651.0  60.0

S  352  91.0  511.0  80.0

S  555  341.0  851.0  50.0

S  737  791.0  791.0  110.0

represent the locations of four noise sources with their IDs, x and y coordinates in the positive quadrant of the plane measured in metres from the origin (0, 0), and noise level values (measured at one-metre distance) in decibel. All input lines starting with an S will appear together at the beginning of the data ile, which are sorted in ascending order of the IDs.

Once the noise location/level data have been read, your program should calculate the  aggregate  noise  level at the origin (location (0, 0)), by adding up the contributions of all the speciied noise sources using the following equation:

Lsum  = 10 log10  (10 1(L)0(1) + 10 1(L)0(2) + ... + 10 10(L n) ) dB                                            (2)

Here, Lsum  represents the aggregate noise level; L1 , L2 , ..., Ln  represent the noise level contributed by the n noise sources (you will need to calculate these values based on Equation 1). Note that:

. Lsum  ≥ 0.

. If Li  = 0 then it should not be added into Equation 2.

For the four noise sources (that is, n = 4) in the sample input, your output for this stage should be (use %05.2f for noise level output formatting):

Stage  1

==========

Number  of  noise  sources:  04

Noise  level  at  (000.0,  000.0):  49.05  dB

Note that Equation 1 requires r   0.  To guarantee the validity of Equation 1, the locations of the noise sources will not be at (0,0) or any of the points used in the following stages.

You will need distance calculation for this stage.   Given  two  points  with  coordinates p1   =  (x1 , y1 )  and p2  = (x2 , y2 ), their distance is calculated as:

dist(p1 , p2 ) =4(x1 - x2 )2 + (y1 - y2 )2                                                                             (3)

3.2    Stage 2 - More Loops (5 Marks)

The second block of input data are lines starting with an O, where each line contains the x and y coordinates of an observation point. Your program should read each of these lines, and compute the aggregate noise level at each observation point, summing over the noise sources that were read in Stage 1 based on Equations 1 and 2. For example, if the next two input lines are:

O  640.0  260.0

O  380.0  150.0

Your Stage 2 output, following on from your Stage 1 output, would be:

Stage  2

==========

Noise  level  at  (640.0,  260.0):  55.16  dB

Noise  level  at  (380.0,  150.0):  52.38  dB

Note that all input lines starting with  an  O will appear together after the  lines starting with  an  S.

Hint:   Stage  1  calculates  the noise level at point  (0,  0).   Stage  2  calculates the noise levels  at the points speciied by the O lines.

3.3    Stage 3 - Draw a Noise Map (5 Marks)

In this stage, you need to draw a “noise map” with a grid of characters. Each character displayed represents a cell of 20 metres wide (in the east-west direction) and 40 metres tall (in the north-south direction), with the 1:2 ratio (roughly) corresponding to the relative width and height of characters in a terminal font.  To represent a square region of 800    800 metres, your map will be 40 characters wide and 20 characters high.  To show the noise level contours, we use the following relationship between noise level in dB and the character displayed (where ‘ ’ represents a whitespace character):

<20      <30       <40      <50       <60      <70       <85      <140       >=140

‘- ’      ‘  ’      ‘4’      ‘  ’      ‘6’      ‘  ’      ‘8’      ‘+’        ‘*’

The character plotted in each cell should correspond to the aggregate noise level at the  centre  of that cell. For example, the irst value to be output (in the top left corner) should correspond to the noise level at the point (x; y) = (10:0; 780:0), since each cell is taken to be 20 metres wide and 40 metres high.  The bottom left corner of your map corresponds to a cell whose midpoint is at (x; y) = (10:0; 20:0).  Similarly, the top right and the bottom right points to be plotted are (790:0; 780:0) and (790:0; 20:0), respectively.  A sample of the expected output for this stage is shown below.

Stage  3

==========

666666666666666666666666                      8888+

666666666666666666666666                      88888

666666666666666666666666                          888

6666666666666666666666666

66666666666666666666666666

6666666666666666666666666666

666666666666666666666666666666

666666666666666666666666666666666

6666666666666666666666666666666666666666

6666666666666666666666666666666666666666

6666666666666666666666666666666666666666

6666666666666666666666666666666666666666

6666666666666666666666666666666666666666

6666666666666666666666666666666666666666

6666666666666666666666666666666666666666

6666666666666666666666666666666666666666

666666666666666666666666666666666666666

6666666666666666666666666666666666666

66666666666666666666666666666666666

66666666666666666666666666666666

3.4    Stage 4 - Deploy a Noise Monitoring Sensor (4 Marks)

Now continue to read the last block of input data where each lines starts with an ‘L ’ followed by a pair of x and y coordinates, indicating the location of a noise sensor deployed, for example:

L  0.0  0.0

L  100.0  750.0

L  600.0  750.0

L  750.0  150.0

To maintain accurate monitoring of the noise level and be able to raise alarms when the noise level has become excessive, it is important that the noise sensors are not too far away from the noise sources.

In this stage, your tasks are:

1.  Add code to calculate the distance between every noise source and its nearest noise sensor.

For example, given the example input above, for noise source #101, its distance to the irst sensor location (0:0; 0:0) is  (41:0 - 0:0)2 + (651:0 - 0:0)2  = 652:29.  Similarly, its distance to the other three sensor locations are 115:25, 567:70, and 868:15, respectively. Thus, for this noise source, the distance to its nearest noise sensor is 115:25.

2.  Add code to calculate the average  distance  between all noise sources and their respective nearest noise sensors. Let this average distance be avg.

For  example,  avg  =  202.77  given  the  example  input  above,  because  the  distance  to  nearest  noise sensor  for  the  4  noise  sources  are  115.25,  239.17,  261.31,  and  195.25,  respectively,  and  202.77   = 115.25 + 239.17 + 261.31 + 195.25

.

4

3.  Now suppose that the university’s Health and Safety Team wants to deploy a new sensor.  Add code to iterate through all integer coordinates that are multiples of 10 within  [0 , 800] 根 [0, 800]  (that  is, (10, 10), (10, 20), . . . (10, 800), (20, 10), (20, 20), . . . , (20, 800), . . . , (800, 800) – atotal of 80根80 coordinate pairs), and ind the pair of coordinates to deploy the new sensor, such that the average distance between all noise sources and their respective nearest noise sensors is reduced by the most after this sensor deployment.

If there is a tie, your program should output the pair with the smallest x coordinate – if there is a further tie where multiple pairs share the same smallest x coordinate, among these pairs, the pair that has the smallest y coordinate should be outputted.

Continue with the example above,  if the new sensor is deployed at location  (100 , 750), the average distance between all noise sources and their respective nearest noise sensors will not change, because there was a sensor at location (100, 750) already.

If a new sensor is deployed at location (90, 510), the average distance between all noise sources and their respective nearest noise sensors will be reduced by the most, that is, from 202 .77 to 143.33 (202.77 - 143.33 = 59.44).   In particular, the distance to nearest noise sensor for noise source  #352 has been reduced from 239.17 to   (91 - 90)2 + (511 - 510)2         1.41 which contributed the reduction,  that is,   = 59.44.  Here, the three 0’s means that the distance to nearest sensor for the other three noise sources will not be reduced when the new sensor is deployed at location (90 , 510).

Given the example input above, the sample output of this stage is (use %05.2f for output formatting; don’t forget the inal newline at the end):

Stage  4

==========

#101,  distance  to  nearest  sensor:  115.25

#352,  distance  to  nearest  sensor:  239.17

#555,  distance  to  nearest  sensor:  261.31

#737,  distance  to  nearest  sensor:  195.35

Average  distance  to  nearest  sensor:  202.77

Best  location  for  the  new  sensor:  (090.0,  510.0)

New  average  distance  to  nearest  sensor:  143.33

For a challenge (and not for submission), can you think of a smarter strategy to ind the best location (not limited to the points with coordinates that are multiples of 10) for the new sensor?

4    Submission and Assessment

This assignment is worth 20% of the inal mark. A detailed marking scheme will be provided on LMS.

Submitting your code.  To submit your code, you will need to:  (1) Log in to LMS subject site,  (2) Nav- igate to  Assignment 2” in the  Assignments” page,  (3) Click on  Load Assignment 2 in a new window”, and (4) follow the instructions on the Gradescope  Assignment 2” page and click on the  Submit” link to make a submission. You can submit as many times as you want to.  Only  the  last submission made  before the deadline will be marked.  Submissions made after the deadline will be marked with late penalties as detailed at the end of this document.  Do not  submit after the deadline unless a late submission is intended.  Two hidden tests will be run for marking purposes.  Results of these tests will be released after the marking is done.

You can (and should) submit both early and often – to check that your program compiles correctly on our test system, which may have some diferent characteristics to your own machines.

Testing on your own computer.  You will be given a sample test ile 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:  ./program  <  test0.txt       /*  Here  ‘<’  feeds  the  data  from  test0.txt  into  program  */

Note that we are using the following command to compile your code on the submission testing system (we name the source code ile program.c).

gcc  -Wall  -std=c17  -o  program  program.c  -lm

The lag “-std=c17” enables the compiler to use a modern standard of the C language  C17.  To ensure that your submission works properly on the submission system, you should use this command to compile your code on your local machine as well. If you are using Grok to prepare your assignment, you may use the following command to compile it in Grok Terminal, which only supports C11 but should be close enough.

gcc  -Wall  -std=c11  -o  program  program.c  -lm

You may discuss your work with others, but what gets typed into your program must be individual work, not from anyone else.

. Do not give (hard or soft) copies of your work to anyone else.

. Do not “lend” your memory stick to others.

. Do not leave your laptop unlocked in the lab or library.

. Do not upload your assignment solutions onto Github or any other public code repositories.

. Do not ask others to give you their programs “just so that I can take a look and get some ideas, I won’t copy, honest” .

The best way to help your friends in this regard is to say a very irm  “no” when they ask for a copy of, or to see, your program, pointing out that your  “no”,  and their  acceptance of that decision, is the only thing that will preserve your friendship.  A  sophisticated program  that undertakes  deep  structural  analysis  of C code  identifying  regions  of similarity  will  be  run  over  all  submissions  in   “compare  every  pair”  mode.   See https://academichonesty.unimelb.edu.au for more information.

Deadline:  Programs not submitted by 4:00 pm Friday 20th October 2023 will lose penalty marks at the rate of 3 marks per day or part day late.  Late submissions after 4:00 pm Sunday 22nd October 2023 will not be accepted.  Students seeking extensions for medical or other  outside my control” reasons should email the lecturer at [email protected]. If you attend a GP or other health care professional as a result of illness, be sure to take a Health Professional Report (HRP) form with you (get it from the Special Consideration section of the Student Portal), you will need this form to be illed out if your illness develops into something that later requires a Special Consideration application to be lodged. You should scan the HPR form and send it in connection with any non-Special Consideration assignment extension requests.

And remember, C programming is fun!