(15) (15 marks) Write a MATLAB script and function. wnlrt loop The script: The u
ID: 3602773 • Letter: #
Question
(15) (15 marks) Write a MATLAB script and function. wnlrt loop The script: The user can enter an arbitrary number of temperatures in Fahrenheit. After the user has entered the final temperature the script will find the average temperature. The script then calls the function described below to convert Fahrenheit to Celsius. Finally, the script will display the average temperature in Fahrenheit and the average temperature in Celsius The function: will convert Fahrenheit to Celsius using the conversion formula 5 Cz (F-32)Explanation / Answer
%intital farenheit sum
fsum = 0;
%initial temparatures count
t = 0;
%infinite loop
while(true)
% read user input
x = input("Enter fahrenheit temperature: 0 to stop ::")
% if input is zero break loop
if(x==0)
break
end
% add temperature to sum
fsum += x;
% increase temperatures count
t += 1;
end
% calculate average temperature
avgfaren = fsum/t;
% function to convert farenheit to celcius
function celc = FtoC(faren)
% fareheit to celcuis formula
celc = 5/9 * (faren-32);
end
% call the function
avgcelc = FtoC(avgfaren);
%print output
fprintf("Average Farenheit:%.2f Average Celcius:%.2f ",avgfaren,avgcelc);
% sample output
%Enter fahrenheit temperature: 0 to stop ::x = 31
%Enter fahrenheit temperature: 0 to stop ::x = 32
%Enter fahrenheit temperature: 0 to stop ::x = 33
%Enter fahrenheit temperature: 0 to stop ::x = 34
%Enter fahrenheit temperature: 0 to stop ::x = 35
%Enter fahrenheit temperature: 0 to stop ::x = 36
%Enter fahrenheit temperature: 0 to stop ::x = 37
%Enter fahrenheit temperature: 0 to stop ::x = 38
%Enter fahrenheit temperature: 0 to stop ::x = 39
%Enter fahrenheit temperature: 0 to stop ::x = 40
%Enter fahrenheit temperature: 0 to stop ::x = 0
%Average Farenheit:35.50
%Average Celcius:1.94
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.