# Algorithms – Chapter 1

Ekluboko, K. (2018) Algorithme (Course material for Programmation : s’initier)

Instead of using a programming language to express an algorithm you can use natural language first (In this case English) and then as a second step use a programming language. But, before starting to study the operations or functions that constitute code perhaps we should recall the definition of an object in general.

According to the Miriam Webster Dictionary an object is,

• something material that may be perceived by the senses
• something mental or physical toward which thought, feeling, or action is directed
• the goal or end of an effort or activity
• a thing that forms an element of or constitutes the subject matter of an investigation or science
• a noun or noun equivalent (such as a pronoun, gerund, or clause) denoting the goal or result of the action of a verb
• a data structure in object-oriented programming that can contain functions as well as constants, variables, and other data structures

## Variables

In computing, any object is designated by a variable.

• A variable is used to store a value.
• A variable is characterized by its name and type.

To declare that we will use one or more variables, we can write in pseudo language:
Variable_name1 : type_of_variable1
Variable_name2 : type_of_variable2

For example;
First name: chain of characters
Age: Integers
Size: actual number

## Type

A type is an abstract model that brings together to a high degree the essential traits of all beings or of all objects of the same nature.

For example; Harpagon is a stingy guy. This would place Harpagon in type stingy which is a characteristic of a group or a family of things. Other examples of types might be strings, integers, a character, a real number, a boolean, etc…

## Constant

A constant is a variable whose value, fixed at the time of its definition, is not modifiable.

Some examples of constants might be;
const Pi := 3.141592654
const e := 2.718282

## Comment

A comment in programming starts with a /* and ends with a */. For example, /* This is a comment */. They are important because they made the program more readable.

## Assignment Operations

Assignment operation assign a value to something. For instance,

Variable
A: Integer /* Here the value of A is unknown */
B: Integer /* Here the value of A is unknown */
A: = 1 /* A takes the value of 1 */
B: = 3 /* B takes the value of 3 */
A: = B + 2 /* A takes the value of B plus 2. In this case 5*/
B: = B + 7 /* B takes the value 10 */

Example of the program in pseudo language:

Variable
/* the declarations */
Puht: actual number /* unit price excluding tax */
Ptht: actual number /* total price excluding tax */
Q: integer /*quantity ordered */

/* Start of execution */
START
WRITE “Give the unit price excluding tax”
WRITE “Give the quantity”
Ptht: = Puht * Q
WRITE “Amount excluding taxes:” ,Ptht
END
/* End of executions */

## Exercise 1

What does the algorithm below do?

Variables
A : number;
B : number;
START
A := 4 ;
B := 11;
A := B-A ;
B := B-A ;
A := A+B ;
END

START
A: = 4;
B: = 11;
A: = 7;
B: = 4;
A: = 11;
END

## Exercise 2

Write another algorithm (using the assignment) that exchanges the values of two variables of any kind.

Variables
A: = char
B: = char
C: = char
START
C: = A;
A: = B;
B: = C;
END

## Exercise 3

Perimeter calculation. Write a program that asks for the length and width of a figure (rectangle or square) and then calculates and displays the perimeter of the rectangle or square. It also displays: it is a rectangle or a square. Add the calculation of the surface area.

``````import java.util.Scanner;

public class Application {

public static void main(String[] args) {

System.out.print("What is the Length: ");
System.out.print("What is the Width: ");

System.out.println("The perimeter = " + ((length + width) * 2));

if (length == width){
System.out.println("It is a square.");
} else {
System.out.println("It is a rectangle.");
}

System.out.println("The surface area is: " + (length * width));

}
}``````

## Exercise 4

Calculation of current. Use the formula V=IR. That is to say, voltage = resistance times current. You have a voltage of 220V or 240V. (To be chosen). Write a program that asks for the resistance of your new device, then displays the current flowing through it. Attention, for devices with a theoretical resistance equivalent to zero (0), it is necessary to display “no consumption”.

Variables
V: = Int
I : = Int
R: = Int
START
V: = 240
WRITE “Give resistance”
IF (R != 0){
WRITE “The current is” V/R;
} else {
WRITE “no consumption”;
}

## Exercise 5

Write an algorithm that asks for the pre-tax price and quantity of an item and then displays an invoice (VAT rate: 19.60%).

Variables
PTP: = Int
Q : = Int
VAT: = Num
START
VAT: = 1.1960
WRITE “What is the pre-tax price?”