4.3 Exercises
99
Exercise 4.6: Function for Area of a Rectangle
Write a program that computes the area A = bc of a rectangle. The values of b
and c should be user input to the program. Also, write the area computation as
a function that takes b and c as input parameters and returns the computed area.
Let the program print the result to screen along with an appropriate text. Run the
program with b = 2 and c = 3 to confirm correct program behavior.
Filename: function_area_rectangle.py.
Exercise 4.7: Average of Integers
Write a program that gets an integer N > 1 from the user and computes the average
of all integers i = 1, . . . , N. The computation should be done in a function that
takes N as input parameter. Print the result to the screen with an appropriate text.
Run the program with N = 5 and confirm that you get the correct answer.
Filename: average_1_to_N.py.
Exercise 4.8: When Does Python Check Function Syntax?
You are told that, when Python reads a function definition for the first time, it does
not execute the function, but still checks the syntax.
Now, you come up with some code lines to confirm that this is the case.
Hint You may, for example, use a print command and a deliberate syntax error in
a modification of ball_function.py (note that the modified code is one of those
quick “one-time” tests you might make for yourself, meant to be deleted once you
have the answer).
Filename: when_check_function_syntax.py.
Exercise 4.9: Find Crossing Points of Two Graphs
Consider two functions f (x) = x and g(x) = x 2 on the interval [−4, 4].
Write a program that, by trial and error, finds approximately for which values
of x the two graphs cross, i.e., f (x) = g(x). Do this by considering N equally
distributed points on the interval, at each point checking whether |f (x)−g(x)| < <,
where is some small number. Let N and be user input to the program and let the
result be printed to screen. Run your program with N = 400 and = 0.01. Explain
the output from the program. Finally, try also other values of N, keeping the value
of fixed. Explain your observations.
Filename: crossing_2_graphs.py.
Exercise 4.10: Linear Interpolation
Some measurements y i , i = 0, 1, . . . , N, of a quantity y have been collected
regularly, once every minute, at times t i = i, i = 0, 1, . . . , N. We want to find
the value y in between the measurements, e.g., at t = 3.2 min. Computing such y
values is called interpolation.
Let your program use linear interpolation to compute y between two consecutive
measurements:
1. Find i such that t i ≤ t ≤ t i+1 .
99
Exercise 4.6: Function for Area of a Rectangle
Write a program that computes the area A = bc of a rectangle. The values of b
and c should be user input to the program. Also, write the area computation as
a function that takes b and c as input parameters and returns the computed area.
Let the program print the result to screen along with an appropriate text. Run the
program with b = 2 and c = 3 to confirm correct program behavior.
Filename: function_area_rectangle.py.
Exercise 4.7: Average of Integers
Write a program that gets an integer N > 1 from the user and computes the average
of all integers i = 1, . . . , N. The computation should be done in a function that
takes N as input parameter. Print the result to the screen with an appropriate text.
Run the program with N = 5 and confirm that you get the correct answer.
Filename: average_1_to_N.py.
Exercise 4.8: When Does Python Check Function Syntax?
You are told that, when Python reads a function definition for the first time, it does
not execute the function, but still checks the syntax.
Now, you come up with some code lines to confirm that this is the case.
Hint You may, for example, use a print command and a deliberate syntax error in
a modification of ball_function.py (note that the modified code is one of those
quick “one-time” tests you might make for yourself, meant to be deleted once you
have the answer).
Filename: when_check_function_syntax.py.
Exercise 4.9: Find Crossing Points of Two Graphs
Consider two functions f (x) = x and g(x) = x 2 on the interval [−4, 4].
Write a program that, by trial and error, finds approximately for which values
of x the two graphs cross, i.e., f (x) = g(x). Do this by considering N equally
distributed points on the interval, at each point checking whether |f (x)−g(x)| < <,
where is some small number. Let N and be user input to the program and let the
result be printed to screen. Run your program with N = 400 and = 0.01. Explain
the output from the program. Finally, try also other values of N, keeping the value
of fixed. Explain your observations.
Filename: crossing_2_graphs.py.
Exercise 4.10: Linear Interpolation
Some measurements y i , i = 0, 1, . . . , N, of a quantity y have been collected
regularly, once every minute, at times t i = i, i = 0, 1, . . . , N. We want to find
the value y in between the measurements, e.g., at t = 3.2 min. Computing such y
values is called interpolation.
Let your program use linear interpolation to compute y between two consecutive
measurements:
1. Find i such that t i ≤ t ≤ t i+1 .
