0% found this document useful (0 votes)
3 views28 pages

Conditional Structures and Examples

The document outlines various programming structures and examples, focusing on conditionals, loops (while and for), and variables. It includes pseudocode diagrams and practical examples for implementing algorithms, such as calculating sums, factorials, and processing user input. The document serves as a guide for understanding programming concepts and creating algorithms in a structured manner.

Translated by

ScribdTranslations
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)
3 views28 pages

Conditional Structures and Examples

The document outlines various programming structures and examples, focusing on conditionals, loops (while and for), and variables. It includes pseudocode diagrams and practical examples for implementing algorithms, such as calculating sums, factorials, and processing user input. The document serves as a guide for understanding programming concepts and creating algorithms in a structured manner.

Translated by

ScribdTranslations
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

STRUCTURES

REPETITIVE
EXAMPLES
MENU OF THE DAY
• Review
conditionals.
• Review cycles.
• Structure
while.
• Structure for.
• Problems.
• Variables.
• Miscellaneous of
examples.
CONDITIONALS

DIAGRAM OF PSEUDOCODE
FLOW
CONDITIONALS -
SCENARIOS

Praised
sea
Chuck
Norris.
CONDITIONALS -
SCENARIOS
The shape of the structure depends on the problem.
Example:
... if the respondent is of legal age, they will be asked if they are married; if not, no further questions will be asked.
has siblings, the quantity is requested and it is inquired whether he is the oldest of them; if he is
minor dismisses the user ...

...
IF (age >= 18) THEN
...
Variables: IF (state == 1) THEN
•edad: Edad. ...
•estado: (0: soltero, 1: casado) S_NO
•solo: (0: Tiene hermanos, 1: IF (only == 0) THEN
...
Only child IF (major == 1) THEN
•num_hermanos: Numero de ...
brothers. FIN_SI
•mayor: (1: He is the oldest of the ...
brothers, 0: He is not the oldest FIN_SI
of the brothers). ...
FIN_SI
NO
luck is what I tell you
FIN_SI
REVIEW CYCLES

• Employee for the performance of repetitive tasks.


• It has 2 main components, the header and the body.
• At the header is the condition that is evaluated to determine the
repetition or not of the cycle
• The body contains the instructions of the cycle that repeat.
STRUCTURE WHILE

DIAGRAM OF PSEUDOCODE
FLOW
STRUCTURE FOR
DIAGRAM OF PSEUDOCODE
FLOW
WRITE('Type the ...')
WRITE('Type 1 if...') LEA(N)
LEA(ban) FOR (i=1:N:1) DO
Write('Enter the data...')
WHILE (ban == 1) DO ...
LEA(d)
... FIN_CHANNEL
WHILE (d != -1) DO
TYPE('Enter 1 if...')
...
LEA(ban)
WRITE('Enter the data...')
FIN_PARA
LEA(d)
FINAL_FOR
total_boletos total_recolectado

VIP: $7000
Normal: $5000
EXAMPLE 1 (A simple
sum)
• Create a program that performs the
sum of the first N numbers
using the WHILE structure and
using the PARA structure.

Solution in Pseint:
•example1_while
•example1_for
EXAMPLE 2 (Factorial of a
number
• Implementing the FOR and WHILE structures to perform
an algorithm that calculates the factorial of a number, that is:
n! = n*(n-1)*(n-2)*…*3*2*1.

• Improve the previous program


de tal modo que le permita al
user repeat the process
as many times as you wish.

Solution in Pseint:
•example2_while
•example2_for
•example2_improved_sentinel.
•example2_improved_flag.
EXAMPLE 3
• In a credit institution, it is needed
to have the value in pesos updated
that have been entered for accounts
current accounts and savings accounts by
separated. For n bank clients,
make a program that allows you
know the total amount deposited in
current accounts and the amount
deposited in savings accounts.
Validate that none of the amounts
entered should be negative and if so, not
keep it in mind in the summation and
to tell it so that later it shows that
Solution in Pseint:
percentage of the total •example3
Deposits were negative.

EXAMPLE 4
Build an algorithm that allows
show the integers from 0
up to N. N must be positive, if the user
the negative input needs to be modified
(that is to make it positive)
• Modify the previous exercise to
show the integer numbers from 0 to
-N. Modify
If the the
userprevious
inputs exercise
a positiveto N, it is the
display necessary
[Link]
make it negative.
integers from N to M. N must be less than M and in case
the opposite should be exchanged.
• Modify the previous exercise so that only the are displayed
even numbers from N to M.
• Modify the previous exercise so that the numbers are displayed.
even or odd according to the user's desire from N to M. N
it must be less than M and otherwise it must be
swap them.
• Read N integers from the keyboard. It is desired to know how many of
they are exactly divisible by 7 and how many are not.
EXAMPLE 5
• Create a system to calculate the average of N grades.
input tracking from the keyboard. The grades must be
in the interval [0,5] do not take into account grades outside the range and
in the end show what percentage of the total notes was not
correct.

Solution in Pseint:
•example5
EXAMPLE 6
• Accumulate N numbers entered via the keyboard, the idea
only positive digits greater than are accumulated
zero, so that in the end it shows what percentage of
the data were not included in the displayed total.

Solution in Pseint:
•example6
EXAMPLE 7
• Build a program that allows accumulating numbers
numbers ending in 1, sum the numbers ending in 2 and count
the numbers ending in 3 that are in the interval [p,q].
Modify p and q if necessary, so that both are
positives and p is less than q.

Solution in Pseint:
•example7
EXAMPLE 8
• Build a program that generates even numbers
from N onwards, as long as the user wishes.

Solution in Pseint:
•example8

As desired here
show at least
a number of
recommend using the
REPEAT structure.
EXAMPLE 9
• An insurance office in Medellín has gathered data concerning all
the traffic accidents that occurred in the metropolitan area of Medellín in
the last year. For each driver involved in an accident, the
siguientes datos: año de nacimiento, sexo (1: Femenino, 2: Masculino),
car registration (1: Medellín, 2 Other cities). Create an algorithm that
show:
– The percentage of drivers under 25 years old.
– Percentage of female drivers.
– Percentage of male drivers aged between 12 and 30 years.
– Percentage of drivers whose cars are registered outside of Medellín.

Solution in Pseint:
•example9
EXAMPLE 10
• There is a series of records, each having 4 values.
that correspond to the lengths of different legs
from a table. Develop an algorithm that checks if
with the 4 values read in each record, it is possible to
to make a table that doesn't wobble, with four legs or, a table
do not take a 3-legged one in case it cannot be manufactured
one of 4. The algorithm must display the number of
tables with 4 and 3 legs that can be formed, the same
that the number of leftover legs.

Solution in Pseint:
•example10
EXAMPLE 11 (Series)
• Create an algorithm that shows
the first N terms of the series
Fibonacci, which has: 0, 1, 1, 2, 3,
5, 8, ...

Solution in Pseint:
•example11
EXAMPLE 12 (Series 2)
• Create an algorithm that displays
the first N terms of the
next series:
0, - 2, 4, -6, 8, -10, 12, ...

Solution in Pseint:
•example12
EXAMPLE 13 (Series 3)
• Create an algorithm that shows
the first N terms of the
next series:
0, -2, 4, -6, 8, 10, -12, 14, 16, 18, -20
22, 24, 26, 28, -30 ...

Solution in Pseint:
•example13
EXAMPLE 14 (Series 4)
• Develop an algorithm that reads a
value of x, y calculate the value of the N
first terms of the series:

Solution in Pseint:
•example14
EXAMPLE 15 (Series 4)

#Termino
1 1
2 - 8 / (2 * 1) = -4

3 32/(3*4) = 2.67
4 -128/(5*6) =
I finish result
-4.27
? ? 1=1 5 512/(7*8) =
1 -8/(2*1) = -4 1 – 4 = -3
9.14
2 32/(3*4) = 1 – 4 + 2.67 = -0.33 4.54
2.67
3 -128/(5*6) = 1 – 4 + 2.67 – 4.27 = -4.6
-4.27
4 512/(7*8) = 1 – 4 + 2.67 – 4.27 + 9.14
9.14 = 5.54
5 5 5.54
EXAMPLE 16 (Problem)
• Several ambulances
they traverse the city and when
received at the CENTRAL
a call is reported the
emergency location
through coordinates, it
same as the location of
all the ambulances. The
the center is the point (0,0) u
origin of coordinates. It
You know that N exists.
ambulances in service.
Create an algorithm that,
given the information
necessary, report the
coordinates of the
closest ambulance to the
emergency point.
EXAMPLE 17 (Series 5)
• Find the value of for a value
given exp(x) given, by means of the
series

You might also like