Skip to content

Instantly share code, notes, and snippets.

@djmunro
Created January 22, 2013 18:33
Show Gist options
  • Save djmunro/4596995 to your computer and use it in GitHub Desktop.
Save djmunro/4596995 to your computer and use it in GitHub Desktop.
function main()
Problem1()
Problem2()
Problem3()
end
function Problem1()
number = 0;
while number ~= 3
printMenu()
% Select Temp conversion from Menu
number = input('Please select one of the above: ');
if(number == 1)
t = input('Please enter a Fahrenheit temperature: ');
FtoK(t);
sprintf('%.2f Fahrenheit degrees is %.2f Kelvin degrees.', F, K)
end
if(number == 2)
t = input('Please enter a Kelvin temperature: ');
KtoF(t);
sprintf('%.2f Kelvin degrees is %.2f Fahrenheit degrees.', K, F)
end
end
end
function printMenu()
disp('1. Fahrenheit to Kelvin')
disp('2. Kelvin to Fahrenheit')
disp('3. Quit')
end
function F = KtoF(K)
F = (9/5)*(K-273)+32;
end
function K = FtoK(F)
K = (5/9)*(F-32) + 273;
end
function Problem2()
%fileName = input('Please enter the name of your file: ');
fid = fopen('1.txt', 'w');
b = input('Please Enter the number of iterations: ')
fprintf(fid, 'Kelvin\tFahrenheit\n')
% Creates a txt file with Kelvin and Fahrenheit
for i = 1:b
K = 273 + (373 - 273)*rand(1);
F = KtoF(K);
fprintf(fid, '%.2f\t%.2f\n', K, F);
end
fclose(fid)
end
function Problem3()
maximum = 0;
minimum = 213;
totalF = 0;
numFiles = 0;
fid = fopen('1.txt', 'r');
tline = fgetl(fid);
while ischar(tline)
tokens = regexp(tline, '\t', 'split');
for i = 1:length(tokens)
ivalues = str2double(tokens{[2]});
numFiles = numFiles + 1;
% Finds Mean
totalF = totalF + ivalues;
% Finds Maximum
if (ivalues > maximum)
maximum = ivalues;
end
% Finds Minimum
if (ivalues < minimum)
minimum = ivalues;
end
end % End For loop
tline = fgetl(fid);
end % End While loop
mean = totalF/numFiles;
sprintf('Mean: %.2f', mean)
sprintf('Minimum: %.2f', minimum)
sprintf('Maximum: %.2f', maximum)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment