Title: CS 101 Databases
1CS 221 Chapter 4
Matlab Programming
2Definite (for) loop
startloop1 endloop5 sum0for
istartloopendloop sumsumiend sum ?
3Definite (for) loop
i1 sum1 i2 sum3 i3 sum6 i4 sum10 i5
sum15 After loop sum 15
4For loops to access array
for index13 A(index) indexend A(4) ?
5For loops to access array
A 1 2 3 A(4) does not exist
6For loops to access array
for r13 for c13 A(r,c) rc
end end A(3,3) ?
7For loops to access array
A 2 3 4 3 4 5 4
5 6 A(3,3) 6
8Decision (if) statement
A1 95 A2 95 A3 80 A4 0 if A3 lt A2
fprintf(true) else fprintf(false) end
9Displayed in command window true
10Decision (if) statement
A1 95 A2 95 A3 80 A4 0 if A3 A2
fprintf(1\n) elseif A1 lt A2
fprintf(2\n) else fprintf(3\n) end
11Decision (if) statement
Displayed in command window 3
12Decision (if) statement
A1 95 A2 95 A3 80 A4 0 if A3 A2
A4 gt 0 fprintf(true) else
fprintf(false) end
13Displayed in command window
false
14Decision (if) statement
A1 95 A2 95 A3 80 A4 0 if A1 A2
A4 lt 0 fprintf(true\n) else
fprintf(false\n) end
15Displayed in command window
true
16while loop
sum 0 n 1 while n lt 5 sum sum n
n n 1 end sum ?
17while loop
sum1 n1 sum3 n2 sum6 n3 sum10 n4 sum15
n5 After loop Sum 15
18while (indefinite) loop
r 1 while r lt 3 c 1 while c lt
3 A(r,c) r c c c 1
end r r 1 end A(3,3) ?
19while (indefinite) loop
A 2 3 4 3 4 5 4
5 6 A(3,3) 6
20while (indefinite) loop
r 1 while r lt 3 c 1 while c lt
2 A(r,c) r c c c 1
end r r 1 end A(3,2) ?
21while (indefinite) loop
A 1 2 2 4 3
6 A(3,2) 6
22for/if combination
for r 13 for c 13 if r c
A(r,c) r c else
A(r,c) r c end
end end A(3,3) ?
23for/if combination
A 0 3 4 3 0 5 4
5 0 A(3,3) 0
24for/while/if combination
for r 13 c 1 while c lt 4
if r c A(r,c) r c
else A(r,c) r - c end
c c 1 end end A(3,1) ?
25for/while/if combination
A 2 -1 -2 1 4 -1 2
1 6 A(3,1) 2
26A 1 2 3 4 i 1 while i lt 5 B(i)
A(i) i i 2 end
- Program runs correctly
- 2. Program has an error, will not run CORRECT
27x 1 while x lt 3 y y x x x
1 end
- Program runs correctly
- 2. Program has an error, will not run CORRECT
28C(1) 0 for k 2 5 C(k) C(k-1) k
end
- Program runs correctly CORRECT
- 2. Program has an error, will not run
29k 2 i 1 while i lt 5 F(i) k i
if i gt 2 F(i) k - i
end k k 1end
- Program runs correctly
- 2. Program has an error, will not run CORRECT
30On completion of Chapter 4 know
- Decision (if) statements including elseif, else
- Definite(for) loops, including nesting
- Indefinite (while) loops including nesting
- Relational operators (, lt, gt, lt, gt, )
- Logical operators ( , , , , )
- Using the above in combinations
- Formatted output (fprintf) but no written exam
questions