Q: The average monthly precipitation
(in.) for Boston and Seattle during 2012 are given in the vectors below
(data from the U.S. National Oceanic and Atmospheric Administration) BOS
= [2.67 1.00 1.21 3.09 3.43 4.71 3.88 3.08 4.10 2.62 1.01 5.93] SEA =
[6.83 3.63 7.20 2.68 2.05 2.96 1.04 0.00 0.03 6.71 8.28 6.85] Where the
elements in the vectors are in the order of the months (January,
February, etc.) Write your program to answer the following: (a)
Calculate the total precipitation for the year and monthly average
precipitation in each city. Do NOT use MATLAB's built-in functions sum
and mean. (b) How many months was the precipitation above the average
in each city? (c) How many months, and on which months, was the
precipitation in Boston lower than the precipitation in Seattle?
Solution:
clear all;
close all;
bos=[2.67,1,1.21,3.09,3.43,4.71,3.88,3.08,4.10,2.62,1.01,5.93];
sea=[6.83,3.63,7.20,2.68,2.05,2.96,1.04,0,0.03,6.71,8.28,6.85];
tot_bos=0;
tot_sea=0;
for x=1:1:12
tot_bos=tot_bos+bos(x);
tot_sea=tot_sea+sea(x);
end
avg_bos=tot_bos/12;
avg_sea=tot_sea/12;
above_avg_bos=0;
above_avg_sea=0;
for y=1:1:12
if(bos(y)>avg_bos)
above_avg_bos=1+above_avg_bos
end;
if(sea(y)>avg_sea)
above_avg_sea=1+above_avg_sea
end;
end
i=1;
for z=1:1:12
if(bos(z)<sea(z));
a(i)=z;
i=i+1;
end
end
fprintf('Total participation for the year in BOS ans SEA are %0.2d %0.2d\n',tot_bos,tot_sea);
fprintf('Monthly avg participation for the year in BOS ans SEA are %0.2d %d\n',avg_bos,avg_sea);
fprintf('Totoal month of above avg participation in the year for BOS and SEA %i %i\n',above_avg_bos,above_avg_sea);
fprintf('Total month where BOS is less than SEA %i and months are %i',i-1,a);
Solution:
clear all;
close all;
bos=[2.67,1,1.21,3.09,3.43,4.71,3.88,3.08,4.10,2.62,1.01,5.93];
sea=[6.83,3.63,7.20,2.68,2.05,2.96,1.04,0,0.03,6.71,8.28,6.85];
tot_bos=0;
tot_sea=0;
for x=1:1:12
tot_bos=tot_bos+bos(x);
tot_sea=tot_sea+sea(x);
end
avg_bos=tot_bos/12;
avg_sea=tot_sea/12;
above_avg_bos=0;
above_avg_sea=0;
for y=1:1:12
if(bos(y)>avg_bos)
above_avg_bos=1+above_avg_bos
end;
if(sea(y)>avg_sea)
above_avg_sea=1+above_avg_sea
end;
end
i=1;
for z=1:1:12
if(bos(z)<sea(z));
a(i)=z;
i=i+1;
end
end
fprintf('Total participation for the year in BOS ans SEA are %0.2d %0.2d\n',tot_bos,tot_sea);
fprintf('Monthly avg participation for the year in BOS ans SEA are %0.2d %d\n',avg_bos,avg_sea);
fprintf('Totoal month of above avg participation in the year for BOS and SEA %i %i\n',above_avg_bos,above_avg_sea);
fprintf('Total month where BOS is less than SEA %i and months are %i',i-1,a);
No comments:
Post a Comment