Comprehensive Study Notes on Algorithm Fundamentals and Computer Problem Solving
Stages for Computer Problem Solving
The process of solving problems via computer begins with specific technical stages to ensure accuracy and functionality. Compilation and execution represent the critical phase where the source code is reviewed to ensure it is free of errors before being processed by the machine. The primary purpose of compilation is to perform a thorough review so the program can be executed successfully afterward. Documenting these stages is essential for the final production; programs that are poorly documented are consistently more difficult to read, troubleshoot (debug), and are nearly impossible to maintain or modify in the future.
Classification and Identification of Errors
Errors in programming are classified based on the effects they produce and the specific moment they occur during the development cycle. They are broadly categorized into those that impede logic or execution and those that do not.
Syntax Errors: These occur when the code violates the grammatical rules of the programming language. They prevent the execution of the program entirely because the compiler cannot translate the source code into machine language.
Logical Errors: These occur when the program runs but produces an incorrect result or an unintended behavior, such as an infinite loop or a logically incoherent answer. For example, if a program is meant to sum two numbers but subtracts them instead, it is a logical error.
Runtime/Execution Errors: These occur while the program is running. Examples include invalid processes, such as attempting to divide by zero.
Managed Errors: These are specific types of errors that the programmer anticipates and handles within the code to prevent crashes.
Compilation Errors: These are errors identified by the compiler during the translation process from high-level code to executable code.
Desktop Testing (Pruebas de Escritorio)
Desktop testing refers to simulations of the behavior of an algorithm implemented to determine its validity. This is a manual dry run performed by the programmer. There are two primary forms demonstrated:
Form 1 Example: Instructions: Leer , ; ; Escribir . Data Input: . Process: . Output: .
Form 2 Example (Trace Table): In this form, a table tracks variable states. For inputs and , the column for the variable "suma" is updated to .
Algorithm Example for Age Categorization: \begin{itemize} \item Define "edad" as integer. \item Write "Ingrese su edad". \item Read "edad". \item If () then write "Infante". \item Else if () then write "Joven". \item Else write "Adulto". \end{itemize}
Flowcharts and Graphical Representation
A flowchart is a tool used to graphically represent the sequence of instructions in an algorithm. Algorithms can be composed of operations, logical decisions, and repetitive cycles. Flowcharts must adhere to four essential principles: Simplicity, Clarity, Standards, and Flexibility. Standard symbols include:
Oval: Indicates the Beginning or End of the program.
Rhombus (Diamond): Represents a Decision where logical paths diverge.
Rectangle: Indicates a Process or operation.
Slanted Rectangle: Represents General Input.
Hexagon: Indicates Iteration (loops).
Keyboard Icon: Represents Keyboard Input.
Monitor Icon: Represents Screen Output.
Document Icon: Indicates Printed Output.
Small Circle: Acts as a Connector.
Square with double vertical lines: Represents a call to a Subroutine.
Arrows: Represent the Flow of the algorithm.
Example Algorithm (Triangle Area):
INICIO
Input BASE
Input ALTURA
Salida AREA
FIN
Basic Programming Elements: Identifiers and Variables
Identifiers are sets of alphanumeric characters of any length used to identify program entities. They can combine letters and numbers. Each language has specific rules for construction, but one universal rule is that no two identifiers can be the same; elements within an algorithm cannot share the same name. Reserved words are specific keywords in the syntax of a programming language that cannot be used as identifiers by the developer.
Variables are reserved spaces in the computer's RAM (Random Access Memory) used to store data. The value in a variable can change during algorithm steps; it always holds the last assigned data. Constraints for naming variables includes:
Must begin with a letter.
Can only contain letters or numbers.
While they can exceed 8 characters, often only the first 8 are used for identification.
The underscore character (_) can be treated as a letter.
Reserved words and the letter "ñ" are prohibited.
Variable writing styles include: lowerCamelCase, UpperCamelCase, and snake_case.
Constants and Data Types
A constant is a value that does not change at any point in the algorithm once assigned. Types include:
Integer Constant: Whole numbers stored in assigned memory.
Character Constant: A single character written inside simple quotes (e.g., 'a').
String Constant: A sequence of characters written inside double quotes (e.g., "text").
Real/Floating Point Constant: Numbers written in decimal or scientific notation.
Boolean Constant: Can only take states 0 or 1 (True/False).
General data types are classified as Simple (indivisible) or Compound (structured/grouped). Common examples include int, float, and char.
Operators and Priority Rules
Operators are symbols representing actions on numbers or elements (operands). They allow the manipulation of values.
Arithmetic Operators (Order of operation):
Parentheses: Expressions within parentheses are evaluated first (innermost to outermost).
Exponenciación: ^
Multiplication, Division, Modulo, and Integer Division: , ,
mod,div.Addition and Subtraction: , . Operators of the same priority are evaluated from left to right.
Relational Operators: Used to establish relationships between two values of the same type. These produce a logical result. Symbols include:
(Less than)
(Less than or equal)
(Greater than)
(Greater than or equal)
(Equal to)
(Different/Not equal) Relational operators have lower priority than arithmetic operators.
Logical Operators: Used for basic operations: NOT (Negation), AND (Conjunction), and OR (Disjunction). The evaluation order is NOT, then AND, then OR.
Mathematical and Logical Examples
Arithmetic resolution examples:
Logical resolution examples where :
((a > b) or (a < c)) and ((a == c) or (a >= b))evaluates to(F or V) and (F or F) \rightarrow V and F = F.not (a == c) and (c > b)evaluates tonot(F) and V \rightarrow V and V = V.
Instructions, Assignments, and I/O
Assignments: Used to give or change a variable's value using the operator or . Format: Variable Name = expression or value.
Expressions: Combinations of constants, variables, operators, parentheses, and functions, similar to traditional mathematical notation (e.g., or ).
Control Variables:
Counter: Used to track how many times an operation occurs or a condition is met. Increments are usually by (e.g., ). They are exclusively of the integer type.
Accumulator: Used to carry the cumulative sum of values calculated progressively (e.g., ).
Input and Output:
Input: The
leer()instruction captures values interactively from the keyboard. The data type must match the variable. Multiple values can be read simultaneously, such asleer(x, y), where inputs like and assign and .Output: The
escribir()instruction sends values or expression results to the standard output device (screen). Strings are enclosed in double quotes (e.g.,Escribir "Introducir el valor de la base: "; Escribir x;).