What Does This Program Do?
Frequently, one must use or modify sections of another programmer’s code. Since the original author is often unavailable to explain his/her code, it is essential to be able to read and understand an arbitrary program. This category has been rewritten using pseudocode similar to the design of the AP CS Principles course. Pseudo-code is English-like, language independent algorithmic code and should be able to be traced by students regardless of whether they are familiar with BASIC, C++, Java, Python, or any other high level language.
Description of Pseudo-code
We will use the following constructs in writing this code for this topic in ACSL:
|Operators||! (not) , ^ (exponent), *, / (real division), % (modulus), +, -, >, <, >=, <=, !=, ==, && (and), [math]||[/math] (or) in that order of precedence|
|Variables||Start with a letter, only letters and digits|
|Indefinite Loop statements||
|Definite Loop statements||
|Arrays:||1D arrays use a single subscript such as A. 2D arrays use row major order starting with [0,0] in the upper left corner. Use  for identifying the subscript(s) so that A is the 6th item in a list and A[2,3] is row 2 (third row) and column 3 (4th column). The size of the array will be specified in the problem statement.|
|Strings:||They can contain 0 or more characters and the indexed position starts with 0 as 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 characters. Strings are identified with surrounding double quotes. Use  for identifying the characters in a substring of a given string as follows: If S = “ACSL WDTPD”, then S[:3] = “ACSL” (positions 0 to 3), S[5:] = “WDTPD” (positions 5 to 9), S[2:6] = “SL WD” (positions 2 to 6), and S = “A” (position 0 only). The value of len(A) is 10.|
The questions in this topic will cover any of the above constructs in the Intermediate and Senior Division. In the Junior Division, loops will not be included in contest 1; loops will be used in contest 2; strings will be used in contest 3; and arrays will be included in contest 4. Line numbers will be included for the purpose of explaining the solution.
Sample Problem 1
After this program is executed, what is the value of B that is printed if the input values are 50 and 10?
- 10 input H, R
- 20 B = 0
- 30 if H>48 then
- 40 B = B + (H - 48) * 2 * R
- 50 H = 48
- 60 end if
- 70 if H>40 then
- 80 B = B + (H - 40) * (3/2) * R
- 90 H = 40
- 100 end if
- 110 B = B + H * R
- 120 output B
This program computes an employee’s weekly salary, given the hourly rate (R) and the number of hours worked in the week (H). The employee is paid an hourly rate for the number of hours worked, up to 40, time and a half for the overtime hours, up to 48 hours, and double for all hours over 48. The table monitors variables B and H:
Therefore, the final value of B is 2*2*10 + 8*3/2*10 + 40*10 = 40 + 120 + 400 = 560.
Sample Problem 2
After the following program is executed, what is the final value of NUM?
- 10 A = “BANANAS”
- 20 NUM = 0: T = “”
- 30 for J = len(A) - 1 to 0 step –1
- 40 T = T + A[j]
- 50 next
- 60 for J = 0 to len(A) - 1
- 70 if A[J] == T[J] then NUM = NUM + 1
- 80 next
The program first stores the reverse of variable A into variable T and then counts the number of letters that are in the same position in both strings. A B A N A N A S T S A N A N A B * * * * * Those positions marked with an asterisk contribute one to the value of NUM. There are 5 such positions.
Sample Problem 3
After the following program is executed, what is the final value of C?
- 10 A = 12: A = 41: A = 52
- 20 A = 57: A = 77: A = -100
- 30 B = 17: B = 34: B = 81
- 40 J = 0: K = 0: N = 0
- 50 while A[J] > 0
- 60 while B[K] <= A[J]
- 70 C[N] = B[K]
- 80 N = N + 1
- 90 k = k + 1
- 100 end while
- 110 C[N] = A[J]: N = N + 1: J = J + 1
- 120 end while
- 130 C[N] = B[K]
The following table traces the variables through the execution of the program.
Thus, the value of C is 52. Note that this program merges two arrays in increasing order into one array until a negative number is input.
The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads.