Introduction to Computer Programming and Algorithm Design

Fundamentals of Computer Programming

  • Programming is defined as the process of designing or creating a computer program to solve computational problems.
  • A programmer is an individual who writes, tests, debugs, and maintains computer programs.
  • A program is defined as a set of step-by-step instructions that directs a computer to perform a specific task and to produce the required results.
  • Programming languages serve as the primary medium to construct software applications, specifically utilized for:
    • Writing games and interactive software.
    • Writing application programs for general-purpose computer systems.

Classification of Programming Languages

Low level language vs High level language

  • Programming languages are divided into two main categories based on their level of abstraction from hardware: Low-Level Languages and High-Level Languages.

  • Low-Level Languages:

    • Characteristics:
    • Machine-dependent: Tied directly to specific computer hardware architectures.
    • Not portable: Programs written for one computer architecture cannot run on a different architecture without substantial modification.
    • Divided into two primary types: Machine Language and Assembly Language.
  • Machine Language:

    • Represents the FIRST generation of programming languages (1GL1\text{GL}).
    • It is the ONLY language that a computer directly recognizes and executes without translation.
    • Uses a series of binary digits (1s1\text{s} and 0s0\text{s}) or hexadecimal/alphanumeric combinations that represent binary machine instructions.
    • Disadvantages:
    • Instructions are extremely cryptic and difficult to learn or read.
    • Programs are exceptionally lengthy for basic operational routines.
    • Machine-dependent.
    • Not portable.
  • Assembly Language:

    • Represents the SECOND generation of programming languages (2GL2\text{GL}).
    • Uses symbolic instruction codes (meaningful abbreviations or mnemonics) to replace raw binary numbers:
    • A→additionA \rightarrow \text{addition}
    • C→compareC \rightarrow \text{compare}
    • L→loadL \rightarrow \text{load}
    • M→multiplyM \rightarrow \text{multiply}
    • Disadvantages:
    • Difficult to learn.
    • Programmers must use an assembler to convert assembly language to machine language.
    • Programs are lengthy.
    • Machine-dependent.
    • Not portable.
    • Example of IBM PC Assembly Language Code:

IBM PC Assembly language example

  • Detailed Explanation of Assembly Language Example:
    • ; Example of IBM PC assembly language
    • ; Accepts a number in register AX;
    • ; subtracts 32 if it is in the range 97-122;
    • ; otherwise leaves it unchanged.
    • SUB32 PROC : Procedure declaration starts here.
    • CMP AX, 97 : Compares the value in register AX to 9797.
    • JL DONE : If the value in AX is less than 9797, jumps to the DONE label.
    • CMP AX, 122 : Compares the value in register AX to 122$.\n - `JG DONE` : If the value in `AX` is greater than 122, jumps to the `DONE` label.\n - `SUB AX, 32` : Subtracts 32 from register `AX` (converts lowercase ASCII to uppercase ASCII).\n - `DONE: RET` : Returns execution to the main program.\n - `SUB32 ENDP` : Marks the end of procedure `SUB32`.\n\n- High-Level Languages:\n - Instructions are English-like and significantly easier to learn than machine or assembly language.\n - High abstraction level: A single high-level instruction corresponds to many individual operations at the machine level.\n - Conversion Requirement: Must be converted to machine language before execution.\n - Portability: Highly portable across different machine hardware architectures.\n\n# Programming Paradigms\n\n![Computer language paradigms](https://assets.knowt.com/pdf-flow-prod/04a3134e-1a13-4f25-b295-ce88ab895cdc-figures/6.jpg)\n\n- Computer language paradigms represent distinct approaches to structuring code and organizing computer operations:\n - Structured Paradigm: Focuses on procedural logic, block structures, and modular design.\n - Object-Oriented Paradigm: Groups data and procedures into cohesive objects that interact with one another.\n - Functional Paradigm: Emphasizes computation through pure mathematical functions and immutable data.\n - Declarative Paradigm: Expresses computational logic without explicitly describing control flow steps.\n\n- Representative Programming Languages across Paradigms:\n - Procedural / Structured: FORTRAN, COBOL, BASIC, C, Pascal, Ada.\n - Object-Oriented: Smalltalk, C++, Visual Basic, Java, C#, F#.\n - Functional / Logic: LISP, Scheme, Prolog.\n\n# Language Translators\n\n- Translator Program: The program that translates the source program into the object program.\n- Source Program: Program written in a high-level programming language or assembly language.\n- Object Program: The source program after it has been translated into machine language.\n\n![Three types of Translator](https://assets.knowt.com/pdf-flow-prod/04a3134e-1a13-4f25-b295-ce88ab895cdc-figures/15.png)\n\n- Three Types of Translators:\n - Assembler: Converts programs written in assembly language directly to machine code.\n - Interpreter: Translates one high-level program instruction at a time into machine code and executes it immediately before moving to the next.\n - Compiler: Converts all of the source program into an object program (machine code) at once prior to execution.\n\n# Problem Solving and Algorithm Development\n\n- Programming is fundamentally a process of problem-solving.\n- Five Steps of Problem-Solving Techniques:\n - Step 1: Analyze the problem.\n - Step 2: Outline the problem requirements.\n - Step 3: Specify what the solution should do.\n - Step 4: Design steps, called an algorithm, to solve the problem (constructing the general solution).\n - Step 5: Verify that the solution really solves the problem.\n\n- Algorithm Definition and Execution Pipeline:\n - An algorithm is the abstract idea of solving a problem.\n - Defined as step-by-step instructions that transform input into output.\n - Translation Pipeline: \text{Problem} \rightarrow \text{Problem Analysis} \rightarrow \text{Algorithm} \rightarrow \text{C++ Program}\n\n- Everyday Life Algorithm Example (Making a cup of hot coffee or chocolate):\n - Start\n - Boil water\n - Prepare a cup\n - Put a tea spoon of coffee & sugar / chocolate & sugar\n - Pour hot water\n - Stir\n - End\n\n- Three Fundamental Control Structures of Algorithms:\n - Sequence: A series of statements that execute one after another in order.\n - Selection (Branching): Statement used to determine which of two or more different statements to execute depending on certain conditions.\n - Looping (Repetition): Statement used to repeat statements while certain conditions are met.\n\n# Input-Process-Output (IPO) Analysis and IPO Charts\n\n- Input Process Output (IPO) Analysis Execution Order:\n  To complete an IPO Analysis, identification must strictly follow this order:\n - Step 1: Output (Display the results).\n - Step 2: Input (Read the data).\n - Step 3: Process (Perform the computation).\n\n- Structure of an Input Process Output (IPO) Chart:\n - Input Section: Enumerates raw input variables and parameters.\n - Processing Section:\n - Processing items: Formulas, equations, and computation steps.\n - Algorithm: Operational target description.\n - Output Section: Enumerates produced output variables.\n\n# Flowcharting and Pseudocode Representation\n\n- Algorithm Planning Steps:\n - Step 1: Perform Problem Analysis (Identify Input, Process, Output).\n - Step 2: Transfer logic to pseudocode or a flowchart.\n - Step 3: Enforce operational boundaries:\n - Must start with a `start` statement.\n - Must close with an `end` statement.\n\n- Ways or Techniques to Represent the Algorithm:\n - Pseudocode:\n - Artificial, informal language used to develop algorithms.\n - Usually referred to as "false code".\n - Similar to everyday English.\n - Standard Structure:\n - `start`\n - `statement 1`\n - `statement 2`\n - `...`\n - `end`\n - Note: Statements refer to any input, output, and process involved.\n - Flowchart:\n - Graphical representation of an algorithm.\n - Uses special-purpose geometric symbols connected by arrows (flow lines).\n\n# Practical Examples and Assignment Exercises\n\n- Example 1: Area of a Rectangle\n - Problem Statement: Calculate the area of a rectangle.\n - Problem Analysis:\n - Input: `width`, `height`\n - Process: \text{area} = \text{width} \times \text{height}\n - Output: `area of rectangle`\n - IPO Chart Format:\n - Input: `width`, `height`\n - Processing Items: \text{area} = \text{width} \times \text{height}\n - Algorithm: Area of a rectangle\n - Output: `area of rectangle`\n - Pseudocode Structure:\n - `start`\n - `1. Input width, height`\n - `2. Compute area: area = width * height`\n - `3. Print area of rectangle`\n - `end`\n\n- Example 2: Average of Three Numbers\n - Problem Statement: Find the average of three numbers input by user.\n - Problem Analysis:\n - Input: `number1`, `number2`, `number3`\n - Process: \text{average} = \frac{\text{number1} + \text{number2} + \text{number3}}{3}\n - Output: `average`\n - IPO Chart Format:\n - Input: `number1`, `number2`, `number3`\n - Processing Items: \text{average} = \frac{\text{number1} + \text{number2} + \text{number3}}{3}\n - Algorithm: average\n - Output: `average`\n - Pseudocode Structure:\n - `start`\n - `1. Input number1, number2, number3`\n - `2. Compute average: average = (number1 + number2 + number3) / 3`\n - `3. Print average`\n - `end`\n\n- Example 3: Total Cost of Apples\n - Problem Statement: Determine the total cost of apples, given the number of kilos of apples purchased and the cost of apples per kilo.\n - Problem Analysis:\n - Input: `Number of kilos of apples`, `Cost of apples per kilo`\n - Process: \text{Total cost} = \text{Number of kilos of apples} \times \text{Cost of apples per kilo}\n - Output: `Total cost of apples`\n - IPO Chart Format:\n - Input: `Number of kilos of apples`, `Cost of apples per kilo`\n - Processing Items: \text{Total cost} = \text{Number of kilos of apples} \times \text{Cost of apples per kilo}\n - Algorithm: Total cost of apples\n - Output: `Total cost of apples`\n - Pseudocode Structure:\n - `start`\n - `1. Input number of kilos of apples, cost of apple per kilo.`\n - `2. Compute total cost of apple: total cost = number of kilos of apples * cost of apple per kilo`\n - `3. Print total cost of apple`\n - `end`\n\n- Assignment Problem 1: ReadMe Bookstore Discount Calculation\n - Problem Statement: ReadMe Bookstore offers a 15\% discount for each revision book. Calculate and display the price that the customer should pay. Prepare IPO analysis, and pseudo-code.\n - IPO Analysis:\n - Input: `book_price`\n - Process:\n - \text{discount} = \text{book_price} \times 0.15\n - \text{final_price} = \text{book_price} - \text{discount}\n - Output: `final_price`\n - Pseudocode Structure:\n - `start`\n - `1. Input book_price`\n - `2. Compute discount: discount = book_price * 0.15`\n - `3. Compute final price: final_price = book_price - discount`\n - `4. Print final_price`\n - `end`\n\n- Assignment Problem 2: ABC Corporation Sales Bonus Calculation\n - Problem Statement: ABC Corporation gives a bonus to Juan based on his sales. The bonus is 5\% of the sales. Calculate Juan's bonus. Prepare IPO analysis, and pseudo-code.\n - IPO Analysis:\n - Input: `sales`\n - Process: \text{bonus} = \text{sales} \times 0.05$$
    • Output: bonus
  • Pseudocode Structure:
    • start
    • 1. Input sales
    • 2. Compute bonus: bonus = sales * 0.05
    • 3. Print bonus
    • end