8) (13 Points) Note, this problem is more difficult and worth less points than t
ID: 3735410 • Letter: 8
Question
8) (13 Points) Note, this problem is more difficult and worth less points than the previous problems. Make sure you have answered all previous problems satisfactorily before working on this problem. Write code that tracks a city's snow removal budget during a simulated winter season of 120 days. The code must implement the following requirements. (See the example on the next page.) a) The amount of snow is stored for each of N roads. You may assume that N exists and contains a valid value. For example, if N equals 3, the data you must maintain for the amount of snow on the roads would look something like: [ 0, 10, 4]. This would mean that currently there is no (0) snow on road 1; 10 inches of snow on road 2; and 4 inches of snow on road 3. b) For each day you simulate, use the function how_much_new_snowO, which will return the number of inches of snow that fell that day. It returns 0 if no snow fell. c) The amount of snow that falls should be added to the amount of snow already on each road. (For example, if 6 new inches of snow fell, then the road data listed in a) above would now be: [6, 16, 10 ]) d) On days that it snows (and only on days that it snows), plows will be sent out. If the current day is an odd day, then only the odd roads (roads with odd indexes) will be plowed, otherwise the even roads will be plowed. All snow on a plowed road is removed (unless you run out of budget- see the budget section below). e) If it does not snow, then one inch of snow will melt off every road. Remember, there can be a minimum of 0 inches on any road. Budget f You have an initial budget of $100. g) Every day you are given $10 more. h) On days that it snows, you are given emergency funds equal to 20% of the current amount of snow on all the streets. (ie: If the roads had [ 30, 0, 50, 20] inches of snow on them, then you would receive an extra $20 for your budget.) i) It costs $1 to remove one inch of snow from a single road. i) If you run out of budget, no more snow will be cleared until the next time it snows. (Keep track of the number of days that you run out of budget.) k) You may use the min0, max0, sum.. and odd0 functions if you choose.Explanation / Answer
Here is the matlab code. Random number generator is used to generate each day snowfall 0 to 9 inch and 30 day trial is calculated as per your requirement. Save the below code as snowfall.m and run from matlab terminal
/*****************************/
function SN=snowfall() %function snowfall
N=5; %Set number of roads
budget=100; %Set initial budget
S=zeros(1,N); %Initialise first day road snowfall to zeros
for i=2:30 %loop for 30 days
budget=budget+10;
Sf=how_much_new_snow(N);%calculate each day snowfall
S(i,:)=S(i-1,:)+Sf; %sum up the snowfall
budget=budget+0.2*sum(S(i,:));%add emergency fund
fprintf('Day%d budget: %f ',i,budget);
if(budget>0) %check for sufficient budget
if(mod(i,2)==0) %for even days
for j=1:2:N
budget=budget-S(i,j);
S(i,j)=0;%clear snow of even roads
end
else %for odd days
for j=2:2:N
budget=budget-S(i,j);
S(i,j)=0;%clear snow of odd roads
end
end
disp(S); %display each day status
end
end
end
%function to compute each day snowfall for n roads
function ns=how_much_new_snow(n)
ns=randi(10,1,n)-1; %generate random number 0 to 10 for n roads
end
/*****************/sample outut
Day2 budget: 112.400000
0 0 0 0 0
0 1 0 3 0
Day3 budget: 117.600000
0 0 0 0 0
0 1 0 3 0
5 0 6 0 0
Day4 budget: 129.200000
0 0 0 0 0
0 1 0 3 0
5 0 6 0 0
0 0 0 8 0
Day5 budget: 119.800000
0 0 0 0 0
0 1 0 3 0
5 0 6 0 0
0 0 0 8 0
1 0 4 0 4
Day6 budget: 119.000000
0 0 0 0 0
0 1 0 3 0
5 0 6 0 0
0 0 0 8 0
1 0 4 0 4
0 9 0 3 0
Day7 budget: 107.400000
0 0 0 0 0
0 1 0 3 0
5 0 6 0 0
0 0 0 8 0
1 0 4 0 4
0 9 0 3 0
8 0 1 0 9
Day8 budget: 106.200000
0 0 0 0 0
0 1 0 3 0
5 0 6 0 0
0 0 0 8 0
1 0 4 0 4
0 9 0 3 0
8 0 1 0 9
0 2 0 9 0
Day9 budget: 95.400000
0 0 0 0 0
0 1 0 3 0
5 0 6 0 0
0 0 0 8 0
1 0 4 0 4
0 9 0 3 0
8 0 1 0 9
0 2 0 9 0
3 0 7 0 6
Day10 budget: 93.200000
0 0 0 0 0
0 1 0 3 0
5 0 6 0 0
0 0 0 8 0
1 0 4 0 4
0 9 0 3 0
8 0 1 0 9
0 2 0 9 0
3 0 7 0 6
0 9 0 1 0
Day11 budget: 82.600000
0 0 0 0 0
0 1 0 3 0
5 0 6 0 0
0 0 0 8 0
1 0 4 0 4
0 9 0 3 0
8 0 1 0 9
0 2 0 9 0
3 0 7 0 6
0 9 0 1 0
6 0 5 0 6
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4
8 0 9 5 12
14 0 18 0 13
0 0 0 2 0
1 7 7 7 2
1 13 7 7 3
5 0 15 0 3
Day30 budget: 1.200000
0 0 0 0 0
0 1 0 3 0
5 0 6 0 0
0 0 0 8 0
1 0 4 0 4
0 9 0 3 0
8 0 1 0 9
0 2 0 9 0
3 0 7 0 6
0 9 0 1 0
6 0 5 0 6
0 1 0 3 0
8 0 8 0 5
0 8 0 8 0
2 0 2 0 4
0 2 0 3 0
5 0 2 0 2
0 3 0 0 0
3 0 4 0 9
0 9 0 8 0
5 0 1 0 8
0 8 0 3 0
1 0 3 0 4
8 0 9 5 12
14 0 18 0 13
0 0 0 2 0
1 7 7 7 2
1 13 7 7 3
5 0 15 0 3
0 6 0 2 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.