Professional Documents
Culture Documents
Series 1
Series 1
Note: In this series of exercises, for each parameterised action requested, you must specify
whether it is a function or a procedure, naming the input parameters and the output parameters.
Exercise 1
1. Write a parameterized action to check whether a real number is positive or not, and use
it action in the algorithm that calculates the square root of a given value.
2. Write the parameterized action which takes as input two positive integer values A and
B, and calculates the first as a power of the second (AB ).
3. Write the parameterized action that calculates the product of two positive integer values
A and B using the successive addition method.
Exercicse 2
A perfect number is a natural number N such that the sum of its positive integer divisors = 2 *
N . In other words, a natural number is perfect if it is equal to half the sum of its divisors. So 6
is a perfect number because 2 × 6 = 12 = 1 + 2 + 3 + 6, or 6 = 1 + 2 + 3.
We want to write an algorithm that displays all the perfect numbers in an interval of values
given by the user, to do this we use parameterised actions.
Exercicse 3
Using a parameterized action that calculates the factorial of a positive integer, write the
algorithm that calculates the sum S such that :
S= 1+(1*2)+(1*2*3)+(1*2*3*4)+…..+(1*2*3*4*…*n-1*n)
Exercicse 4
Write the algorithm that splits this array into two arrays, the first array for positive
values, and the second array for negative values.
We want to do the same job as the previous algorithm, using subprograms. To do this, we use
a subprogram called Split, which splits an array in two.
Exercise 6
Write an algorithm to calculate the number of occurrences of a value given by the user in a
matrix A[M, N] of integers.
We now want to do the same job as the previous algorithm using a parameterized action called
Occurrence.
1. Should the parameterized action used be a function or a procedure? Justify your answer.
2. Write the parameterized action Occurrence, specifying its parameters.
3. Use the parameterized action Occurrence in an algorithm to calculate the number of
occurrences of 3 different values X1, X2, and X3 respectively in 3 integer matrixes: A
[M, N], B [M, N] and C [M, N].