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 a program to answer the following: Calculate the
total precipitation for the year and monthly average precipitation in
each city. 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:
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];
fprintf('Total precipitation in Boston=%f \n',sum(bos));
fprintf('Total precipitation in Seattle=%f \n',sum(sea));
fprintf('Average monthly precipitation in Boston=%f \n',sum(bos)/12);
fprintf('Average monthly precipitation in Seattle=%f \n',sum(sea)/12);
numMonthBos=0;
for v=bos
if v > sum(bos)/12
numMonthBos+=1;
end
end
numMonthSea=0;
for v=bos
if v > sum(sea)/12
numMonthSea+=1;
end
end
fprintf('Number of months when precipitation is above average in Boston=%d \n',numMonthBos);
fprintf('Number of months when precipitation is above average in Seattle=%d \n',numMonthSea);
total1=0;
for i=1:12
if bos(i) < sea(i)
total1+=1;
end
end
fprintf('Number of months when precipitation is lower in boston than in Seattle=%d \n',total1);
Solution:
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];
fprintf('Total precipitation in Boston=%f \n',sum(bos));
fprintf('Total precipitation in Seattle=%f \n',sum(sea));
fprintf('Average monthly precipitation in Boston=%f \n',sum(bos)/12);
fprintf('Average monthly precipitation in Seattle=%f \n',sum(sea)/12);
numMonthBos=0;
for v=bos
if v > sum(bos)/12
numMonthBos+=1;
end
end
numMonthSea=0;
for v=bos
if v > sum(sea)/12
numMonthSea+=1;
end
end
fprintf('Number of months when precipitation is above average in Boston=%d \n',numMonthBos);
fprintf('Number of months when precipitation is above average in Seattle=%d \n',numMonthSea);
total1=0;
for i=1:12
if bos(i) < sea(i)
total1+=1;
end
end
fprintf('Number of months when precipitation is lower in boston than in Seattle=%d \n',total1);
No comments:
Post a Comment