Q:

Write a program to write a temperature conversion chart to a file. The main script will:

0
  •  call a function that explains what the program will do
  •  call a function to prompt the user for the minimum and maximum

temperatures in degrees Fahrenheit, and return both values. This

function checks to make sure that the minimum is less than the 

maximum, and calls a subfunction to swap the values if not. 

  • call a function to write temperatures to a file: the temperature in 

degrees F from the minimum to the maximum in one column, and

the corresponding temperature in degrees Celsius in another 

column. The conversion is C = (F – 32) * 5/9.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now