control_flow_exercises

pdf

School

University of Massachusetts, Boston *

*We aren’t endorsed by this school

Course

110

Subject

Electrical Engineering

Date

Jan 9, 2024

Type

pdf

Pages

3

Uploaded by HighnessThunderCobra34

Report
Control Flow 1 Exercises Exercise 1. Consider the following code fragment: if m >= 1 and m <= 5: stdio.write( "Spring " ) elif m >= 6 and m <= 8: stdio.write( "Summer " ) else : stdio.write( "Fall " ) stdio.writeln(y) What does the program write when m and y take on the following values? a. m = 10 and y = 2020 b. m = 5 and y = 2021 c. m = 6 and y = 2022 Exercise 2. What does the following code fragment write? i = 9 while i >= 0: stdio.writeln( str (i) + " " + str (2 ** i)) i -= 2 Exercise 3. What are the arithmetic progressions returned by the following calles to range() ? a. range(-5) b. range(5) c. range(3, 10) d. range(3, 10, 2) e. range(5, -5, -1) Exercise 4. What does the following code fragment write? for i in range (3, 40, 4): if i % 5 == 0: stdio.writeln(i) Exercise 5. What does the following code fragment write? i = 1 for c in "hello" : stdio.writeln(c * i) i += 1 Exercise 6. What does the following code fragment write? for i in range (5): for j in range (6): if j == 5: stdio.writeln(i + j) else : stdio.write( str (i + j) + "-" ) 1 / 3
Control Flow Exercise 7. Implement a program called genaralizedharmonic.py that accepts n (int) and r (int) as command-line arguments and writes the value of the generalized harmonic number H ( n, r ) to standard output, computed using the formula H ( n, r ) = 1 1 r + 1 2 r + 1 3 r + · · · + 1 n r . Exercise 8. Implement a program called matrix.py that accepts n (int) and k (int) as command-line arguments and writes an n × n matrix in which the elements below the main diagonal are all zeros and the rest of the elements have the value k . The elements of the matrix must be separated by a single space and each row must end with a newline character at the end. ~/workspace/ipp/programs python matrix.py 5 2 2 2 2 2 2 0 2 2 2 2 0 0 2 2 2 0 0 0 2 2 0 0 0 0 2 _ Exercise 9. Consider the program gambler.py . a. How many variables does the program define? b. Write down the names of the variables and the scope of each variable. 2 Solutions Solution 1. a. Fall 2020 b. Spring 2021 c. Summer 2022 Solution 2. 9 512 7 128 5 32 3 8 1 2 _ Solution 3. a. [] b. [0, 1, 2, 3, 4] c. [3, 4, 5, 6, 7, 8, 9] d. [3, 5, 7, 9] e. [5, 4, 3, 2, 1, 0, -1, -2, -3, -4] 2 / 3
Control Flow Solution 4. 15 35 Solution 5. h ee lll llll ooooo Solution 6. 0-1-2-3-4-5 1-2-3-4-5-6 2-3-4-5-6-7 3-4-5-6-7-8 4-5-6-7-8-9 Solution 7. generalizedharmonic.py import stdio import sys n = int (sys.argv [1]) r = int (sys.argv [2]) total = 0 for i in range (1, n + 1): total += 1 / i ** r stdio.writeln(total) Solution 8. matrix.py import stdio import sys n = int (sys.argv [1]) k = int (sys.argv [2]) for i in range (n): for j in range (n): e = 0 if i > j else k if j == n - 1: stdio.writeln(e) else : stdio.write( str (e) + " " ) Solution 9. a. There are seven variables defined in gambler.py . b. Here are their names and scopes: Variable Scope stake lines 9 — 25 goal lines 10 — 25 trials lines 11 — 25 bets lines 12 — 25 wins lines 13 — 25 t lines 14 — 23 cash lines 15 — 23 3 / 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help