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

L01 / EE121 Algorithmic/ FALL 2022 / R.

NAMANE

Recitation#2: Conditional Constructs


Objectives:
The main objective of this recitation is to make students more familiar with the use of conditional statements in
solving problems.

Exercise 1: Even or odd?


1. Write an algorithm that reads a real number and checks whether it is even or odd. Draw the corresponding
flowchart of your algorithm.

Exercise 2: Absolute Values


1. Write an algorithm that calculates and displays the absolute value |𝑥𝑥|of a real number 𝑥𝑥 given by the user.
2. Write an algorithm that calculates and displays the absolute difference |𝑥𝑥 − 𝑦𝑦| of two inputted real numbers 𝑥𝑥
and 𝑦𝑦.

Exercise 3: Sorting numbers


1. Write an algorithm that sorts two inputted integers (𝐴𝐴 and 𝐵𝐵) in descending order.
2. Write an algorithm that sorts three inputted integers (𝐴𝐴, 𝐵𝐵 and 𝐶𝐶) in ascending order. Draw the corresponding
flowchart of your algorithm.

Exercise 4: Solving first and second order equations


1. Write the algorithm that solves, in R, a linear equation 𝑎𝑎𝑎𝑎 + 𝑏𝑏 = 0, where 𝑎𝑎 and 𝑏𝑏 are given by the user.
Draw the corresponding flowchart of your algorithm.
2. Write the algorithm that solves, in R, a quadratic equation 𝑎𝑎𝑎𝑎 2 + 𝑏𝑏𝑏𝑏 + 𝑐𝑐 = 0, where 𝑎𝑎, 𝑏𝑏, and 𝑐𝑐 are given by
the user.

Exercise 5: Evaluating a function


Consider the function 𝑓𝑓(𝑥𝑥) defined in R by:
20 𝑓𝑓𝑓𝑓𝑓𝑓 𝑥𝑥 < −5
𝑓𝑓(𝑥𝑥) = �−𝑥𝑥 + 10 𝑓𝑓𝑓𝑓𝑓𝑓 − 5 ≤ 𝑥𝑥 ≤ 5
−10 𝑓𝑓𝑓𝑓𝑓𝑓 𝑥𝑥 > 5
Write an algorithm for displaying the value of 𝑓𝑓(𝑥𝑥) at a value of 𝑥𝑥 given by the user.

Exercise 6: Type of a triangle


There are three special names given to triangles that tell how many sides are equal. Scalene (all sides are different);
Isosceles (two sides are equal); Equilateral (all three sides are equal).
Write an algorithm that takes three sides of a triangle as input and check whether the triangle is equilateral, isosceles,
or scalene. Here you are asked to give two different solutions (with and without logical operators in your decisions).

Exercise 7: Average mark and instructor note


Given four marks, which are marks obtained by a student in four different courses, the task is to calculate their
average and then assign an instructor note to that student. The instructor note to be assigned is according to the
following rules:
Average mark Instructor Note
[18,20] Excellent
[15,18[ Very good
[12,15[ Good
[10,12[ Fair
[0,10[ Poor
Write an algorithm that reads in the four marks of the student and then calculates and displays his average mark and its
corresponding instructor note.
13.5
For example if the input marks are 15, 12, 11, and 16 then the output should be:
Good
1
L01 / EE121 Algorithmic/ FALL 2022 / R. NAMANE

Optional Exercises:

Exercise 8: Grades deliberation


A student passage from first year (L1) to second year (L2) is conditioned by the following:
1. If the average of the two semesters averages is greater or equal to 10, then the student Passes.
2. Otherwise, the student has to validate a minimum of 30 credits with at least 10 credits in one semester and 20
other credits in the other semester. In this case, the student Passes with debts.
3. If neither condition 1 nor condition 2 is met, then the student Fails.
Write an algorithm that asks a student for his Avg_S1, Avg_S2, Cr_S1, and Cr_S2. The algorithm then shows if he
Passes, Passes with debts, or Fails.

Exercise 9: Amount of parking fees


A parking garage charges a 50 dinars minimum fee to park for up to three hours and an additional 20 dinars for each
extra hour. Write an algorithm that reads the number of parked hours for a given customer who parked his car in the
garage, and then calculates and prints the amount of his parking fees.

Exercise 10: Types of characters


Write an algorithm that asks the user to enter a character, then displays whether it is an uppercase or lowercase letter.
In case the inputted character is not a letter, the algorithm displays an error message.

Exercise 11: Majority voting algorithm


After finishing their exams, three friends want to decide if to go on a trip. The final decision to go or not is based on
their yes-no voting. This means that if the majority (at least two of them) votes for “yes”, the trip will be held. The trip
will be cancelled otherwise.
Write an algorithm and flowchart that reads the decision of each friend (“yes” or “no”) and then prints “Trip OK” if at
least two friends vote for “yes”. The algorithm prints “Trip Not OK” otherwise (if at least two friends vote for “no”).
Remarks:
 Assume that the input provided by each friend is either “yes” or “no”, and not other thing.
 Do not use the logical operators (AND and OR) in all your decisions.

Exercise 12: Simple calculator


Write an algorithm that allows the user to introduce three real numbers. The algorithm then asks the user to make a
choice, from a menu displayed on the screen, to calculate the sum, the product, the average, find the minimum or find
the maximum of these three numbers.
The menu should appear on the screen as follows:

---------------- MENU ---------------------


1 ---------------- Sum ----------------------
2--------------- Product --------------------
3--------------- Average --------------------
4--------------- Minimum ------------------
5--------------- Maximum ------------------
Enter your choice?

You might also like