How to use while true loop for this qustion?

The Fahrenheit temperature is converted to Celsius temperature by the following formula , write a script file to accept temperature in Fahrenheit and calculate its Celsius equivalent for 5 times using DO WHILE loop.
While writing the Program please consider the Following:
  1. The program must ask the user to input the temperature in Fahrenheit.
  2. The program must display the temperature in Celsius.

7 个评论

Hello mahmoud,
Do you want to calculate the average of 5 times of "C" ?
stozaki
This is not what is needed. The complete assignment was posted before but got deleted.
The requirement posted before was to use a "do while" loop. Each time through the loop, the program needs to prompt for a value and do temperature conversion and display the value. The loop is to be executed five times.
The assignment does not involve mean(): it involves using a while loop with exit condition at the end to loop a fixed number of times.
This is of course very easy so I have been explaining the structure to use instead of explicitly showing the counter logic, hoping to get the user to think about how to turn the general situation into the particular one. The user has not posted any attempts yet and the volunteers are likely getting the impression that the user is hoping that someone will provide a complete solution.
frankly, i was wating for someone's full answer. but i will try.
Since you saw the complete question, Is this script crrect ?
time = 5;
x = 1;
mindeg = -459.67;
maxdeg = 212;
while x <= time
F = input('inter the degree in Fahrenheit: ');
if F < mindeg || F > maxdeg
disp("please inter a number between -459.67°F and 212°F.")
continue
end
x = x + 1;
C = ( F - 32 )*5/9;
fprintf("the degree in Celsius is: %f°F \n",C)
end
I do not at the moment recall any limits on the input temperatures. I would tend to expect that if temperatures as low as 0K are permitted then you would want to allow temperatures higher than 100C.
Sorry to ask again. is thes anser true?
x=1;
while true
F = input('inter the degree in Fahrenheit: ');
C = ( F - 32 )*5/9;
fprintf("the degree in Celsius is: %f°F \n",C)
if x == 5
break
end
x = x + 1;
end

请先登录,再进行评论。

 采纳的回答

assign initial conditions
while true
do a calculation
if condition is satisfied
break;
end
end
here, "condition is satisfied" needs to be expressed the opposite way than you would use for a "do while". For example in C
do {
x = rand();
} while x > 0.1;
would become
while true
x = rand() ;
if x <= 0.1; break; end
end
Notice the C condition of x > 0.1 becomes the condition x <= 0.1

更多回答(1 个)

Hello
Here is an example of calculation
% Define the value of F used in the calculation as a "vector".
F = [70 80 75 84 92];
% Calculate the average value of F using the mean function.
AveC = mean(5/9*(F - 32))
AveC =
26.7778
documentation for mean function
stozaki

类别

帮助中心File 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