KM134 Computer Programming - PowerPoint PPT Presentation

1 / 52
About This Presentation
Title:

KM134 Computer Programming

Description:

Advanced Program Scripts: SWITCH, LOOP, ARRAY, FUNCTION, ... Enter string: Hakan Uraz. String is: Hakan Uraz. String with spaces between characters is: ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 53
Provided by: chemEngA
Category:

less

Transcript and Presenter's Notes

Title: KM134 Computer Programming


1
KM134Computer Programming
Advanced Program Scripts SWITCH, LOOP, ARRAY,
FUNCTION, RECURSION EXAMPLES
  • Ankara University
  • Chemical Engineering Department
  • Assistant Prof. Dr. Refik SAMET

2
switch example
  • day input('Which day of the week? ')
  • switch day
  • case 1
  • day 'Monday'
  • case 2
  • day 'Tuesday'
  • case 3
  • day 'Wednesday'
  • case 4
  • day 'Thursday'
  • case 5
  • day 'Friday'
  • case 6
  • day 'Saturday'
  • case 7
  • day 'Sunday'
  • end

3
OUTPUT
  • prog27
  • Which day of the week? 4
  • day
  • Thursday

4
loop, switch, menu
  • clear
  • clc
  • anamenu 0
  • while anamenu lt 5
  • anamenu menu('Baslik', 'x entry', 'y
    entry',...
  • 'z entry', 'start', 'quit')
  • switch anamenu
  • case 1, x input('Enter x ')
  • case 2, y input('Enter y ')
  • case 3, z input('Enter z ')
  • case 4, f x2y2z2
  • fprintf('The value of function
    d\n', f)
  • case 5, break
  • end
  • end

5
OUTPUT
prog28 Enter x 3 Enter y 2 Enter z 1 The
value of function 14
6
switch
  • val input('Enter grade ', 's')
  • switch val
  • case 'A', 'a'
  • disp('Very good')
  • case 'B', 'b'
  • disp('Good')
  • case 'C', 'c'
  • disp('Fair')
  • case 'D', 'd'
  • disp('Bad')
  • end

7
OUTPUT
  • prog29
  • Enter grade B
  • Good

8
switch
  • val input('Enter grade ')
  • switch val
  • case 'A', 'a'
  • disp('Very good')
  • case 'B', 'b'
  • disp('Good')
  • case 'C', 'c'
  • disp('Fair')
  • case 'D', 'd'
  • disp('Bad')
  • end

9
OUTPUT
  • prog29b
  • Enter grade c
  • ??? Undefined function or variable 'c'.
  • Enter grade 'c'
  • Fair

10
loop, switch
  • aCount 0
  • bCount 0
  • cCount 0
  • dCount 0
  • fCount 0
  • disp('Enter the grades. Enter "z" to end...')
  • grade input('Enter ', 's')
  • while (grade 'z') (grade 'Z')
  • switch grade
  • case 'A', 'a'
  • aCount aCount 1
  • case 'B', 'b'
  • bCount bCount 1
  • case 'C', 'c'
  • cCount cCount 1
  • case 'D', 'd'
  • dCount dCount 1
  • case 'F', 'f'
  • fCount fCount 1

11
OUTPUT
  • prog3
  • Enter the grades. Enter "z" to end...
  • Enter b
  • Enter A
  • Enter A
  • Enter D
  • Enter A
  • Enter c
  • Enter z
  • 3 "A"s
  • 1 "B"s
  • 1 "C"s
  • 1 "D"s
  • 0 "F"s

12
function
  • for x110
  • fprintf('d\n', square(x))
  • end
  • square.m contains
  • function resultsquare(x)
  • takes the square of the given parameter
  • result x x

13
OUTPUT
  • prog31
  • 1
  • 4
  • 9
  • 16
  • 25
  • 36
  • 49
  • 64
  • 81
  • 100

14
NOTE
  • The comment after the function heading is used by
    the help function
  • help square
  • takes the square of the given parameter

15
function
  • number1 input('Enter number1 ')
  • number2 input('Enter number2 ')
  • number3 input('Enter number3 ')
  • fprintf('Maximum is d\n',...
  • findMax(number1, number2,
    number3))
  • findMax.m contains
  • function maxfindMax(x, y, z)
  • max x
  • if y gt max
  • max y
  • end
  • if z gt max
  • max z
  • end

16
OUTPUT
  • prog32
  • Enter number1 6
  • Enter number2 2
  • Enter number3 4
  • Maximum is 6

17
Built-in function rand
  • for i110
  • fprintf('3d', 1 round(5 rand))
  • if mod(i, 5) 0
  • fprintf('\n')
  • end
  • end

18
OUTPUT
  • prog33
  • 6 2 4 3 5
  • 5 3 1 5 3

19
An example game program
  • sum rollDice
  • switch sum
  • case 7, 11
  • gameStatus 'WON'
  • case 2, 3, 12
  • gameStatus 'LOST'
  • otherwise
  • gameStatus 'CONTINUE'
  • myPoint sum
  • fprintf('Point is d\n', myPoint)
  • end

20
Game program continued...
  • while (strcmp(gameStatus, 'CONTINUE') 1)
  • sum rollDice
  • if sum myPoint
  • gameStatus 'WON'
  • elseif sum 7
  • gameStatus 'LOST'
  • end
  • end
  • if strcmp(gameStatus, 'WON')1
  • fprintf('Player wins\n')
  • else
  • fprintf('Player loses\n')
  • end

21
Game program rollDice function
  • function sumrollDice
  • die1 round(1 rand 5)
  • die2 round(1 rand 5)
  • sum die1 die2
  • fprintf('Player rolled d d d\n',...
  • die1, die2, sum)

22
OUTPUT
  • prog34
  • Player rolled 4 5 9
  • Point is 9
  • Player rolled 6 5 11
  • Player rolled 2 3 5
  • Player rolled 6 6 12
  • Player rolled 3 5 8
  • Player rolled 1 3 4
  • Player rolled 5 1 6
  • Player rolled 2 2 4
  • Player rolled 2 4 6
  • Player rolled 2 2 4
  • Player rolled 1 5 6
  • Player rolled 3 6 9
  • Player wins

23
recursive function - factorial
  • for i110
  • fprintf('d! d\n', i, factorial(i))
  • end
  • factorial.m contains
  • function resultfactorial(number)
  • if number lt 1
  • result 1
  • else
  • result number factorial(number - 1)
  • end

24
OUTPUT
  • prog35
  • 1! 1
  • 2! 2
  • 3! 6
  • 4! 24
  • 5! 120
  • 6! 720
  • 7! 5040
  • 8! 40320
  • 9! 362880
  • 10! 3628800

25
Recursive function - fibonacci
  • number input('Enter ')
  • result fibonacci(number)
  • fprintf('Fibonacci( d ) d\n', number,
    result)
  • fibonacci.m contains
  • function resultfibonacci(n)
  • if n0 n1
  • result n
  • else
  • result fibonacci(n-1) fibonacci(n-2)
  • end

26
OUTPUT
  • prog36
  • Enter 5
  • Fibonacci( 5 ) 5
  • prog36
  • Enter 8
  • Fibonacci( 8 ) 21

27
array example
  • for i110
  • n(i) ii
  • end
  • fprintf('Element Value\n')
  • for i110
  • fprintf('7d7d\n', i, n(i))
  • end

28
OUTPUT
  • prog37
  • Element Value
  • 1 1
  • 2 4
  • 3 9
  • 4 16
  • 5 25
  • 6 36
  • 7 49
  • 8 64
  • 9 81
  • 10 100

29
array example
  • for i110
  • n(i) 2 2 i
  • end
  • fprintf('Element Value\n')
  • for i110
  • fprintf('7d7d\n', i, n(i))
  • end

30
OUTPUT
  • prog38
  • Element Value
  • 1 4
  • 2 6
  • 3 8
  • 4 10
  • 5 12
  • 6 14
  • 7 16
  • 8 18
  • 9 20
  • 10 22

31
array example
  • a 1 3 5 4 7 2 99 16 45 67 89 45
  • total 0
  • for i1length(a)
  • total total a(i)
  • end
  • fprintf('Total of array element values is
    d\n',...
  • total)

32
OUTPUT
  • prog39
  • Total of array element values is 383

33
array example
  • n 19 3 15 7 11 9 13 5 17 1
  • fprintf('Element Value Histogram\n')
  • for i1length(n)
  • fprintf('7d7d ',i, n(i))
  • for j1n(i)
  • fprintf('')
  • end
  • fprintf('\n')
  • end

34
OUTPUT
  • prog40
  • Element Value Histogram
  • 1 19
  • 2 3
  • 3 15
  • 4 7
  • 5 11
  • 6 9
  • 7 13
  • 8 5
  • 9 17
  • 10 1

35
string example
  • string input('Enter string ', 's')
  • fprintf('String is s\n, string)
  • fprintf(String with spaces between characters
    is\n',...
  • string)
  • for i1length(string)
  • fprintf('c ', string(i))
  • end
  • fprintf('\n')

36
OUTPUT
  • prog41
  • Enter string Hakan Uraz
  • String is Hakan Uraz
  • String with spaces between characters is
  • H a k a n U r a z

37
Call-by-value in functions
  • a 5
  • passValue(a)
  • a
  • passValue.m contains
  • function passValue(x)
  • x x 1
  • x

38
OUTPUT
  • prog42
  • a
  • 5
  • x
  • 6
  • a
  • 5

39
Call-by-value in functions
  • a 3 4 5
  • passArray(a)
  • a
  • passArray.m contains
  • function passArray(a)
  • a(1) a(1) 1
  • a(1)
  • a

40
OUTPUT
  • prog43
  • a
  • 3 4 5
  • ans
  • 4
  • a
  • 4 4 5
  • a

41
array sorting
  • a 2 6 4 8 10 12 89 68 45 37
  • for pass1length(a)-1
  • for i1length(a)-1
  • if a(i) gt a(i1)
  • hold a(i)
  • a(i) a(i 1)
  • a(i 1) hold
  • end
  • end
  • end
  • disp('Array in sorted ascending order')
  • a

42
OUTPUT
  • prog44
  • a
  • 2 6 4 8 10 12 89 68
    45 37
  • Array in sorted ascending order
  • a
  • 2 4 6 8 10 12 37 45
    68 89

43
Linear Search
  • a 20 12 8 13 67 18 38 47 62
  • searchKey input('Enter search key ')
  • element linearSearch(a, searchKey)
  • if element -1
  • fprintf('Found value in element d\n', ...
  • element)
  • else
  • fprintf('Value not found\n')
  • end

44
linearSearch function
  • function pos linearSearch(a, key)
  • pos -1
  • for n1length(a)
  • if a(n) key
  • pos n
  • end
  • end

45
OUTPUT
  • prog45
  • Enter search key 67
  • Found value in element 5
  • prog45
  • Enter search key 75
  • Value not found

46
Arrays two dimensions
  • array 1 2 3 4 5 6
  • disp('Array is ')
  • array
  • disp('Array is ')
  • n, m size(array)
  • for i1n
  • for j1m
  • fprintf('3d', array(i, j))
  • end
  • fprintf('\n')
  • end

47
OUTPUT
  • prog46
  • Array is
  • array
  • 1 2 3
  • 4 5 6
  • Array is
  • 1 2 3
  • 4 5 6

48
Arrays two dimensions
  • studentGrades 77 68 86 73 96 87 89 78 70 90
    86 81
  • fprintf('\nLowest grade d\nHighest grade
    d\n',...
  • minimum(studentGrades), maximum(studentGrades)
    )
  • students, examssize(studentGrades)
  • for student 1students
  • fprintf('The average grade for student d is
    5.2f\n',...
  • student, average(studentGrades,
    student))
  • end

49
minimum.m contains...
  • function lowGrademinimum(grades)
  • lowGrade 100
  • students, tests size(grades)
  • for i1students
  • for j1tests
  • if grades(i, j) lt lowGrade
  • lowGrade grades(i, j)
  • end
  • end
  • end

50
maximum.m contains...
  • function highGrademaximum(grades)
  • highGrade 0
  • students, tests size(grades)
  • for i1students
  • for j1tests
  • if grades(i, j) gt highGrade
  • highGrade grades(i, j)
  • end
  • end
  • end

51
average.m contains
  • function avgaverage(grades, student)
  • total 0
  • students, tests size(grades)
  • for i1tests
  • total total grades(student, i)
  • end
  • avg total / tests

52
OUTPUT
  • prog47
  • studentGrades
  • 77 68 86 73
  • 96 87 89 78
  • 70 90 86 81
  • Lowest grade 68
  • Highest grade 96
  • The average grade for student 1 is 76.00
  • The average grade for student 2 is 87.50
  • The average grade for student 3 is 81.75
Write a Comment
User Comments (0)
About PowerShow.com