Algorithmics

Introduction to Computer Science Principles

Day 1

Understanding programs, algorithms, and fundamental programming concepts

What is a Program?

Allows to use a computer to achieve a task. The program will take input data, will transform them and produce output data, using the computer hardware:

Algorithms


It's only a set of instructions that will produce an expected result, very close to a program but... without any implementation

What is an algorithm?

It's a cooking recipe

From the previous description, we can take the cooking recipe example, in such a recipe we have:

What is an algorithm? (2)


It's a recipe that does not apply in the culinary domain!

What is an algorithm? (3)


Here is the equivalence between cooking recipes and algorithms

Cooking Recipe, Algorithm Equivalent Ingredients,input data Cooking actions,instructions Cooking utensils,Computer/language built-in functions Dish,output data

⚙ Exercise : analysis of the "Aligot" recipe


You know already plenty of algorithms

  • Installation instructions
  • Directions

What is an instruction?

It's an action to be executed by the computer. It is a step in the algorithm

Instructions kinds Examples (Login on a website)
Read/Write "Read User Credentials from the UI Form"
Logical Tests
If user is authenticated Then
    ... //execute this if true
Else
    ... //execute this if false
Endif
Loops
While number of invalid tries is less than the maximum authorised, do
... // repeat this until condition is reached
EndWhile
Variable declaration and initialization
User <- Read User from login form
Function declaration and invocation
function sayBonjour(user:String){
    display "Bonjour, " +user
}
...
If user is authenticated Then
    sayBonjour(user)
EndIf

What is an instruction?

It's an action to be executed by the computer. It is a step in the algorithm

Instructions kinds Examples (Login on a website)
Variable declaration and initialization
Define user as String
...
user <- Read User from login form
Function declaration and invocation
function sayBonjour(person:String){
    display "Bonjour, " +person
}
...
If user is authenticated Then
    sayBonjour(user)
EndIf

Pseudo code format

Why pseudo-code? For several reasons

Pseudo code format (2)

What is pseudo-code?

Pseudo code format (3)

Example


        Variables A,B as Integer
        Begin
            A <- 1
            B <- A + 3
            A <- 3
        End
    

Variables

What is a variable

Computer science is the "science" of data organization and processing,

Variables (2)

Why is the variable useful?

Variable types

What to use in pseudo-code? Mainly 4 types of variables

What is an assignment

Several examples of assignment

A <- 1
Assigns value 1 to A
A <- "B"
Assigns String "B" to A
A <- B
Assigns the value contained in B to A
A <- B + 1
Assigns the value of B, incremented by 1, B is not affected
A <- "Bonjour " + B
concatenate the String "Bonjour " with the value of B and store it in A

What is an operator?

Example of operators

Exercises, determine variable values



# Case 1
Variables A,B as Integer
Begin
    A <- 1
    B <- A+3
    A <- 3
End
            

# Case 2
Variables A,B,C as Integer
Begin
    A <- 3
    B <- 10
    C <- A + B
    B <- A + B
    A <- C
End

# Case 3
Variables A,B,C as String
Begin
    A <- "423"
    B <- "12"
    C <- A + B
End
            

# Case 4
Write an algorithm allowing to
initialize 2 variables, and
then exchange their values,
this is called "variables permutation".

Read and Write

How to identify and react to different situations in a program

It is possible to chain several tests

Loops

it is necessary to repeat operation, without duplicating code!!!, for that purpose we have 3 main categories of loop

The repetition will take place until a particular condition is reached

Loops, examples


# While loop
Variable Answer as String
Begin
    Display "do you want a coffee? (O/N)"
    While not(Answer = "Y"  or Answer = "N")
        Read Answer
    EndWhile
End

# Do-While loop
Variable Answer as String
Begin
    Display "do you want a coffee? (O/N)"
    Do
        Read Answer
    While not(Answer = "Y"  or Answer = "N")

End

# For loop
Variable DayNumber as Integer
Begin
   For DayNumber <- 1 to 15
        Display "Day number: " + DayNumber
   EndFor
End

Logigrams

demonstration with diagrams.net

⚙ Exercises

⚙ Exercises (2)

In the following exercises, the size of the triangle base is configurable

⚙ Exercises (3)

⚙ Exercises (4) - Logical Tests

⚙ Exercises (5) - More Loops

⚙ Exercises (6) - Input Validation

⚙ Exercises (7) - Combined Operations

Exercise A: Temperature Converter

Write an algorithm that:
1. Asks for temperature in Celsius
2. Converts to Fahrenheit
3. Displays the result

Formula: F = (C × 9/5) + 32

Exercise B: Simple Calculator

Write an algorithm that:
1. Asks for two numbers (A and B)
2. Asks for an operation (+, -, *, /)
3. Performs the operation
4. Displays the result

⚙ Exercises (8) - Number Patterns

⚙ Exercises (9) - Rectangle Pattern

Write an algorithm that asks for width and height, then displays a rectangle made of asterisks

Example with width=5 and height=3:
*****
*****
*****

Bonus: Make it a hollow rectangle!

*****
*   *
*****

Trieuse de diapositives