Fundamentals of Programming - Algorithms and Flowcharts PDF
Document Details
Uploaded by Deleted User
Pamantasan ng Lungsod ng Maynila
Richard C. Regala
Tags
Related
- GE8151- PROBLEM SOLVING AND PYTHON PROGRAMMING - Question Bank PDF
- CPRO1 Introduction to Computer Programming PDF
- Lecture 1: Subject Introduction and Overview on Structured Programming Approach PDF
- Algorithms and Flowcharts Lecture Notes PDF
- Algorithms and Flowcharts PDF
- Unit 02: Computational Thinking and Algorithms PDF
Summary
This document is a lecture presentation on fundamental programming concepts, focusing on algorithms and flowcharts. It provides examples and pseudocode illustrations, making it useful for students learning these important programming topics.
Full Transcript
# Fundamentals of Programming ## Algorithms and Flowcharts (by Examples) - Richard C. Regala - Faculty, CS Department - College of Engineering and Technology - Pamantasan ng Lungsod ng Maynila ## Algorithms and Flowcharts - A typical programming task can be divided into two phases. - **Proble...
# Fundamentals of Programming ## Algorithms and Flowcharts (by Examples) - Richard C. Regala - Faculty, CS Department - College of Engineering and Technology - Pamantasan ng Lungsod ng Maynila ## Algorithms and Flowcharts - A typical programming task can be divided into two phases. - **Problem solving phase** - Produce an ordered sequence of steps that describe the solution of the problem. - This sequence of steps is called an **algorithm**. - **Implementation phase** - Implement the program in some programming language. ## Steps in Problem Solving - First, produce a general algorithm (one can use pseudocode). - Refine the algorithm successively to get step by step detailed **algorithm** that is very close to a computer language. - **Pseudocode** is an artificial and informal language that helps programmers develop algorithms. Pseudocode is very similar to everyday English. ## Pseudocode & Algorithm - **Example 1:** Write an algorithm to determine a student's final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks. ### Pseudocode: - Input a set of 4 marks. - Calculate their average by summing and dividing by 4. - If average is below 50, print "FAIL". - Else, print "PASS". ### Detailed Algorithm: - **Step 1:** Input M1, M2, M3, M4. - **Step 2:** GRADE = (M1+M2+M3+M4)/4 - **Step 3:** If (GRADE < 50) then - Print "FAIL". - Else, print "PASS". - Endif ## The Flowchart - **(Dictionary)** A schematic representation of a sequence of operations, as in a manufacturing process or computer program. - **(Technical)** A graphical representation of the sequence of operations in an information system or program. Information system flowcharts show how data flows from source documents through the computer to final distribution to users. Program flowcharts show the sequence of instructions in a single program or subroutine. Different symbols are used to draw each type of flowchart. ### A Flowchart - Shows logic of an algorithm. - Emphasizes individual steps and their interconnections. - E.g., control flow from one action to the next. ## Example 1 - A flowchart showing the logic for the algorithm to calculate a final grade and determine passing or failing. ## Example 2 - Write an algorithm and draw a flowchart to convert the length in feat to centimeter. ### Pseudocode: - Input the length in feet (Lft). - Calculate the length in cm (Lcm) by multiplying LFT with 30. - Print length in cm (LCM). ### Algorithm: - **Step 1:** Input Lft. - **Step 2:** Lcm = Lft x 30 - **Step 3:** Print Lcm ## Example 3 - Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area. ### Pseudocode: - Input the width (W) and Length (L) of a rectangle. - Calculate the area (A) by multiplying L with W. - Print A. ### Algorithm - **Step 1:** Input W, L - **Step 2:** A = L x W - **Step 3:** Print A ## Example 4 - Write an algorithm and draw a flowchart that will calculate the roots of a quadratic equation $ax^2 + bx + c = 0$. - **Hint:** d = sqrt($b^2$ - 4ac), and the roots are: $x_1 = (-b +d) / 2a$ and $x_2 = (-b-d)/2a$ ### Pseudocode: - Input the coefficients (a, b, c) of the quadratic equation. - Calculate d. - Calculate $x_1$. - Calculate $x_2$. - Print $x_1$ and $x_2$. ### Algorithm: - **Step 1:** Input a, b, c - **Step 2:** d = sqrt(b*b - 4*a*c) - **Step 3:** $x_1 = (-b + d) / (2*a)$ - **Step 4:** $x_2 = (-b-d) / (2*a)$ - **Step 5:** Print $x_1$, $x_2$ ## Decision Structures - The expression A>B is a logical expression. - It describes a condition we want to test. - If A>B is true (if A is greater than B) we take the action on the left. - Print the value of A - If A>B is false (if A is not greater than B) we take the action on the right. - Print the value of B. ## IF-THEN-ELSE Structure - The structure is as follows: - If condition then - true alternative - Else - false alternative - Endif - The algorithm for the flowchart is as follows: - If A>B then - Print A - Else - Print B - Endif ## Relational Operators | Operator | Description | | --------- | -------------------------- | | > | Greater than | | < | Less than | | = | Equal to | | ≥ | Greater than or equal to | | ≤ | Less than or equal to | | ≠ | Not equal to | ## Example 5 - Write an algorithm that reads two values, determines the largest value and prints the largest value with an identifying message. ### Algorithm: - **Step 1:** Input VALUE1, VALUE2 - **Step 2:** If (VALUE1 > VALUE2) then - MAX = VALUE1 - Else, MAX = VALUE2 - Endif - **Step 3:** Print "The largest value is", MAX ## Nested IFS - One of the alternatives within an IF-THEN-ELSE statement may involve further IF-THEN-ELSE statements. ## Example 6 - Write an algorithm that reads three numbers and prints the value of the largest number. ### Algorithm: - **Step 1:** Input N1, N2, N3 - **Step 2:** If (N1>N2) then - If (N1>N3) then - MAX = N1 [N1>N2, N1>N3] - Else, MAX = N3 [N3>N1>N2] - Endif - Else - If (N2>N3) then - MAX = N2 [N2>N1, N2>N3] - Else, MAX = N3 [N3>N2>N1] - Endif - Endif - **Step 3:** Print "The largest number is", MAX ## Example 7 - Write an algorithm and draw a flowchart to: - Read an employee name (NAME), overtime hours worked (OVERTIME), hours absent (ABSENT) - Determine the bonus payment (PAYMENT). ### Algorithm: - **Step 1:** Input NAME, OVERTIME, ABSENT - **Step 2:** If (OVERTIME-(2/3)*ABSENT > 40) then - PAYMENT = 50 - Else if (OVERTIME-(2/3)*ABSENT > 30) then - PAYMENT = 40 - Else if (OVERTIME-(2/3)*ABSENT > 20) then - PAYMENT = 30 - Else if (OVERTIME-(2/3)*ABSENT > 10) then - PAYMENT = 20 - Else PAYMENT = 10 - Endif - Endif - **Step 3:** Print "Bonus for", NAME "is $", PAYMENT ## Bonus Schedule | OVERTIME - (2/3)*ABSENT | Bonus Paid | | ------------------------------ | ----------- | | >40 hours | $50 | | >30 but ≤ 40 hours | $40 | | >20 but ≤ 30 hours | $30 | | >10 but ≤ 20 hours | $20 | | ≤ 10 hours | $10 | ## Sample Exercises: - **Sample 1:** Write a program that calculates the sum of two input numbers and display the result. - **Sample 2:** Write a program to calculate the area of a circle and display the result. Use the formula: A=πr² where Pi is approximately equal to 3.1416. - **Sample 3:** Write a program that computes the average of three input quizzes, and then display the result. - **Sample 4:** Write a program that converts the input Fahrenheit degree into its Celsius degree equivalent. Use the formula: C= (5/9)*F-32. - **Sample 5:** Create a program to compute the volume of a sphere. Use the formula: V= (4/3)*πr³ where is pi equal to 3.1416 approximately. The r³ is the radius. Display result. ## Thank You!