Trouble with loop practice

1 次查看(过去 30 天)
Alexander Nicholas
Alexander Nicholas 2017-4-17
评论: Nick 2017-4-17
The prompt and my initial work is in the code below. This is my first time using for loops and am having difficulties. I understand that I have to somehow have the code check each element to see if it is >50, and if it is, add one to some variable in order to total the number of times this occurs. Then I have to have the code add up all values and divide by the number of values (this I have no idea how to do).
We have to use a for loop and so I tried to get the code to check if the tempv is in the range of 50 to 75 (as 75 is the largest value), and if it falls in this range, it adds one to a variable that starts at zero. But when I run the code it just spits out the variable from 0 to 26 (because of 50 to 75). What am I doing wrong???
% % The vector tempv below contains the daily average temperatures of a May.
% Write a program to compute:
% 1) the number of days with average temperatures above 50 Fahrenheit
% 2) the average temperature in the May.
%
% You want to output the calculation result. The output example:
% "There were 19 days with an average T above 50 Fahrenheit in the May."
% "The average temperature was 53.870968 Fahrenheit."
%
% If a temperature is above 50 Fahrenheit, you also want to display
% the following message in the Command Window:
% "It is a warm day!",
%
% You are required to use two different approaches as specified below.
clear, clc tempv = [50, 60, 65, 30, 40, 66, 30, 55, 72, 72, 75, 50, 60, 65, 30, 40, ... 66, 30, 55, 63, 72, 75, 50, 60, 65, 30, 40, 66, 30, 55, 53];%
% Q3.1 Approach 1: use a for loop to implement the requirement % Write your code here:
n=0; for tempv=50:75 n=n+1; fprintf('In May the number of days the T was over 50 was: %i\n', n) end
And here is the command window output:
In May the number of days the T was over 50 was: 1
In May the number of days the T was over 50 was: 2
In May the number of days the T was over 50 was: 3
In May the number of days the T was over 50 was: 4
In May the number of days the T was over 50 was: 5
In May the number of days the T was over 50 was: 6
In May the number of days the T was over 50 was: 7
In May the number of days the T was over 50 was: 8
In May the number of days the T was over 50 was: 9
In May the number of days the T was over 50 was: 10
In May the number of days the T was over 50 was: 11
In May the number of days the T was over 50 was: 12
In May the number of days the T was over 50 was: 13
In May the number of days the T was over 50 was: 14
In May the number of days the T was over 50 was: 15
In May the number of days the T was over 50 was: 16
In May the number of days the T was over 50 was: 17
In May the number of days the T was over 50 was: 18
In May the number of days the T was over 50 was: 19
In May the number of days the T was over 50 was: 20
In May the number of days the T was over 50 was: 21
In May the number of days the T was over 50 was: 22
In May the number of days the T was over 50 was: 23
In May the number of days the T was over 50 was: 24
In May the number of days the T was over 50 was: 25
In May the number of days the T was over 50 was: 26
  1 个评论
Nick
Nick 2017-4-17
check your loop to see what you are looping over. Right now you have loop from the numbers 50 -> 75. Why would you do that? You want to loop over the variable tempv checking during every iteration if the temperature is higher than specified temperature.
for i = 1 : length(tempv)
%check if temperature is between value
if tempv(i) >= 50 && tempv(i) <= 75
%do something
end
end
with a loop the i in tempv will be given the value of i every time in the loop so:
tempv(1) = 50 tempv(2) = 60
see if you can get it from here

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by