Write a program to write a temperature conversion chart to a file. The main script will:
belongs to book: MATLAB: A Practical Introduction to Programming and Problem Solving|Stormy Attaway|Fourth Edition| Chapter number:6| Question number:20.6
All Answers
total answers (1)
Ch6Ex20.m
% Writes a temperature conversion chart to a file
% Explain the program
explainTemp
% Prompt the user for min and max F temps
[tmin, tmax] = tempMinMax;
% Write the F temps and corresponding C temps to file
fandCTemps(tmin, tmax)
explainTemp.m
function explainTemp
% Explains that the program will write a temperature
% conversion chart from F to C to a file
fprintf('This program writes a temperature conversion')
fprintf(' chart to a file fcchart.dat.\n')
fprintf('\nIt prompts the user for a minimum and maximum')
fprintf(' temp in degrees F\n')
fprintf('It writes the F temps from min to max in one')
fprintf(' column\n and correspoding C temps in another\n')
end
tempMinMax.m
function [tmin, tmax] = tempMinMax
% Prompts the user for min and max F temps
% and makes sure min < max
% Format of call: tempMinMax or tempMinMax()
% Returns min and max temperatures in F
tmin = input('Enter the minimum F temp: ');
tmax = input('Enter the maximum F temp: ');
if tmin > tmax
[tmin, tmax] = swap(tmin, tmax);
end
end
function [outa, outb] = swap(ina, inb)
% swaps the values of arguments
% Format of call: swap(a,b)
% Returns b then a
outa = inb;
outb = ina;
end
fandCTemps.m
function fandCTemps(tmin, tmax)
% Writes the F and C temps to a file
% Format of call: fandCTemps(min temp, max temp)
% Does not return any values
f = tmin:tmax;
c = (f-32)*5/9;
mat = [f;c]';
save fcchart.dat mat -ascii
end
need an explanation for this answer? contact us directly to get an explanation for this answer