1 of 26

What Does This Program Do in ACSL?

DR. QIONG CHENG�COMPUTER SCIENCE DEPARTMENT

UNIVERSITY OF NORTH CAROLINA AT CHARLOTTE

1

Copyright © 2021by Qiong Cheng

2 of 26

Motivation

  • Frequently one must use or modify sections of another programmer’s code.
  • It is essential to be able to read and understand an arbitrary program.
  • Code fragments used in this category are examples of “good” BASIC programming.

2

3 of 26

ACSL - What Does This Program Do in ACSL?

  • This category presents a program and asks the student to determine that the program does.

  • The programs are written using a pseudocode that should be readily understandable by all programmers familiar with a high-level programming language, such as Python, Java, or C.

3

4 of 26

Understanding ACSL Pseudo-code

  1. evaluation of arithmetic expressions
  2. single & nested loops (FOR & DO loops)
  3. branching (IF/THEN statements)
  4. subscripted variables
  5. multi-dimensional arrays
  6. string manipulations

4

5 of 26

Constructs Used in the ACSL Pseudo-code

5

6 of 26

Constructs Used in the ACSL Pseudo-code

  • - Operators
  • - Functions
  • - Variables
  • - Sequential statements
  • - Decisional statements

6

7 of 26

Constructs Used in the ACSL Pseudo-code

7

8 of 26

Operators

  • ! (not)
  • ^ or ↑(exponent)
  • *, / (real division)
  • % (modulus)
  • +, -
  • >, <, >=, <=, !=, ==
  • && (and)
  • || (or)

8

in that order of precedence

high

low

9 of 26

Operators

  • ! (not)
  • ^ or ↑(exponent)
  • *, / (real division)
  • % (modulus)
  • +, -
  • >, <, >=, <=, !=, ==
  • && (and)
  • || (or)

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)

10 of 26

Functions

  • int(x) - greatest integer <= x (returning the next least whole number) Namely, it truncates a number down. This is especially tricky when the argument (the number in the parentheses) is negative. 
  • Examples

  • a. INT(3.2) = 3
  • b. INT(-5.4) = -6
  • c. INT(0) = 0
  • d. INT(-0.4) = -1

10

11 of 26

Functions

  • abs(x) - absolute value (returning the absolute value of the argument)
  • Examples

  • a. ABS(-3) = 3
  • b. ABS(0) = 0
  • c. ABS(14) = 14
  • d. ABS(1236) = 1236
  • e. ABS(-23.45) = 23.45
  • f. ABS(5 * -4) = ABS(-20) = 20

11

12 of 26

Functions

  • sqrt(x) - square root. The SQR (stands for Square Root) function returns the square root of the argument.
  • LEFT$(A$, N) - The LEFT$ (pronounced, "left string") function returns the first N characters (including embedded spaces) from the left end of the string, A$. It requires two arguments. If the value for N is 0, the empty (or null) string is returned. If the value for N exceeds the length of the string A$ then the whole string A$ is returned (with no extra padded spaces).

  • Examples
  •                 a. LEFT$("Programming", 3) = "Pro"�                b. LEFT$("Basic", 1) = "B"�                c. LEFT$("Wyomissing Area", 11) = "Wyomissing "         Note that here is a trailing space in the answer after the 'g'�                d. LEFT$("abcd", 0) = ""        (the null string)�                e. LEFT$("maryland", 3 - 2) = "m"         You must evaluate the expression, 3-2, before using LEFT$�                f. LEFT$("high school", 100) = "high school"

12

13 of 26

Functions

  • RIGHT$(A$, N) - The RIGHT$ (pronounced, "right string") function returns the last N characters (including embedded spaces) from the right end of the string, A$. It requires two arguments. If the value for N is 0, the empty string is returned. If the value for N exceeds the length of the string A$ then the whole string A$ is returned. The characters that are returned are not reversed, but rather they keep their original relative order.

  • MID$(A$, B, C) - The MID$ (pronounced, "mid string") function returns the next C characters from the string A$, starting with (and including) the Bth character. It requires three arguments. If the value for C is 0, the empty string is returned. If the value for C exceeds the number of remaining characters after the Bth character, the remaining portion of the string is returned nevertheless with no extra padded spaces.
  • Examples
  •         a. RIGHT$("where", 10) = "where“
  • b. RIGHT$("United States", 7) = " States“
  • c. MID$("Wyomissing", 4, 1) = "m"�        d. MID$("high school", 6, 0) = ""    the empty string�         f. MID$("abcdef", 5, 24) = "ef"

13

14 of 26

Functions

  • LEN - The LEN (stands for Length) function returns the length of a string. You obtain the length of a string simply by counting the number of characters (including spaces) in that string. The length of the null string is zero.
  • STR$ - The STR$ (stands for String and pronounced "string function") function turns a value into a string literal. The final answers to all of the examples below must include double quotes.
  • Examples
  •                 a. LEN("computer") = 8         
  • b. STR$(2.789) = "2.789"�            c. STR$(0) = "0"      

14

15 of 26

Functions

  • VAL - The VAL (stands for Value) function turns a string literal (that happens to "look like" a numerical value) into a numerical value that could later be added, subtracted, multiplied, etc. in a mathematical expression. Until a string literal such as "15" is turned into a value (like 15, without the quotes), it cannot be manipulated mathematically.
  • SGN - The SGN (stands for Sign) function returns the sign of the argument. That is, it returns -1, if the argument is a negative number. It returns 1 if the argument is a positive number and it returns 0 if the argument is 0, itself. The SGN function returns no other values than 0, 1, and -1.
  • Examples
  •                 a. VAL("-8.9") = -8.9         
  • b. SQR(25) = 5           
  • c. SQR(7) = 2.64575  
  • d. SGN(-23) = -1    
  • e. SGN(0) = 0
  • f. SGN(4.345) = 1

15

16 of 26

Functions

  • VAL - The VAL (stands for Value) function turns a string literal (that happens to "look like" a numerical value) into a numerical value that could later be added, subtracted, multiplied, etc. in a mathematical expression. Until a string literal such as "15" is turned into a value (like 15, without the quotes), it cannot be manipulated mathematically.
  • SGN - The SGN (stands for Sign) function returns the sign of the argument. That is, it returns -1, if the argument is a negative number. It returns 1 if the argument is a positive number and it returns 0 if the argument is 0, itself. The SGN function returns no other values than 0, 1, and -1.
  • Examples
  •                 a. VAL("-8.9") = -8.9         
  • b. SQR(25) = 5           
  • c. SQR(7) = 2.64575  
  • d. SGN(-23) = -1    
  • e. SGN(0) = 0
  • f. SGN(4.345) = 1

16

17 of 26

Statements

  • Sequential statement
  • Decision statements
  • Indefinite Loop statements
  • Definite Loop statements

17

18 of 26

Sequential Statements

  • INPUT variable
  • variable = expression (assignment)
  • OUTPUT variable

  • One commonly used statement in BASIC is the INPUT statement. Presenting a variable name after the keyword INPUT stops the program and allows the user to enter a value for that variable. For example,
  • DIM A AS INTEGER�INPUT A�PRINT A�END

18

Appearing in Contest 1

19 of 26

Decision Statements

  • IF boolean expression THEN
  • Statement(s)
  • ELSE (optional)
  • Statement(s)
  • END IF
  • The user is given the chance to input a value for A. That value is then printed on the screen by the PRINT statement.
  • DIM A, B AS INTEGER�INPUT A, B�IF A > 4 THEN PRINT "A"�IF B > 10 THEN PRINT B�END

19

DIM A AS INTEGERINPUT AIF A > 35 THEN     PRINT (A + 2)ELSE     PRINT (A - 2)END IFEND

DIM A AS INTEGERINPUT AIF A > 35 THEN PRINT (A + 2) ELSE PRINT (A - 2)END

Appearing in Contest 1

20 of 26

Practice

  • When the following program is run, what value is printed?
  •  
  • A = 1: B = 2: C = 3: D = 4
  • If A + C = D then D = D - 2
  • If 2 * D + 4 * A < 10 then A = A + 4
  • If A + D > 3 * B + C then B = C – A
  • If B < C then C = B
  • Print A + B + C + D

20

21 of 26

Practice

  • Print the value of G after the following program is executed.
  •  
  • A = 36: B = 4: C = 5
  • D = A / B + C
  • E = (2 * D + 2) / C
  • IF E > B THEN B = E ELSE E = 3 * B
  • F = A / (B – 1) * C
  • IF F / 2 = INT (F/2) THEN F=F+1 ELSE F = (F-1) / 2
  • G = F * B * E
  • PRINT G

21

22 of 26

Practice

  • What value of D is printed after this program is run?
  • A = 100: B = 10: C = 5: D = 2
  • IF (A / (B * C)) < D THEN A = A/2 ELSE B = B + 2
  • IF (A + 2 * B) >= (C * D) ↑ 2 THEN C = 2 * C ELSE D = D↑2
  • IF (A - C↑2) >= (B * D↑2) THEN A = A/2 ELSE D = D*C
  • IF ((A*B)/C) < (B*C)/D THEN B=B/2 ELSE C= C↑2
  • IF (A>B) OR (C<D) THEN A=A/2 ELSE C=C+4
  • IF (A<C) AND (B>D) THEN B=2*B ELSE D=D+1
  • PRINT D
  • END

22

23 of 26

Practice

  • What are the final values of A, B, C, and D after this program is run?
  • A = 1: B = 2: C = 4: D = 8
  • IF A+D > B*C THEN A = 2 * A ELSE C = C – 2
  • IF A * C – 1 < D + B THEN B = D + 2 ELSE D = B – A
  • IF 3 * C > B – A THEN A = B ELSE C = D
  • IF (A > B) OR (D > C) THEN A = C ELSE B = D
  • IF (A – D > 0) AND (B < C) THEN D = C + 1 ELSE B = A – 2
  • END

23

24 of 26

Loopings

  • Indefinite Loop statements
    • WHILE Boolean expression
    • Statement(s)
    • END WHILE
  • Definite Loop statements
    • FOR variable = start TO end STEP increment
    • Statement(s)
    • NEXT

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.

24

DIM A AS INTEGERFOR A = 1 TO 5     PRINT ANEXT AEND

DIM A, B AS INTEGERB = 5FOR A = 1 TO 10     IF A + B >= 12 THEN PRINT "GREATER"NEXT AEND

Appearing in Contest 2

25 of 26

Arrays

  • 1 dimensional arrays use a single subscript such as A(5).
  • 2 dimensional arrays use (row, col) order such as A(2,3).
  • Arrays can start at location 0 for 1 dimensional arrays and location (0,0) for 2 dimensional arrays. Most ACSL past problems start with either A(1) or A(1,1).
  • The size of the array will usually be specified in the problem statement.

25

Appearing in Contest 3

26 of 26

Strings

  • Strings can contain 0 or more characters and the indexed position starts with 0 at the first character.
  • An empty string has a length of 0.
  • Errors occur if accessing a character that is in a negative position or equal to the length of the string or larger. 
  • The len(A) function will find the length of the string which is the total number of characters.
  • Strings are identified with surrounding double quotes.
  • Use [ ] for identifying the characters in a substring of a given string as follows:
    • S = “ACSL WDTPD” (S has a length of 10 and D is at location 9)
    • S[:3] = “ACS” (take the first 3 characters starting on the left)
    • S[4:] = “DTPD” (take the last 4 characters starting on the right)
    • S[2:6] = “SL WD” (take the characters starting at location 2 and ending at location 6)
    • S[0] = “A” (position 0 only).
  • String concatenation is accomplished using the + symbol

26

Appearing in Contest 4