Agenda This is CS1371!!!!!!! Permanent move!!!!
%
% Concept: Procedural Abstraction
%
% Functions: text files with a .m extension
% - naming convention
% - do you know how sqrt works?
% - do you care?
%
% - calling functions
% - user defined functions
% - testing functions (with a script!)
**********Cylinder Function *****************
function volume = cylinder(height, radius) % Function Call is a must
base = pi * radius.^2 % pi is a constant, area of circle
volume = base * height % volume is area time height
end
***************** Problem Odd and Divisible **************************
function is = oddAndDivisible(num, dv1, dv2)
% Inputs (3): - (double) a number to check
% - (double) first divisor
% - (double) second divisor
% Outputs (1): - (logical) logical to see if the first input is odd and
% divisible by the second and third inputs
%
% Function Description:
% Write a function, oddAndDivisible, that checks to see if the first
% input is odd and divisible by the second and third inputs. If all the
% conditions are met, the function should output true. If even
% one of these conditions are not met, then the function should output
% false.
%
% Example:
% If the function call was:
%
% log = oddAndDivisible(35, 5, 7)
%
% First, it checks to see if 35 is odd (which it is). It would then
% check to see if 35 is divisible by 5 (which it is). It would then
% check to see if 35 is divisible by 7 (which it is). Because all of
% these conditions are true, then:
%
% log => true
isOdd = mod(num,2) == 1;
isDiv1 = mod(num/dv1, 1) == 0;
isDiv2 = mod(num/dv2, 1) == 0;
is = isOdd & isDiv1 & isDiv2;
end
Test Cases:
%
log1 = oddAndDivisible(35, 5, 7)
% log1 => true
%
log2 = oddAndDivisible(30, 5, 6)
% log1 => false
Hints:
% You will need the following information:
% *NOTE* Logical Comparisons ALWAYS return logicals (true or false)
% - Double Equal Comparison ( == )
% ~ Double Equal comparison is a way to compare if one statement
% equivalates to another. For example, if I wanted to know if
% A is equal to B, then I can make the following call:
%
% C = A == B
%
% In which C will be either:
% - true, if A is the same as B
% - false, if A is NOT the same as B
%
% - AND Comparison ( & )
% ~ AND comparison is as simple as it sounds. It compares
% multiple logicals, or logical statements. If every logical or
% logical statement is true, then a true will be returned.
% If any or all the statements are false, then a false will
% be returned. So consider the following statement:
%
% C = A & B
%
% Then C will be either:
% - true, if A and B are both true
% - false, if either A or B or both are false
********** Problem Who wants Candy ***********
%Test Cases:
% bagSize1 = 512;
% numKids1 = 22;
%
% [pieces1, wasted1] = candy(bagSize1, numKids1);
% pieces1 => 23
% wasted1 => 6
%
% bagSize2 = 300;
% numKids2 = 17;
%
% [pieces2, wasted2] = candy(bagSize2, numKids2);
% pieces2 => 17
% wasted2 => 11
function [pieces1, wasted1] = candy1(bagSize1, numKids1);
pieces = bagSize1/numKids1;
pieces1 = floor(pieces)
wasted1 = bagSize1 - (pieces1 * numKids1)
end