What Does This Program Do in ACSL? Strings
DR. QIONG CHENG�COMPUTER SCIENCE DEPARTMENT
UNIVERSITY OF NORTH CAROLINA AT CHARLOTTE
1
Copyright © 2021by Qiong Cheng
Motivation
2
ACSL - What Does This Program Do in ACSL?
3
Understanding ACSL Pseudo-code
4
Constructs Used in the ACSL Pseudo-code
5
Constructs Used in the ACSL Pseudo-code
6
Constructs Used in the ACSL Pseudo-code
7
Operators
8
in that order of precedence
high
low
Operators
9
in that order of precedence
high
low
Expression:
Arithmetic expression: (2 + 3 * 4)/2 + 3^2 + 2 ↑ 3 + 7 % 3
Logical expression:
- using comparison operators (>, <, >=, <=, !=, ==)
3 < 5, 3!= 5
- using logical operators (!, && (and), || (or))
! (3!=5)
(3 < 5) && (3!=5)
(3 < 5) || (3!=5)
Functions
10
Functions
11
Functions
12
Functions
13
Functions
14
Functions
15
Functions
16
Statements
17
Sequential Statements
18
Appearing in Contest 1
Decision Statements
19
DIM A AS INTEGER�INPUT A�IF A > 35 THEN� PRINT (A + 2)�ELSE� PRINT (A - 2)�END IF�END
DIM A AS INTEGER�INPUT A�IF A > 35 THEN PRINT (A + 2) ELSE PRINT (A - 2)�END
Appearing in Contest 1
Practice
20
Practice
21
Practice
22
Practice
23
Constructs Used in the ACSL Pseudo-code
24
Loopings
The FOR loop in BASIC is an example of a repetition statement. It allows a set of statements to continually be executed over and over again for a specific number of iterations. A loop variable is used after the keyword FOR and "lower" and "upper" bounds are specified. On the last line of the loop the keyward NEXT is followed by the loop variable. By default, the loop variable increments by one each time the loop iterates. When the loop variable equals the upper bound it iterates one more time. Then, the loop variable increments but the loop does not iterate and the rest of the program (below/after the loop) executes.
25
DIM A AS INTEGER�FOR A = 1 TO 5� PRINT A�NEXT A�END
DIM A, B AS INTEGER�B = 5�FOR A = 1 TO 10� IF A + B >= 12 THEN PRINT "GREATER"�NEXT A�END
Appearing in Contest 2
Practice
26
The table shows the values of I, J and X:
I | J | X |
1 | 1 | |
2 | 1 | |
2 | 2 | |
3 | 1 | |
3 | 2 | |
3 | 3 | |
4 | 1 | |
4 | 2 | |
4 | 3 | |
4 | 4 | |
Practice
27
Line(s) | j | k | n | A(j) | B(k) | C(n) |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
Arrays
28
Arrays
29
Appearing in Contest 3
3 | 5 | 7 | 9 | 11 |
A(3)
A(0)
0
1
2
3
4
Array A
What Does This Program Do – 1D Arrays
30
7 | 4 | 2 | 5 | 8 | 3 | 6 | 1 |
0
1
2
3
4
Array A
5
6
7
J | A |
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
| |
| |
| |
What Does This Program Do – 1D Arrays
31
8 | 5 | 2 | 1 | 0 | -1 | -2 | -3 |
0
1
2
3
4
Array A
5
6
7
J | A |
0 | A(0) =8 |
1 | A(1) =5 |
2 | A(3) = 1 |
3 | A(4)=0 |
4 | A(5)=-1 |
5 | A(6)=-2 |
6 | A(7)=-3 |
| |
| |
| |
K | A |
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
| |
| |
| |
What Does This Program Do – 1D Arrays
32
8 | 5 | 2 | 1 | 0 | 0 | -2 | -3 |
0
1
2
3
4
Array A
5
6
7
J | A |
0 | A(0) =8 |
1 | A(1) =5 |
2 | A(3) = 1 |
3 | A(4)=0 |
4 | A(5)=-1 |
5 | A(6)=-2 |
6 | A(7)=-3 |
| |
| |
| |
K | A |
0 | |
1 | |
2 | |
3 | |
4 | |
5 | A(5)=0 |
6 | |
| |
| |
| |
Practice
33
The table shows the values of J, A, T, and X:
Line | A | J | T | X |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
B | A | N | A | S |
0
1
2
3
4
Array A
| | | | |
0
1
2
3
4
Array T
34
| A | J | T | X |
1 | BANANAS | | | |
2 | | | . | 0 |
3 | | 7 | | |
4 | | | S | |
3 | | 6 | | |
4,5 | | | SA | |
3 | | 5 | | |
4,5 | | | SAN | |
3 | | 4 | | |
4,5 | | | SANA | |
3 | | 3 | | |
4,5 | | | SANAB | |
6 | | 1 | | |
7,8 | | | | |
6 | | 2 | | |
7,8 | | | | 1 |
6 | | 3 | | |
7,8 | | | | 2 |
6 | | 4 | | |
7,8 | | | | 3 |
6 | | 5 | | |
B | A | N | A | S |
0
1
2
3
4
Array A
S | A | N | A | B |
0
1
2
3
4
Array T
1
2
3
4
5
6
7
8
Practice
35
The table shows the values of J, A, T, and X:
Line | A | J | T | X |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
B | A | N | A | N | A | S |
0
1
2
3
4
Array A
5
6
| | | | | | |
0
1
2
3
4
Array T
5
6
Practice
36
The table shows the values of J, A, T, and NUM:
Line | A | J | T | NUM |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
37
Line no. | A | J | T | X |
1 | BANANAS | | | |
2 | | | . | 0 |
3 | | 7 | | |
4 | | | S | |
3 | | 6 | | |
4,5 | | | SA | |
3 | | 5 | | |
4,5 | | | SAN | |
3 | | 4 | | |
4,5 | | | SANA | |
3 | | 3 | | |
4,5 | | | SANAN | |
3 | | 2 | | |
4,5 | | | SANANA | |
3 | | 1 | | |
4,5 | | | SANANAB | |
6 | | 1 | | |
7,8 | | | | |
6 | | 2 | | |
7,8 | | | | 1 |
| | …. | | 5 |
B | A | N | A | N | A | S |
0
1
2
3
4
Array A
5
6
S | A | N | A | N | A | B |
0
1
2
3
4
Array T
5
6
1
2
3
4
5
6
7
8
38
1
2
3
4
5
6
7
8
9
10
11
12
The following table traces the variables through the execution of the program.
12 | 41 | 52 | 57 | 77 | -100 |
0
1
2
3
4
Array A
5
17 | 34 | 81 |
0
1
2
Array B
Outer Loop | Inner Loop | j | k | n | | A(j) | B(k) | C(n) |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | |
0
1
2
Array C
3
4
5
6
39
1
2
3
4
5
6
7
8
9
10
11
12
The following table traces the variables through the execution of the program.
12 | 41 | 52 | 57 | 77 | -100 |
0
1
2
3
4
Array A
5
17 | 34 | 81 |
0
1
2
Array B
Outer Loop | Inner Loop | j | k | n | | A(j) | B(k) | C(n) |
1 | | 0 | 0 | 0 | | 12 | 17 | |
| | 1 | | 1 | | 41 | | 12 |
2 | 1 | | 1 | 2 | | | 34 | 17 |
| 2 | | 2 | 3 | | | 81 | 34 |
| | 2 | | 4 | | 52 | | 41 |
3 | | 3 | | 5 | | 57 | | 52 |
4 | | 4 | | 6 | | 77 | | 57 |
5 | | 5 | | 7 | | -100 | | 77 |
| | | | | | | | |
| | | | | | | | |
12 | 17 | 34 | 41 | 52 | 57 | 77 |
0
1
2
Array C
3
4
5
6
Arrays
40
Appearing in Contest 3
3 | 5 | 7 |
9 | 1 | 3 |
4 | 2 | 1 |
5 | 7 | 9 |
Row index
Array B
0
1
2
0
1
2
3
B(2, 1)
B(0, 0)
Column index
03-04 C3 What Does this Program Do – 2D Arrays
41
41
8 | 1 | 6 |
3 | 5 | 7 |
4 | 9 | 2 |
Row index
Array A
0
1
2
0
1
2
Column index
Outer Loop | Inner Loop | J | K | A(J,K) | X |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
03-04 C3 What Does this Program Do – 2D Arrays
42
42
8 | 1 | 6 |
3 | 5 | 7 |
4 | 9 | 2 |
Row index
Array A
0
1
2
0
1
2
Column index
Outer Loop | Inner Loop | J | K | A(J,K) | X |
1 | 1 | 0 | 0 | 8 | 8 |
| 2 | | 1 | 1 | 7 |
| 3 | | 2 | 6 | 13 |
2 | 1 | 1 | 0 | 3 | 10 |
| 2 | | 1 | 5 | 5 |
| 3 | | 2 | 7 | -2 |
3 | 1 | 2 | 0 | 4 | 2 |
| 2 | | 1 | 9 | -7 |
| 3 | | 2 | 2 | -5 |
| | | | | |
43
I | J | A(I, J) | C |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | |
| | |
| | |
Array A
0
1
2
0
1
2
44
I | J | A(I, J) | C |
1 | 1 | 3 | 0 |
| 2 | 4 -> 2 | 2 |
| 3 | 5 | |
| 4 | 6 -> 3 | 3 |
| 5 | 7 | |
2 | 1 | 5 | |
| 2 | 6->3 | 4 |
| 3 | 7 | |
| 4 | 8->4 | 6 |
| 5 | 9 | |
3 | 1 | 7 | |
| 2 | 8->4 | 8 |
| 3 | 9 | |
| 4 | 10->5 | 9 |
| 5 | 11 | |
4 | 1 | 9 | |
| 2 | 10->5 | 10 |
| 3 | 11 | |
| 4 | 12->6 | 12 |
| 5 | 13 | |
| | | | | |
| 3 | 4 | 5 | 3 | 7 |
| 5 | 4 | 7 | 6 | 9 |
| 7 | 8 | 9 | 9 | 11 |
| 9 | 10 | 11 | 12 | 13 |
| 11 | 6 | 13 | 7 | |
Array A
0
1
2
0
1
2
4
3
3
4
5
5 | 1 | 11 | |
| 2 | 12->6 | 14 |
| 3 | 13 | |
| 4 | 14->7 | 15 |
| 5 | 15 | |
5
Arrays in Python
45
1 | 2 | | |
Two-dimension Arrays in Python: Array of Arrays
46
1
2
3
4
5
6
7
8
9
10
[0]
[1]
[2]
[3]
[0]
[1]
[2]
[3]
What Is A String?�
47
Strings
48
Appearing in Contest 4
The LEN function
49
Comparing Strings
50
Sample Problems
51
The program first stores the reverse of A$ into T$, and then counts the number of letters that are in the same position in both strings.
Those positions marked with an asterisk contribute one to the value of X. There are 5 such positions.
Sample Problems
52
I value | B |
0 | |
2 | |
4 | |
6 | |
8 | |
C | I | N | D | E | R | E | L | L | A |
index
A:
0
1
2
3
4
5
6
7
8
9
Sample Problems
53
J | X |
| |
| |
| |
| |
| |
| |
E | A | S | T | E | R |
index
A:
0
1
2
3
4
5
K | Y |
| |
| |
| |
Sample Problems
54
J | X |
0 | |
1 | |
2 | |
3 | T |
4 | TA |
5 | TAR |
E | A | S | T | E | R |
index
A:
0
1
2
3
4
5
K | Y |
0 | T |
1 | TA |
2 | |
The LEFT function
55
The RIGHT function
56
The MID function
57
The STR function
58