


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



% Jane Doe, ENGR 1731C, Lab 5 February 12, 2009 % This lab teaches the basics of switch‐case‐otherwise and how it is used
Documentation in Programs
% John Doe, % Engr 1731C % Lab 5, 02/12/ % This program will calculate the given function with the number x
% Jane Doe, ENGR 1731C, Lab 5 02/12/ % This lab will ask user to input a number to convert it to a different one % requested by the user
% Program name: lab5.m % Jane Doe, % February 5, 2009 % ENGR 1731C Lab 5 Switch‐Case‐otherwise structure %% ********************************************************************************** % This program performs various calculator functions % The functions it performs are: sine, cosine, tangent, square root, % inverse square, natural log, and common log (log 10) % **************** % Request for the number to be processed x=input('Enter the number x= ')
% Program name: lab5.m % Created on February 12, 2009 by Jane Doe % % ENGR 1731C Lab 5 Switch‐Case‐otherwise structure % ***************************************** % This program performs various calculator functions % The functions it performs are: sine, cosine, tangent, square root, % inverse square, natural log, and common log (log 10) %% **************** % Input Variable: % x ( number to be processed) % If the function to evaluate is sine, cosine or tangent , then the number % x should be given in radians % Output Variable: % y ( is the function of the number x) % The allowed functions for this program are % ‘s’ or ‘S’ for sine; ‘c’ or ‘C’ for cosine; ‘t’ or ‘T’ for tangent; % ‘r’ or ‘R’ for square root; ‘i’ or ‘I’ for 1/x; ‘q’ or ‘Q’ for squared; % ‘l’ or ‘L’ for natural log; ‘o’ or ‘O’ for log 10 (x) % Request for the number to be processed x=input(‘Enter the number x= ‘)
Strings
Strings (cont.)
% concatenates character arrays: word 1 word2 word % into a new character array: sentence
outside = input('It is sunny? (Y) or (N): ', 's')
Conversion Character
Description Character %c Single character %d Decimal notation (signed) %e (^) Exponential notation (using a lowercase e as in 3.1415e+00)
%E Exponential notation (using an uppercase E as in 3.1415E+00)
%f (^) Fixed‐point notation %s String of characters
fprintf('color is: %s, num1 is: %d, num2 is: %1.3f \n', 'red', 8, pi)
text='yellow', x = 55, y = 3.56;
fprintf('color is: %s, num1 is: %d, num2 is: %1.3f \n', text, x, y)
x = -5:0.5:5;
y =[x; x.^3];
% Display the resultant array. % a) Not very user friendly format
disp('The cubic function is: ' )
y
% b) A more user friendly format
fprintf('The cubic function is: %1.2f %1.8f \n', y)
fprintf('The cubic function is: %1.2f %1.3f \n', y)
sentence = input('It is sunny? (Y) or (N): ', 's')
if (sentence == 'Y')
display ('go outside')
else
display( 'take umbrella')
end
% This program illustrates the use of if-then-else % statement %************* % The program provides the user with the Tax Rate % for three different states. % The three states are: % Texas(T), Virginia(V) and Delaware(D)
disp('Please enter the state you want to see the tax rate')
state=input('Type T for Texas, V for Virginia or D for Delaware: ', 's')
if state== 'T' SalesTax=.0835; statename= 'Texas,';
elseif state=='V' SalesTax=.085; statename= 'Virginia,';
elseif state=='D' SalesTax=.079; statename= 'Delaware,';
else display('Error. Only allowed: T, V or D') SalesTax= 0.00 ; statename= 'NOT REGISTERED HERE,';
End
fprintf('For the state of: %s the sales tax is: %1.3f\n', statename, SalesTax)