%Lab 24 solutions %Patrick D. Schmid clear; %get the numbers from the user numbers = input('Enter two numbers in matrix form (e.g. [7 5.9]): '); third = input('Enter a single number to compare (e.g. 5.87): '); %sort the numbers. Note that we are reusing the same variable as we might %with some assignments in C++ numbers = sort(numbers); %open the output file out = fopen('output.txt', 'wt'); %let's do the comparisons %the logic is similar to C++, but the syntax is different! if (third < numbers(1)) %print to the screen fprintf('Third number (%6.2f) is less than both (%6.2f and %6.2f).\n', third, numbers(1), numbers(2)); % print to the file... fprintf(out, 'Third number (%6.2f) is less than both (%6.2f and %6.2f).\n', third, numbers(1), numbers(2)); %notice that we are not using curly braces elseif (third == numbers(1)) fprintf('Third number (%6.2f) is equal to lower number (%6.2f).\n', third, numbers(1)); fprintf(out, 'Third number (%6.2f) is equal to lower number (%6.2f).\n', third, numbers(1)); elseif (third == numbers(2)) fprintf('Third number (%6.2f) is equal to greater number (%6.2f).\n', third, numbers(2)); fprintf(out, 'Third number (%6.2f) is equal to greater number (%6.2f).\n', third, numbers(2)); elseif (third > numbers(2)) fprintf('Third number (%6.2f) is greater than both (%6.2f and %6.2f).\n', third, numbers(1), numbers(2)); fprintf(out, 'Third number (%6.2f) is greater than both (%6.2f and %6.2f).\n', third, numbers(1), numbers(2)); else fprintf('Third number (%6.2f) is in between the two numbers (%6.2f and %6.2f).\n', third, numbers(1), numbers(2)); fprintf(out, 'Third number (%6.2f) is in between the two numbers (%6.2f and %6.2f).\n', third, numbers(1), numbers(2)); %end of if-else if-else block end %Close your file %You MUST do this in MatLab! fclose(out);