0% found this document useful (0 votes)
9 views11 pages

Reading Control (U-2)

The document outlines the general structure of a computer program, detailing the programming process from problem definition to algorithm design, coding, and maintenance. It discusses the components of a program, types of instructions, data types, and the importance of control flow structures like sequential and selective structures. Additionally, it covers the declaration of variables and constants, input/output operations, and the writing style of algorithms.
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)
9 views11 pages

Reading Control (U-2)

The document outlines the general structure of a computer program, detailing the programming process from problem definition to algorithm design, coding, and maintenance. It discusses the components of a program, types of instructions, data types, and the importance of control flow structures like sequential and selective structures. Additionally, it covers the declaration of variables and constants, input/output operations, and the writing style of algorithms.
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
You are on page 1/ 11

Name:

Chardin D. Tavera Reyes

Professor/Professoress:

Gilberto Vittini

Subject:
Introduction to Algorithm Development

Topic :
1) Reading Control (U-2)

Registration:

2023-1303

Date :
01-02-2024
CHAPTER 3 General structure of a program

3.1. CONCEPT OF PROGRAM


A computer program is a set of instructions that allows
execute a specific task. The programming process involves solving
problems and consists of several stages: definition and analysis of the problem,
algorithm design (through flowcharts, NS diagrams or
pseudocode), program coding, debugging, and verification,
documentation and maintenance.

3.2. CONSTITUENT PARTS OF A PROGRAM


The development of a program involves establishing specifications that
include input, output, and resolution algorithms. The program,
conceptually, it acts like a black box that transforms the inputs into
outputs through a set of codes. The inputs come from
devices such as the keyboard or the disk, while the outputs are presented
on peripheral devices such as screens or printers. The process of
entering data is called data input, and presenting results is called
call output data.

3.3. INSTRUCTIONS AND TYPES OF INSTRUCTIONS


The design of an algorithm or program involves defining the actions or
instructions to solve a problem. These instructions are written and
they are stored in memory in sequence. A program can be linear, with
instructions that are executed sequentially, or non-linearly, with branches
in the sequence through decision instructions.

3.3.1. Types of instructions


The instructions available in a programming language depend on the
type of language. The basic instructions are independent of the language.
The most common classification, from the previous point of view, is:

start/end instructions,
assignment instructions,
reading instructions,
writing instructions,
branching instructions.
3.3.2. Assignment Instructions
Assignment instructions assign values to variables. For example, if you
A has 80, meaning that variable A takes the value 80. In a sequence of
instructions, the value of one variable can depend on another. For example, if
A is 12 and then B takes A, meaning that B will take the value of A, that is, 12.
If the instruction N N + 5 is executed and N is 2, then N will become 7.
Variables can be thought of as boxes where values are stored, and
The assignment instructions modify the content of these boxes.

3.3.3. Data reading instructions (input)


The data reading instructions (input) allow capturing information.
from an input device like the terminal.
a) read(NUMBER, HOURS, RATE): It means to capture from the terminal the
values NUMBER, HOURS and RATE. For example, if 12325, 32 is entered,
1200 respectively, the values would be assigned to the variables as follows:
12325
b) read(A, B, C): In this case, the values A, B will be captured from the terminal
and C. For example, if you enter 100, 200, 300 respectively, the values
The variables will be assigned as follows: A = 100 B = 200 C = 300

3.3.4. Instructions for writing results (output)


These instructions write the values to an output device.
variables A, B y C, que son 100, 200 y 300 respectivamente.

3.3.5. Forking instructions


Branching instructions interrupt the linear flow of a
program, which can be forward or backward. The branches can
being conditional, depending on the outcome of an evaluation of the
condition, or unconditional, where the bifurcation occurs without the need for
fulfill no condition.

3.4. BASIC ELEMENTS OF A PROGRAM


Software programs consist of several fundamental elements.
which include reserved words, identifiers, special characters,
constants, variables, expressions, and statements. These elements are
they combine following specific rules called the syntax of the language. Furthermore,
programs can include loops, counters, accumulators, switches and
It is crucial to understand these elements and programming techniques to
design and code algorithms effectively.

3.5. DATA, DATA TYPES, AND PRIMITIVE OPERATIONS


Data is the basis of any computer and can come in various types.
The programs operate on this data, transforming it from input to
output. It is crucial to design both the data structure and the algorithm. There are
two main types of data: simple and composite. Simple data
include numeric, logical, and character types, while the compound ones
they are simple data sets with defined relationships between them. The
data is represented internally in the computer as sequences of
bits, and high-level languages allow to abstract this representation.

3.5.1. Numerical data


The numeric type includes integers and reals. Integers are numbers
complete, without decimals, and can be positive or negative. The reals
they have decimals and can be positive or negative. They are used for
represent different types of data in programming, with different ranges
and precisions depending on the type of data. Real numbers can
represent themselves in scientific notation to handle very large values or
very small.

3.5.2. Logical data (boolean)


The logical type, also called boolean, can only be true or
false. It is used to represent conditions like yes or no. C++ and
Java accepts this type of data.

3.5.3. Character type and string type data


Character types are finite and ordered sets recognized by
the computers. Character-type data contains a single character and
they can include letters, numbers, and special characters such as +, -, *, /, among
others. Strings are sequences delimited by quotes and
its length is the number of characters between the delimiters. The examples
incluyen 'Hola Mortimer', '12 de octubre de 1492', 'Sr. McKoy'.
3.6. CONSTANTS AND VARIABLES
Constants are values that do not change during execution of the
program, while the variables can change. The constants
they can be of different types, such as real, character, and logical. The
variables have a name and a type that describe their use. The names of
the variables must be significant and related to the object that
they represent. Some languages allow assigning names to constants
typical ones, such as π and e.

3.6.1. Declaration of constants and variables


The declaration of variables and constants in programs requires assigning them
a name and, optionally, an initial value. For variables, the is used
syntax <data_type> <variable_name> [= <expression>].
For constants, use const <data_type> <constant_name> =
<expression>. Examples include:

- Variables: car letter, abbreviation; int numStudents = 25; real salary =


23,000.
- Constants: const double PI = 3.141592; const string name = 'Mackoy'; const
c

3.7. EXPRESSIONS
Expressions are combinations of elements such as constants,
variables and mathematical operators, which are used to perform calculations.
They can be arithmetic, relational, logical or character types, and the type of
the result they produce depends on the elements that compose them.

3.7.1. Arithmetic expressions


Arithmetic expressions are similar to mathematical formulas and
they involve variables and numerical constants, along with arithmetic operations
such as addition, subtraction, multiplication, division, exponentiation, integer division and
module. The operators are used similarly to mathematics, with
some languages that do not support all operators. For example, in
In FORTRAN, the div and mod operators do not exist.
The calculations involve real and integer data types, normally giving
results of the same type if the operands are also. The operators of
increment (++) and decrement (--) increase or decrease the value of a
variable in a unit, depending on whether they are used as prefixes or suffixes.

3.7.2. Priority Rules


The rules of priority in mathematical expressions establish the order of
the operations. Operations inside parentheses are evaluated first,
followed by multiplications and divisions, and finally additions and subtractions. In
in case of equal priority, the order is followed from left to right. For
example, in the expression "12 + 3 * 7 + 5 * 4", multiplication is resolved first.
multiplications, then the sums, resulting in 53.

3.7.3. Logical (boolean) expressions


Logical expressions, also known as Boolean expressions, are those
whose value is always true or false. These expressions are formed
combining logical constants, logical variables, and other logical expressions
through logical operators such as 'not', 'and' and 'or', as well as operators
relational such as "<", ">", "=" and others. These expressions are fundamental
to express conditions in algorithms and takes its name in honor of the
British mathematician George Boole, who developed the Algebra of logic
Boolean.

3.8. INTERNAL FUNCTIONS


Internal functions are special operations built into
programs, such as ln for natural logarithm or sqrt for square root. It
They can take real or integer arguments and produce specific results.
For example, sqrt(25) gives 5 and round(6.5) gives 7. These functions are useful in
various tasks, such as solving quadratic equations, although one must have
be careful with negative values to avoid errors.

3.9. THE ASSIGNMENT OPERATION


The assignment operation is fundamental for storing values in
variables. It is represented by the symbol or in different languages of
programming. It is used to assign a value to a variable, which
replace any previous value in that variable. The computer performs the
assignment in two steps: first calculate the value of the expression on the right
from the operator and then stores it in the variable on the left of the operator.
Assignments can be arithmetic, logical, or character-based, and allow
actions such as increasing the value of a variable.

3.9.1. Arithmetic assignment


Arithmetic assignments involve mathematical expressions to
assign values to variables. For example, AMN takes the value of the sum of 3,
14 and 8. QUOTIENT is assigned with the result of the division of the sum of
14.5 and 8 divided by the product of 0.75 and 3.4. The variables can be changed.
within an assignment, as in the case of A taking the value of N plus
1.

3.9.2. Asignación lógica , de cadenas de caracteres ,Asignación múltiple


In programming, assignment can be logical or string type.
characters. In logical assignment, variables take true or
false according to the evaluation of logical expressions. In string assignment,
a string of characters is assigned to a variable. There are also
multiple assignments, where arithmetic operators can be combined
with the assignment operator, such as +=, -=, *=, /=, and %=, to perform
multiple assignments in a single statement.

3.9.5. Type conversion


Examples of type conversion and value assignment are presented to
variables. In the first example, values are assigned to the variables A, B, and C.
then mathematical operations are performed on them, obtaining new
values. In the second example, a sequence of operations is shown
that affect the value of the variable x.

3.10. INPUT AND OUTPUT OF INFORMATION


The input and output of information in computers is crucial for achieving
calculations. Input, through devices like keyboards, assigns values to
variables. The output, on devices such as screens or printers, shows
results. In algorithms, reading and writing are represented by "read" and
write
and C, while "write('hello Vargas')" displays the message on the screen.
3.11. WRITING OF ALGORITHMS/PROGRAMS
The writing of algorithms or programs requires clarity and structure to
facilitate their understanding and coding. We use languages similar to the
programs, such as pseudocode, based on programming logic
structured like Pascal. An algorithm consists of a header that starts with
the word 'algorithm' followed by the name of the program, and a block that
includes variable and constant declarations, as well as actions
executables.

3.11.1. Header of the program or algorithm, 3.11.2. Declaration of


variables, 3.11.3. Declaration of numeric constants.
All algorithms and programs must begin with a header in the
that the identifier or corresponding name is expressed with the word
reserved that indicates the language. In programming languages,
The reserved word is usually program. In algorithmics, it is called an algorithm.

The variable declaration section in a list of algorithms and describes


all the variables used, specifying their names and data types.
Start with the reserved word 'var' followed by the declarations of
It is advisable to use meaningful names for the variables and
add brief comments that indicate their use.

In this section, all named constants are declared. Its


the format is
const
pi = 3.141592 tamaño = 43
horas = 6.50
The values of these constants can no longer vary during the course of
algorithm.

3.11.4. Declaration of constants and character variables


Constants and character variables can be declared in a program. The
simple character constants and strings can be declared in the section
program constant. Character variables can store a
single character or multiple characters, depending on the language of
programming used. In some languages like Pascal, you can use
specific data types like 'string' to declare string variables.
3.11.5. Comments
The documentation of a program is essential for its maintenance and
update. It can be internal or external. The internal documentation is
achieved through comments in the source code. Different languages of
Programming has specific notations for comments.
In Visual Basic, single quotes or the reserved word are used.
"Rem" for comments.
In C/C++, single-line comments start with '//'
multiline between "/" and "/".
In Java, single-line comments start with "//", and multi-line comments
between "/ " and " /", with class documentation between "/** */".
In Pascal, comments are enclosed between "(* *)".
In Modula-2, comments are closed between "(* *)".

3.11.6. Writing style of algorithms


The writing style of algorithms and programs follows a defined structure,
starting with a header that includes the identifier and the
variable and constant declarations. Then follows the body of the algorithm,
where the different sentences are listed.
CHAPTER 4
Control Flow I: Selective Structures

4.1. THE CONTROL FLOW OF A PROGRAM


In programming, the flow of control refers to the order in which statements are executed.
statements of the program, which can be sequential, selection or
repetition. The selections of structures, such as 'if' and 'according-to', choose what
sentences execute, while repetition structures, like 'from'
and "while", they repeat a set of statements. The sequential execution continues
the order of the source code, but the branches allow modifying this flow
according to specific conditions, selecting among different paths of
execution.

4.2. SEQUENTIAL STRUCTURE


In a sequential structure, actions or instructions are executed in
sequence, one after another, where the output of one action is the input of the
next. This structure has a clearly defined input and output.
It is graphically represented by flowcharts and pseudocode.
example, in the calculation of the sum and the product of two numbers, the
operations are carried out sequentially, first the numbers are read, then
the sum and the product are calculated, and finally the results are shown.

4.3. SELECTIVE STRUCTURES


Selective structures, also known as decision structures,
are used in algorithms when different paths need to be taken
based on the evaluation of a condition. They are represented with words
like 'if', 'then', 'else' or through geometric shapes. They can be simple,
doubles or multiples, depending on the number of alternatives available
present.

4.4. SIMPLE ALTERNATIVE (IF-THEN)


The simple conditional structure if-then executes an action if a
the condition is true and does nothing if it is false. It is represented graphically
with a flowchart and is written in pseudocode both in Spanish and
In English. The pseudocode shows how the actions are structured within
from the condition, with a start and an end if it is a composite action.
4.4.1. Double alternative (if-then-else)
The structure of double alternative, also known as 'if-then-'
"sino" or "if-then-else" allows you to choose between two options based on a
condition. If the condition is true, action S1 is executed; if it is false,
the action S2 is executed. It is represented in both flowcharts and
pseudocode. In pseudocode, actions are indented for clarity
the readability.

4.5. MULTIPLE CHOICE (as applicable, case of/case)


The multiple decision structure allows choosing among various alternatives
according to the value of an expression. It is represented by the statement
switch in languages such as C, C++, Java, and C#. This structure avoids the
complexity of nested structures when evaluating an expression that can
take multiple values. It can be expressed in several ways in
pseudocode, providing flexibility in the implementation of actions
simple or compound.

4.6. NESTED DECISION STRUCTURES


Nested decision structures, also known as in
ladder, allow designing selections that contain more than two
alternatives. This is achieved by incorporating `if` structures
within other structures 'if'. These structures can be complex and
require indentation to maintain the clarity of the algorithm. They can
build multiple decision structures using the format 'if
<condition> then `with` if_not` for each alternative. The writing of
These structures can vary depending on the programming language used.
For example, an algorithm can calculate the largest number among three
numbers or even solve quadratic equations.

4.7. THE SENTENCE ir-a (goto)


The use of the goto statement in programming has been controversial.
Although present in some languages, such as C#, its use is
not recommended due to its tendency to produce unclear code and
prone to errors. Although it can be useful in specific situations, its
is generally considered a bad practice. Jump statements,
break, continue, return, throw, also allow altering the flow of
control of a program, but they are used in a more controlled manner and
structured.

You might also like