Week 5: Algorithms and Programming Basics
Document Details

Uploaded by RejoicingTroll7809
Tags
Related
- Introduction to Programming Concepts PDF
- Fundamentals Of Programming PDF
- Lecture 1: Subject Introduction and Overview on Structured Programming Approach PDF
- Fundamentals of Programming - Algorithms and Flowcharts PDF
- Algorithms and Flowcharts Lecture Notes PDF
- Lecture 4 (Pseudocode-internet-processor) PDF
Summary
This document covers fundamental concepts in computer science, including program development, algorithms, and programming languages. Included are discussions on flowcharts, pseudocode, compilers, and interpreters, along with practical exercises and assignments to reinforce understanding of these topics.
Full Transcript
**[WEEKÂ 5: ALGORITHMS AND PROGRAMMING BASICS]** In this week's lecture, we will explore three main topics: 1. **Program Development, Flowcharts, and Algorithms** 2. **Programming Languages, Compilers, and Interpreters** 3. **Algorithms -- Pseudocode and Flowcharts** **[1. PROGRAM DEVELOPMENT...
**[WEEKÂ 5: ALGORITHMS AND PROGRAMMING BASICS]** In this week's lecture, we will explore three main topics: 1. **Program Development, Flowcharts, and Algorithms** 2. **Programming Languages, Compilers, and Interpreters** 3. **Algorithms -- Pseudocode and Flowcharts** **[1. PROGRAM DEVELOPMENT, FLOWCHARTS, AND ALGORITHMS]** **What Is Program Development?** - **Definition:** Program development is the process of designing, writing, testing, and maintaining code that instructs computers to perform specific tasks. - **Phases of Development:** - **Planning:** Understand the problem and determine requirements. - **Design:** Outline the structure and flow of the program (using diagrams like flowcharts). - **Coding:** Write the actual code in a programming language. - **Testing:** Run the program to identify and fix errors. - **Maintenance:** Update the program as needed after deployment. **What Is an Algorithm?** - **Definition:** An algorithm is a step-by-step procedure or a set of rules to solve a problem or perform a computation. - **Example:** A recipe for baking a cake is like an algorithm---it lists the steps in order. - **Key Characteristics:** - **Input:** What you start with (data, parameters). - **Process:** The steps taken to transform the input. - **Output:** The final result. **Flowcharts: Visualizing Algorithms** - **Purpose:** Flowcharts are diagrams that visually represent the steps of an algorithm or process. - **Common Symbols:** - **Oval:** Start and End points. - **Rectangle:** Process or instruction. - **Diamond:** Decision (yes/no question). - **Parallelogram:** Input/Output operations. - **Arrows:** A line that connects & shows the relationship between the shapes. **Example Flowchart: Calculating the Sum of Two Numbers** flowchart TD A\[Start\] \--\> B\[Input Number 1 and Number 2\] B \--\> C\[Calculate Sum = Number1 + Number2\] C \--\> D\[Display Sum\] D \--\> E\[End\] *Diagram Explanation:* 1. **Start:** The process begins. 2. **Input:** The user is prompted to enter two numbers. 3. **Calculation:** The program computes the sum of the two numbers. 4. **Output:** The sum is displayed. 5. **End:** The process terminates. **Assignment 1** - **Task:** Create a flowchart for an algorithm that determines if a number is even or odd. - **Hints:** - Use a diamond symbol to represent the decision (i.e., checking if the remainder when divided by 2 equals 0). - Include input and output steps. **2. [PROGRAMMING LANGUAGES, COMPILERS, AND INTERPRETERS]** **Introduction to Programming Languages** - **Definition:** A programming language is a formal language comprising a set of instructions that produce various kinds of output. They are used to implement algorithms. - **High-Level vs. Low-Level:** - **High-Level Languages:** Closer to human language (e.g., Python, Java). Easier to read, write, and maintain. - **Low-Level Languages:** Closer to machine language (e.g., Assembly). Offers more control over hardware but is harder to read and write. **Compilers vs. Interpreters** - **Compilers:** - **Function:** Translate the entire source code into machine code before execution. - **Examples:** C, C++. - **Pros:** Faster execution since code is pre-compiled; optimizations can be applied. - **Cons:** Errors are reported after compiling the whole program, which can make debugging harder. - **Interpreters:** - **Function:** Translate and execute code line-by-line. - **Examples:** Python, JavaScript. - **Pros:** Easier to debug since execution stops at the error line; good for rapid prototyping. - **Cons:** Slower execution because translation happens during runtime. **Diagram: Compilation vs. Interpretation Process** flowchart LR subgraph Compilation A\[Source Code\] \--\> B\[Compiler\] B \--\> C\[Machine Code\] C \--\> D\[Execution\] end subgraph Interpretation E\[Source Code\] \--\> F\[Interpreter\] F \--\> G\[Direct Execution\] end *Diagram Explanation:* - **Compilation:** The compiler converts the complete source code into machine code which is then executed. - **Interpretation:** The interpreter reads and executes each line of code directly without a separate machine code stage. **Assignment 2** - **Task:** Write a brief paragraph (4--5 sentences) comparing compilers and interpreters. Focus on one advantage and one disadvantage of each. **3. Algorithms -- Pseudocode and Flowcharts** **What Is Pseudocode?** - **Definition:** Pseudocode is a plain language description of the steps in an algorithm. It is not written in any specific programming language syntax but in an easy-to-understand format. - **Purpose:** It helps programmers plan their code and logic before writing actual code. - **Key Features:** - Uses simple language. - Focuses on logic rather than syntax. - Helps in brainstorming and debugging. **Writing Effective Pseudocode** - **Guidelines:** - **Keep It Simple:** Use clear and concise language. - **Structure:** Break down the problem into smaller, manageable parts. - **Use Indentation:** Helps in showing hierarchy and decision structures (like loops and conditionals). **Example: Factorial of a Number** **Pseudocode:** START INPUT number SET factorial to 1 FOR each integer i from 1 to number DO SET factorial = factorial \* i END FOR OUTPUT factorial END **Flowchart for Factorial Calculation:** flowchart TD A\[Start\] \--\> B\[Input number\] B \--\> C\[Set factorial = 1\] C \--\> D\[Set i = 1\] D \--\> E{Is i \ F\[Set factorial = factorial \* i\] F \--\> G\[Increment i by 1\] G \--\> E E \-- No \--\> H\[Output factorial\] H \--\> I\[End\] *Diagram Explanation:* 1. **Start:** Begin the process. 2. **Input:** The user inputs a number. 3. **Initialization:** Factorial is set to 1, and a counter i starts at 1. 4. **Loop (Decision):** The algorithm checks if i is less than or equal to the number. 5. **Calculation:** Multiply the current factorial by i and increment i. 6. **Output:** Once the condition fails, output the factorial result. 7. **End:** Terminate the process. **Assignment 3** - **Task:** 1. Write pseudocode for finding the largest number in an array of integers. 2. Create a flowchart that represents the pseudocode you have written. - **Hints:** 1. Start by assuming the first element is the largest. 2. Loop through the array and update the largest number if a bigger number is found.