Algorithms - Week 02
Algorithms - Week 02
Algorithms
Prof. Jose Yauri
Departament of Mathematics and Physics
National University of San Cristobal of Huamanga
1
Outline
• Pseudocode
• Flowcharts
• Exercises
2
Algorithms (review)
• An algorithm is a set of steps for solving a problem.
• Any algorithms should be:
• finite (it can't go on forever)
• unambiguous (it must say exactly, and clearly, what to
do)
• effective (it must get the job done, i.e., it must perform
the required task correctly)
3
Goal of Design of Algorithms
• When designing an algorithms, we should strive to:
• Make it easy to understand, code and debug.
• Use a structured, top-down approach (methodology).
• Find an algorithm that has already been written and solve the
problem.
• Make it to use the computer resources.
• The time of computer processor, the amount of computer storage,
memory.
4
Pseudocode
• Pseudocode allows to describe algorithms.
• A pseudocode is a sequence of short English (or Spanish) phrases to
describe the steps of an algorithm.
• The pseudocode is an informal “language” used to help programmers
develop software.
• The pseudocode is programming language independent. However, most
programmers tend to write pseudocode that reflects their language of
choice.
5
Pseudocode Statements
• To write pseudocode, there are three basic statements:
• Sequence
• Selective
• If (Si), If Else (Si Caso Contrario), Case-Type (Caja Selección de Tipos)
• Interative (repetitive)
• For (Para), While (Mientras condició n hacer), Do-Until (Hacer Hasta
condición)
6
Making Pseudocode
• The rules of Pseudocode are reasonably
straightforward. All statements showing
"dependency" are to be indented.
7
Examples
8
Examples
Compute the average of 5 grades
1. grade_counter zero
2. total zero
3. While grade_counter <= 5
4. Input the next grade
5. total total + grade
6. grade_counter
grade_counter+1
7. course_average total/5
8. Print course_average
9
Flowcharts
• Describe graphically the program flow. It is written from the top of a
page to the bottom. Each command is placed in a box of the
appropriate shape, and arrows are used to direct program flow.
10
Flowcharts
11
Flowcharts
• For a given value, Limit, what is the smallest positive integer
Number for which the sum
Sum = 1 + 2 + ... + Number
is greater than Limit. What is the value for this Sum?
Pseudocode:
Input: An integer Limit
Ouput: Two integers: Number and Sum
1. Enter Limit
2. Number 0.
3. Sum 0.
4. Repeat the following:
a. If Sum > Limit, terminate the repetition, otherwise.
b. Increment Number by one.
c. Add Number to Sum and set equal to Sum.
5. Print Number and Sum.
12
Flowcharts
13
Flowcharts
• Compute the hypotenuse of a
triangle given its cathetus.
14
Exercises: Make pseudocode to:
1. Leer un character y determinar este es un caracter o no.
2. Leer dos números, multiplicar dichos números y mostrar el resultado.
3. Leer tres números e imprimir en forma ordenada, de menor a mayor.
4. Dado 5 números, hallar e imprimir el menor y mayor de dichos
números.
5. Calcular la sumatoria de los números enteros pares comprendidos entre
el 1 y 100 (2 + 4 + 6 +…. + 100). Mientras procesa, se debe imprimir los
números pares y luego su sumatoria.
15
Exercises: Make flowcharts to:
1. Calcular la sumatoria de los números enteros impares comprendidos
entre el 1 y 200 (1 + 3 + …. + 198). Mientras procesa, se debe imprimir
los números impares y luego su sumatoria.
2. Determinar el área de un rectángulo.
3. Las entradas a un espectáculo sólo se puede adquirir hasta cuatro, como
máximo. Por la compra de dos entradas se le descuenta el 10%; por tres
entradas, el 15% y , por cuatro entradas, 20%. Asumiendo que cada
entrada cuesta 100 soles, calcule el monto de pago.
4. Calcular la media aritmética de N números. Tanto N y los números
deben ser ingresados por el usuario.
5. A partir del número de días ingresado por el usuario, convertir a años,
meses, semanas y días. Considere 1 año= 365 días, 1 mes = 30 días.
16