Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Tutorial 1: Algorithms and data structures 1

Exercise 1:
Write an algorithm that reads 2 numbers and determines the largest and smallest

Exercise 2: Write an algorithm that reads two integers and displays the quotient
and the rest of the Euclidean division of these two numbers.

Exercise 3:
Write an algorithm that requires the input of any letter of the alphabet. If the letter
entered corresponds to the initial of the day of the week, there is the following
display:
"The letter L corresponds to Monday" if the letter entered is L for example.
"The letter M corresponds to Tuesday or Wednesday" if the letter entered is M for
example.
Otherwise show
"The letter A does not correspond to any day of the week" if it is the case of the
letter A.

Exercise 4: Manual Execution


We consider the following algorithm

1 Algorithm computation;
2 x, y, z: integer;
3 begin
4 x:=5;
5 y:=10;
6 z:=2*x-y;
7 z:=y-6;
8 y:=x+z+6;
9 x:=6*y;
10. write(x)
11. y:=y-1;
12 write (y,z);
13 end
a) perform the manual execution of this algorithm;
b) deduce the values of x, y, and z displayed.

Exercise 5:
Write an algorithm to swap 2 integer objects using an intermediate variable.

Exercise 6:
Write an algorithm that asks you to enter a word from the keyboard, and then
displays the word entered, its frst and last letter, and its length.

Exercise 7:
Write an algorithm which reads 10 integer numbers, calculate their sum and
displays the result.

Exercise 8:
Write an algorithm that reads 12 real numbers and calculates the mean, variance,
standard deviation and then displays them.

Exercise 9:
Write an algorithm that reads a positive integer and calculates the factorial of that
number. The algorithm must accept only positive or zero numbers and ignore all
negative numbers.

Exercise 10:
Write an algorithm asking to enter a word of any length and determining whether
the word is a palindrome or not. (e.g. redivider, deifed, civic, radar, level, rotor,
kayak, reviver, racecar, redder, madam, and refer)

Exercise 11:
Write an algorithm that calculates the sum and the average of the n frst natural
integers, then displays the results.

Exercise 12:
Write an algorithm which allows to reads 2 positives integers and calculates their
product using the following principle.
AxB=A+A+A+...+A, (B times).

Exercise 13:
Write an algorithm that reads an integer n and determines whether it is a prime
number or not. Principle: if any number less than n and diferent from 1 is not
divisor of n, then n is prime.

Exercise 14:
Write the algorithm that asks to enter a square matrix of order n, displays it,
calculates its trace and displays it. The algorithm then searches for the smallest
element of the matrix, the poster and its various coordinates.

Exercise 15:
Write an algorithm that reads an integer and displays its equivalent in binary

Exercise 16:
Write an algorithm that reads an integer n, calculates the elements of the pascal
triangle of depth n and fnally displays the triangle thus obtained.

Exercise 17: Date conversion


Propose an algorithm that converts a given date as the number of the day in the
year into a date that expresses the number of the day in the month. For example
32/2007 corresponds to 01/02/2007

Exercise 18: Number between two days


Propose an algorithm to calculate the number of days between two given dates in
the form dd / mm / yyyy. It is assumed that both dates belong to the same year.
Exercise 19: Date of the following day
Propose an algorithm that reads a date in the form dd / mm / yyyy and determines
the date of the next day.

Exercise 20: System of equations


Write an algorithm that solves a linear system of two equations with two unknowns
parameters.

Exercise 21: Magic square


A magic square is a matrix of order n whose elements are all integers between 1
and n2 such that the sum of the elements of each column and the sum of the
elements of each diagonal line are equal. Propose an algorithm that builds a magic
square of order n.

Exercise 22:Verifcation of a permutation


Write an algorithm that reads an integer n then a permutation of n elements and
verifes that the values entered constitute a permutation of 1, ..., n.

Exercise 23:
Write an algorithm that reads 2 square matrices of order n, posters, calculates their
product and displays.

Exercise 24:Scalar product of two vectors


Let U and V be two vectors of real n and of components u1, u2, ..., un and v1, v2 ...,
vn respectively. Calculate the U * V scalar p

You might also like