Download presentation
Presentation is loading. Please wait.
1
CS005 Introduction to Programming:
Matlab Eamonn Keogh
2
Let us write a function called IsOdd, that takes in an integer, and returns true (‘1’) if it is odd, otherwise returns a false (‘0’). 2 7 IsOdd IsOdd 1
3
Here are some basic facts we can exploit:
EDU>> (7/2) ans = 3.5000 EDU>> (11/2) 5.5000 EDU>> (4/2) 2 EDU>> (18/2) 9 When I divide an odd number by 2, the answer is always “something.5000”. This is not the case for even numbers, they never have a fractional part.
4
Here are some basic facts we can exploit:
EDU>> (7/2) ans = 3.5000 EDU>> (11/2) 5.5000 EDU>> (4/2) 2 EDU>> (18/2) 9 EDU>> floor(7/2) ans = 3 EDU>> floor(11/2) 5 EDU>> floor(4/2) 2 EDU>> floor(18/2) 9 When I take the floor of a odd number divided by 2, the answer is always 0.5 smaller…. This is not the case for even numbers.
5
function WasOdd = IsOdd(IntegerToTest)
if IntegerToTest/2 > floor(IntegerToTest/2) WasOdd = 1; else WasOdd = 0; end
6
function WasOdd = IsOdd(IntegerToTest)
if IntegerToTest/2 > floor(IntegerToTest/2) WasOdd = 1; else WasOdd = 0; end EDU>> (7/2) ans = 3.5000 EDU>> floor(7/2) 3
7
function WasOdd = IsOdd(IntegerToTest)
if IntegerToTest/2 > floor(IntegerToTest/2) WasOdd = 1; else WasOdd = 0; end EDU>> (4/2) ans = 2 EDU>> floor(4/2)
8
Iteration (looping) true false The for statement Get the Users Score
Run some code multiple times Check for condition true Process code false Jump to end…
9
We can repeat a code block multiple times with the for statement
for index = values do these statements end
10
for i = 1 : 10 do these statements end Start with i at this value
Stop when i hits this value for i = 1 : 10 do these statements end Make i one larger when you get here
11
function DummyVar = SaySpamFiveTimes for i = 1 : 5 disp('Spam') end
EDU>> SaySpamFiveTimes Spam EDU>>
12
function DummyVar = SaySpam(TimesToRepeat) for i = 1 : TimesToRepeat
disp('Spam') end EDU>> SaySpam(2) Spam EDU>> SaySpam(0) EDU>> SaySpam(4) EDU>> SaySpam(50) Spam (I deleted the rest, eamonnn)
13
function DummyVar = CountToTen() for i = 1 : 10 disp(i) end
EDU>> CountToTen 1 2 3 4 5 6 7 8 9 10 EDU>>
14
function DummyVar = CountToK(K) for i = 1 : K disp(i) end
EDU>> CountToK(4) 1 2 3 4 EDU>> CountToK(0) EDU>> CountToK(2) EDU>>
15
function DummyVar = CountFromKuptoL(K,L) for i = K : L disp(i) end
EDU>> CountFromKuptoL(3,7) 3 4 5 6 7 EDU>> CountFromKuptoL(99,104) 99 100 101 102 103 104 EDU>>
16
function DummyVar = SevenTimesTable() for i = 1 : 10
disp('Seven times '); disp(i); disp(' is '); disp(i * 7); end >> SevenTimesTable Seven times 1 is 7 2 14 3 21 (I deleted the rest, eamonn)
17
Let us write a function called GetTriangularNumber, that takes in an integer T, and returns the Tth triangular number. 2 GetTriangularNumber 3
18
function DummyVar = CountToK(K)
% We saw this function a few slides ago.. for i = 1 : K disp(i) end EDU>> CountToK(6) 1 2 3 4 5 6 EDU>> So, we know how to print out all the integers from 1 to 6, if we could just add them up as we printed them, it would solve our problem, so: Let us work by adapting a working function that does something similar.
19
function DummyVar = CountToK(K)
% We saw this function a few slides ago.. for i = 1 : K disp(i) end Let us work by adapting a working function that does something similar. Rename the function Pass in T, the order of the triangular number Return the actual triangular number (we have more to do, next slide) function TriangularNumber = GetTriangularNumber(T) for i = 1 : T disp(i) end
20
function TriangularNumber = GetTriangularNumber(T)
for i = 1 : T disp(i) end function TriangularNumber = GetTriangularNumber(T) TriangularNumber = 0; for i = 1 : T TriangularNumber = TriangularNumber + i; end
21
EDU>> GetTriangularNumber(17) ans = 153 EDU>>
When they landed, they saw a fire of burning coals there with fish on it, and some bread. Jesus said to them, "Bring some of the fish you have just caught." Simon Peter climbed aboard and dragged the net ashore. It was full of large fish, 153, but even with so many the net was not torn. Jesus said to them, "Come and have breakfast." None of the disciples dared ask him, "Who are you?
22
function TriangularNumber = GetTriangularNumber(T)
% We can add some error checking if T < 0 disp('Error: Triangular Numbers are only defined for positive numbers') end TriangularNumber = 0; for i = 1 : T TriangularNumber = TriangularNumber + i; EDU>> GetTriangularNumber(2) ans = 3 EDU>> GetTriangularNumber(-3) Error: Triangular Numbers are only defined for positive numbers EDU>>
23
function TriangularNumber = GetTriangularNumber(T)
% Nesting the for loop inside the else block is better if T < 0 disp('Error: Triangular Numbers are only defined for positive numbers') else TriangularNumber = 0; for i = 1 : T TriangularNumber = TriangularNumber + i; end EDU>> GetTriangularNumber(2) ans = 3 EDU>> GetTriangularNumber(-3) Error: Triangular Numbers are only defined for positive numbers EDU>>
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.