Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views8 pages

TD SWE Advanced DataStructure1

The document is a tutorial on advanced data structures, covering various exercises related to algorithms, conditionals, iterations, and conversions. It includes tasks such as variable assignments, algorithm displays, and user input handling, as well as exercises on nested loops and functions. Additionally, it addresses array manipulations and provides a menu-driven program for managing a list of names.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

TD SWE Advanced DataStructure1

The document is a tutorial on advanced data structures, covering various exercises related to algorithms, conditionals, iterations, and conversions. It includes tasks such as variable assignments, algorithm displays, and user input handling, as well as exercises on nested loops and functions. Additionally, it addresses array manipulations and provides a menu-driven program for managing a list of names.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Tutorial No 1 Advanced Data Structure

Exercise 1 – Assignments

1. Consider the algorithms below.

(a) What will be the contents of variables a, b and possibly c after their execution?

(b) In each of the above cases, are there any unnecessary lines, and if so which ones?

1 // algo 1.1
1 // algo 1.2 1 // algo 1.3
2 var a, b, c : integer
2 var a, b : integer 2 var a, b : integer
3 begin
3 begin 3 begin
4a←1
4a←1 4a←2
5b←a+2
5b←a+2 5b←a+2
6c←b-3
6a←3 6a←a+2
7 end
7 end 7b←a-2

8 end
1 // algo 1.5
1 // algo 1.4
2 var a, b, c : Integer 1 // algo 1.6
2 var a, b, c : integer
3 debut 2 var a, b : character
3 begin
4a←1 3 begin
4a←2
5b←2 4 a ← ’1’
5b←4
6b←a+b 5 b ← ’2’
6c←a+b
7c←a+b 6a←a+b
7a←1
8 fin 7 end
8c←b-a

9 end

2. Consider the algorithm opposite

(a) Does it allow the values of the values of variables a and b to be swapped?

(b) Propose solutions to permute the values of 2 variables digital? Does each of the proposed
solutions work in the case of non-numeric variables?

Dr TABUEU FOTSO Laurent Cabrel – IUG


(c) Given 3 variables a, b and c, propose an algorithm for permute them circularly, transferring the
initial values of a to b, from b to c, and from c to a.

1 var a, b : integer

2 begin

3a←1

4b←2

5a←b

6b←a

7 end

Exercise 2

1. What are the displays caused by the algorithms?


1 // algorithm 2.1 // algorithm 2.2

2 var a, b: real 2 var a, b: integer

3 var c: integer 3 var c: real

4 start 4 start

5 a ← 4.21 5a←5

6b←a*2 6b←2

7 write a, b 7 write a, b

8 write b * 2 8 write a / b

9c←b*2 9c←a/b

10 write c 1 // algorithm 2.1 10 write c

2 var a, b: real 11 end

2. Write an algorithm that asks the user for an integer, then displays its square.

Exercise 3 – Conditionals

1. Write an algorithm that asks the user for an integer, tests whether this number is positive (≥ 0) or
not, and displays “positive” or “negative”.

2. Write an algorithm that asks the user for an integer, tests if this number is strictly positive, zero or
strict negative, and displays this result.

3. Write an algorithm that asks for a real value from the user and displays its absolute value (without
using a function predefined obviously).

Dr TABUEU FOTSO Laurent Cabrel – IUG


4. Write an algorithm that asks for a real number from the user and rounds it to the nearest integer
(the x.5 will be rounded to the higher integer).

5. Write an algorithm that asks for the number of a month and displays the number of days in that
month (without taking counting leap years).

6. Write an algorithm that checks if a year is a leap year. Remember that there are leap years every

4 years, but the first year of a century is not (1800, 1900 were not leap years) except every 400 years

(2000 was a leap year).

7. Write an algorithm that requests a date in the form of 2 integers (day number and month number)
and displays the season (eg: 12/02; winter). It will be assumed that the first day of the season is
always the 21st.

8. Write a program that asks for the coordinates (x, y) of the vertices A, B and C of a triangle and
displays the nature of the triangle (isosceles, equilateral, rectangular or any).

Exercise 4 – Iterations

1. Write an algorithm that asks for a positive integer, and rejects it until the number entered
does not match.
2. Write an algorithm that asks for 10 integers, counts the number of positive integers entered,
and displays this result
3. Write an algorithm that asks the user for positive integers, adds them, and stops displaying
the result as soon as a negative integer is entered.
4. Modify this last algorithm to display the average of the series of positive integers entered.

Exercise 5 – Conversion to binary

1. Write an algorithm for converting an integer to binary.

2. (Optional) Write an algorithm for converting an integer to any base b.

Exercise 6 – Continuations

1. Write an algorithm to display the first n terms of the following sequences (n requested from
the user):
(a) Arithmetic sequence (b) Newton's sequence
(real a requested from the user)

Dr TABUEU FOTSO Laurent Cabrel – IUG


2. (Optional) Newton's sequence converges to the square root of a. Modify algorithm (b) to
calculate the square root of a number to a given precision.

Exercise 7 – Guess a number


1. Write an algorithm allowing you to play the smallest-largest game. We draw a number at
random to make the player guess it by telling them each turn whether the proposed
number is larger or smaller than the number to be searched for. When the player has
found, the algorithm ends by displaying the number of rounds.
Note: We assume that we have an integer functionRandomnumber(var max: integer) which
draws a number at random and returns it.
2. Then modify this algorithm to limit the number of player proposals to 10, and display
“Lost!” if the player has not found it.

Exercise 8 – Nested loops

1. Chessboards

(a) Write an algorithm to write a square of 8 times 8 characters 'x'.

(b) Write an algorithm to write a chessboard. We will represent the black boxes by 'x' and the white
boxes by spaces.

(c) Modify the previous algorithm to display a frame around the chessboard, using the characters '|',
'−' and '+'.

(d) Modify this algorithm again to display all the boxes with these same characters (see example 4
below – incomplete)

Dr TABUEU FOTSO Laurent Cabrel – IUG


2. Write an algorithm allowing you to write a multiplication table like the one shown opposite. At
first we will not worry about the number of spaces between the numbers, then we will refine it
taking this into account.

Dr TABUEU FOTSO Laurent Cabrel – IUG


Tutorial No 2 Advanced Data Structure (Procedures and
functions)

Exercise 1: Write a function that lets you know if an integer is divisible by


another. We can use a new type called logic to return the result
Exercise 2: Create a small set of procedures and functions to easily
manipulate hours and minutes and composed of:
1- The Minutes function, which calculates the number of minutes
corresponding to a given number of hours and a given number of minutes.
function Minutes(H: integer, M: integer): integer
2- The HoursMinutes function or procedure which performs the inverse
transformation of the Minute function. For HoursMinutes, there are two
results to provide, a function cannot be suitable, you must therefore write
a procedure comprising three parameters: The duration (input), the hour
(output) and the minutes (output).
Procedure HoursMinutes (Duration: integer, var H: integer, var M: integer)
3- The AddTime procedure which adds two pairs of hour and minute data
using the two previous functions. The AddTime procedure receives four
parameters as input, provides two parameters as exit. The MinuteEnTout
local variable is used to store an intermediate result, but it is not essential.

Exercise 3: This exercise allows you to complete the procedures and


functions of the previous exercise
1- Create a function that allows you to say whether a month has 30 days
or not. This function will return 1 if so and 0 otherwise.
Function IsAMonthOfThirtyDays (month: integer): boolean

2- Create a function that allows you to say whether a month has 31 days
or not. This function will return 1 if so and 0 otherwise.

3- Create a function that allows you to say whether a year is a leap year or
not. This function will return 1 if so and 0 otherwise. For a year to be a
leap year, it is enough for the year to be a number divisible by 4 and not
divisible by 100, or for it to be divisible by 400.

Dr TABUEU FOTSO Laurent Cabrel – IUG


4- Use these functions to write a function returning the number of days for
a given month and year.

Function Number_of_days (month: integer, year: integer): integer

5- Write a main program allowing the user to enter a month number


(between 1 and 12) and a year (between 1582 and 2003), which will then
be passed as parameters to the Number_of_days() function. It is necessary
to test the validity of the months and years.

Exercise 4: We want to manage an array containing a list of names. To do


this, we decide to repeat the display of a menu and the execution of the
command chosen by the user.

The menu will look like this:


Please hit:
+ the letter V to see the list
+ The letter S to remove a name from the list
+ The letter A to add a name to the list
+ The letter to search if a name is in the list
+ The letter T to end.
This management requires first that the same name does not appear twice
in the list, then that a new name is added, at the beginning of the list, at
the end of the list, or after another name, depending on the choice of the
user. We could write a “big” monolithic algorithm, but for the main
program to be as simple and therefore as clear as possible, we will make
calls of
procedures. The separate writings of these procedures will highlight the
interest of new procedures and functions which will provide them with
identical services. We will not detail the algorithms of the various
procedures until the program is completely
organized.

Dr TABUEU FOTSO Laurent Cabrel – IUG


Exercise 1

Write the algorithms allowing:

1. Calculating the number of occurrences of a given element in a table

2. Calculating the average and minimum of the elements of an array.

3. Calculation of the scalar product of two real vectors u and v of dimension n: u.v

Exercise 2

Write the algorithm performing the shift of the elements of an array.

Example :

• Initial C A L A G E table

• Modified table (left shift) C A L A G E D

Exercise 3

Write the algorithm that calculates the product of two real square matrices A = (aij ) and B = (bij ) of
dimension n

Exercise 4

Consider an array T with T (i) ∈ {0, 1}. Write an algorithm that returns position i in the array such that
T[i] is the start of the longest consecutive sequence of zeros

Exercise 5 Write an algorithm that calculates the largest deviation in an array (the deviation is the
absolute value of the difference of two elements)

Dr TABUEU FOTSO Laurent Cabrel – IUG

You might also like