TD1 Algo
Table of contents
- Algorithm Ex1, Ex2, Ex3, Ex4
- Exercice 1, Exercice 2, Exercice 3, Exercice 4
- Exercice 5, Exercice 6, Exercice 7, Exercice 8
1. Algorithm Ex1, Ex2, Ex3, Ex4
1.1 Exercise 1: Algorithm Ex1 1
This exercise involves tracing the execution of a simple algorithm that performs arithmetic operations and variable assignments. The algorithm takes an integer input, calculates two new variables based on it, and then modifies the original input variable.
The algorithm's steps are:
- Prompt the user to enter a number.
- Read the user's input into variable
A. - Calculate
Bas2 * A + 10. - Calculate
CasB - A * 2. - Update
Ausing the formula(B - C) div A. - Display the final values of
A,B, andC1.
To complete the table, we need to identify the types of instructions and the values of variables at each step.
| Instruction Type | Description |
|---|---|
| Constants | None defined |
| Variables | A, B, C (Integer) |
| Read instructions | Read(A) |
| Write instructions | Write ('Please enter a number') |
| Assignment instructions | B ← 2 * A + 10 |
If the value 3 is entered for A, the execution trace is as follows:
| Step | A | B | C | Display (Output) |
|---|---|---|---|---|
| 1 | None | None | None | Please enter a number |
| 2 | 3 | None | None | None |
| 3 | 3 | 16 | None | None |
| 4 | 3 | 16 | 10 | None |
| 5 | 2 | 16 | 10 | None |
| 6 | 2 | 16 | 10 | A= 2, B= 16 and C= 10 |
1.2 Exercise 2: Algorithm Ex2
This algorithm demonstrates variable declaration with different data types, constant usage, and a mix of arithmetic, logical, and assignment operations. It also shows how boolean expressions are evaluated.
The algorithm defines a constant C and variables A (Real), B (Integer), D (Char), and M (Boolean).
- Initialize
MtoTrue. - Assign the character 'k' to
D. - Set
Bto 10. - Calculate
Aas(B - C) / 2. - Evaluate a boolean expression:
MbecomesTrueifAis less than 10 ANDBis greater than 20. - Calculate
Busing the modulo operator:(B - C) mod 2. - Assign the character '@' to
D. - Display the values of
A,B,C, andD2.
The execution trace for algorithm Ex2 is:
- Step: A, B, C, D, M, Display (Output)
- 1
- 2: k
- 3: k
- 4: k
- 5: k
- 6: k
- 7: @
- 8: @, A= 2.5, B= 1, C= 5 and D = @ 2.
1.3 Exercise 3: Algorithm Ex3 2
This exercise focuses on understanding the order of operations and how variable values change through sequential assignments and boolean evaluations within an algorithm.
The algorithm initializes integer variables A and B, and boolean variables B1 and B2.
Ais initially set to 10.Bis calculated asA + 2 * 3.B1is determined by a compound boolean condition:(A + B < 30) AND (A = 2 * B).Ais then updated by multiplying its current value by 2.B2is determined by another boolean condition:(A < 10) OR (B < 20)2.
The final values of the variables after executing algorithm Ex3 are:
| Variable | Final Value |
|---|---|
| A | 20 |
| B | 16 |
| B1 | False |
| B2 | True |
1.4 Exercise 4: Swapping Variables
This exercise explores variable assignment and its implications, specifically addressing whether a sequence of assignments correctly swaps the values of two variables.
Algorithm Ex4:
- Declares two integer variables,
XandY. - Prompts the user to enter two numbers.
- Reads the input into
XandY. - Assigns the value of
YtoX. - Assigns the value of
X(which now holds the original value ofY) toY. - Displays the final values of
XandY3.
If X is assigned 1 and Y is assigned 2:
- After
Read(X, Y),X = 1,Y = 2. - After
X ← Y,X = 2,Y = 2. - After
Y ← X,X = 2,Y = 2. The final output will beX = 2 and Y = 23.
Algorithm Ex4 does not swap the values of X and Y because the original value of X is lost when X ← Y is executed. To correctly swap the values, a temporary variable is needed.
A correct algorithm to swap X and Y is: Algorithm Swap X, Y: Integer Temp: Integer
Write ('Please enter two numbers') Read (X, Y)
Temp ← X X ← Y Y ← Temp
Write ('X = ', X, ' and Y = ', Y) End Algorithm 3.
📝 If
X = 1andY = 2initially, afterTemp ← X,Tempbecomes 1. ThenX ← YmakesXequal to 2. Finally,Y ← TempsetsYto 1, achieving the swap 3.
2. Exercice 1, Exercice 2, Exercice 3, Exercice 4
2.1 Algorithme Ex1: Tracé d'exécution
This section details the execution trace of Algorithme Ex1, focusing on variable assignments and output. The algorithm involves basic arithmetic operations and variable updates 4.
The following table outlines the execution trace for Algorithme Ex1 when the input value for A is 3 4.
| Étape | A | B | C | Affichage |
|---|---|---|---|---|
| 1 | 3 | — | — | Entrez un nombre SVP |
| 2 | 3 | — | — | — |
| 3 | 3 | 16 | — | — |
| 4 | 3 | 16 | 10 | — |
| 5 | 6 | 16 | 10 | — |
| 6 | 6 | 16 | 10 | A= 6, B= 16 et C= 10 |
2.2 Algorithme Ex2: Tracé d'exécution
This exercise presents Algorithme Ex2, which includes constant declarations, variable assignments of different data types (real, integer, character, boolean), and conditional logic 5.
The algorithm initializes several variables and performs calculations, including division and a modulo operation. A boolean variable M is updated based on a compound condition involving comparisons between variables A and B 5.
The final output displays the values of variables A, B, C, and D after the algorithm's execution 5.
2.3 Algorithme Ex3: Variable values after execution
Algorithme Ex3 involves integer and boolean variables. It performs arithmetic operations to update integer variables A and B, and then evaluates boolean expressions to determine the values of B1 and B2 6.
The algorithm proceeds as follows:
Ais initialized to 10.Bis calculated asA + 2 * 3.B1is assigned the result of a boolean expression:(A + B < 30) ET (A = 2 * B).Ais updated by multiplying its current value by 2.B2is assigned the result of another boolean expression:(A < 10) OU (B < 20)6.
The final values of A, B, B1, and B2 are determined by the sequence of these operations 6.
2.4 Algorithme Ex4: Variable swapping
Algorithme Ex4 demonstrates a common programming task: swapping the values of two integer variables, X and Y. It prompts the user to enter two numbers, reads them into X and Y, and then performs the swap 6.
The standard method for swapping two variables involves using a temporary variable. However, this algorithm appears to use a different approach 6.
The algorithm reads two numbers into X and Y. It then assigns the value of Y to X, and subsequently assigns the original value of X (which is now stored in Y) back to Y.
This effectively swaps their contents 6.
📝 If the user enters
X = 5andY = 10, after the swap,Xwill be10andYwill be5.
3. Exercice 5, Exercice 6, Exercice 7, Exercice 8
3.1 Algorithm for multiplication table by 9
This algorithm generates and displays the multiplication table for the number 9. It iterates from 1 to a specified limit, calculating and presenting each multiplication step.
- The algorithm initializes a loop counter, typically starting from 1.
- In each iteration, it multiplies the counter by 9.
- The result is then displayed in the format "9 * counter = result".
- The loop continues until a predefined upper limit is reached (e.g., 10 or 12) 7.
3.2 Algorithm to calculate the discriminant of a quadratic equation
This algorithm is designed to compute the discriminant (Δ) for a second-degree polynomial equation of the form ax2+bx+c=0. The discriminant is crucial for determining the nature of the roots of the equation.
The formula for the discriminant is: Δ=b2−4ac
- The algorithm requires the coefficients a, b, and c as input.
- It calculates Δ using the formula b2−4ac.
- The computed value of Δ is then displayed 7.
📝 For the quadratic equation 2x2+5x+3=0, where a=2, b=5, and c=3, the discriminant is calculated as Δ=52−4(2)(3)=25−24=1 7.
3.3 Algorithm to calculate a student's general average
This algorithm calculates a student's overall average grade, considering that different modules have varying coefficients (weights).
- The algorithm needs the grades for each module and their respective coefficients.
- For each module, it calculates the weighted grade by multiplying the grade by its coefficient.
- The sum of all weighted grades is then divided by the sum of all module coefficients to obtain the general average 7.
3.4 Algorithm to extract digits from a number
This algorithm takes a positive integer, less than or equal to 999, and separates it into its hundreds, tens, and units digits.
- The algorithm prompts the user to enter a positive integer up to 999.
- Hundreds digit: Calculated by integer division of the number by 100.
- Tens digit: Calculated by taking the remainder of the number divided by 100, then performing integer division by 10.
- Units digit: Calculated by taking the remainder of the number divided by 10.
- The algorithm then displays the calculated number of hundreds, tens, and units 7.
© 2026 Maeve. All rights reserved. This content is protected by copyright.