lecture 3&4_CSSD 101_Problem Solving

Published on
Embed video
Share video
Ask about this video

Scene 1 (0s)

Ghana Communication Technology University (GCTU) Department of Computer Science CSSD 101 Programming and Problem Solving Lecturer: Prince Modey Email: pmodey@gctu.edu.gh.

Scene 2 (9s)

Objectives ❖ By the end of this lecture, students should be able to: ➢explain problem solving techniques ➢Demonstrate the ability to design basic algorithms for problem-solving ➢Write pseudocodes to represent algorithms ➢Demonstrate the ability to design and interpret flowcharts..

Scene 3 (22s)

Problem Solving ❖Problem solving involves identifying a specific issue or challenge and devising a systematic approach to arrive at a solution. ❖It is a fundamental skill for computer scientists and programmers. ➢It enables them to break down complex problems into manageable parts and apply algorithms and programming to create effective solutions..

Scene 4 (37s)

Steps in Problem Solving ❖Understand the Problem: ➢Clarify Requirements: Ensure you comprehend what is being asked. ➢Read the problem statement carefully, and clarify any uncertainties. ➢Identify Inputs and Outputs: Determine what data you will receive (inputs) and what results are expected (outputs). ❖Analyze the Problem: ➢Break It Down: Decompose the problem into smaller, manageable components. ➢This may involve identifying subproblems that can be solved independently. ➢Explore Constraints: Acknowledge any limitations or restrictions defined in the problem statement (e.g., time limits, resource constraints)..

Scene 5 (1m 3s)

Steps in Problem Solving ❖Develop a Plan (Algorithm Design): ➢Choose an Approach: Decide on how to tackle the problem. ➢This may involve selecting an appropriate algorithm or strategy based on the nature of the problem. ➢Create a Step-by-Step Procedure: Write down an algorithm, which is essentially a sequence of instructions that outlines how to solve the problem. ➢This could be in the form of pseudocode or a flowchart. ❖Implement the Solution: ❖Code the Algorithm: Translate the designed algorithm into a specific programming language, such as Python, Java, or C++. ❖Write the Program: Ensure that the code is structured, efficient, and readable. ❖This includes following best practices, such as commenting and using meaningful variable names..

Scene 6 (1m 34s)

Steps in Problem Solving ❖Test the Solution: ❖Run Test Cases: Execute the program with various test cases to ensure it behaves as expected. This includes both typical cases and edge cases. ❖Debug: If any issues arise, use debugging techniques to locate and fix errors within the program. Testing, debugging, and refining are iterative processes. ❖Evaluate and Optimize: ❖Review the Solution: Assess whether the solution meets the original problem requirements effectively. ❖Optimize the Code: Consider ways to improve the efficiency of the program in terms of time and space complexity, if necessary. ❖Document and Maintain: ❖Create Documentation: Maintain clear and comprehensive documentation for users and other developers who may work with the code in the future. ❖Refactor if Needed: As requirements change or bugs are discovered, the solution may need to be updated or refactored to maintain its effectiveness..

Scene 7 (2m 11s)

Concepts in Problem Solving ❖Algorithms ❖Data Structures ❖Logical Thinking ❖Creativity.

Scene 8 (2m 18s)

What is Computational Thinking? ❖It is a problem-solving process that involves the following: ➢Decomposition: Breaking a problem into smaller, manageable parts. ➢Pattern Recognition: Identifying trends or similarities in data. ➢Abstraction: Focusing on the important information only, ignoring irrelevant details. ➢Algorithm Design: Creating a step-by-step solution to a problem..

Scene 9 (2m 34s)

Importance of Computational Thinking ❖Enhances logical reasoning skills. ❖Fosters creativity in problem-solving. ❖Applicable across various disciplines (science, mathematics, humanities). ❖Encourages systematic and logical approaches to real-world problems..

Scene 10 (2m 46s)

Problem-Solving Techniques ❖Brute Force: Trying every possible solution. ❖Divide and Conquer: Breaking the problem into subproblems, solving each independently. ❖Dynamic Programming: Solving problems by breaking them down and storing results of subproblems. ❖Greedy Algorithms: Making the optimal choice at each stage to find an overall solution..

Scene 11 (3m 3s)

Exploring Problem-Solving Techniques ❖Divide and Conquer Example: ➢Problem: Sorting numbers. ➢Strategy: Split the list into smaller sublists, sort each, then merge them. ❖Greedy Algorithm Example: ➢Problem: Change-making problem with coins. ➢Strategy: Always use the largest denomination first..

Scene 12 (3m 18s)

ALGORITHMS, PSEUDOCODES AND FLOWCHARTS.

Scene 13 (3m 24s)

Algorithm ❖An algorithm is a finite set of well-defined instructions to solve a problem or complete a task. ❖An algorithm is a procedure consisting of a finite number of precisely defined steps for solving a problem. ❖Each step of an algorithm must be an unambiguous instruction which, when written in a computer language, can be executed by a computer. An algorithm is: ❖A sequence of instructions. ❖ A procedure or formula for solving a problem. ❖It was created by a mathematician, Mohammed Ibn-Musa Al-Khwarizmi. 13.

Scene 14 (3m 47s)

Characteristics of an algorithm ❖Well-ordered: the steps are in a clear order ❖Unambiguous: the operations described are understood by a computing agent without further simplification. ❖Effectively computable: the computing agent can actually carry out the operation. ❖Language independent. ❖Finiteness: an algorithm must have a finite number of steps. It should eventually terminate after a certain number of steps. 14.

Scene 15 (4m 5s)

Characteristics of an algorithm ❖Definiteness: Each step of the algorithm must be clearly and unambiguously defined. ❖Input: An algorithm should have zero or more inputs, which are the values provided to it initially. ❖Output: An algorithm should produce one or more outputs, which are the results of the algorithm's computations. ❖Effectiveness: The steps of an algorithm should be basic enough to be carried out, in principle, by a person using only paper and pencil. 15.

Scene 16 (4m 27s)

Method for developing an algorithm ❖1. Define the problem: state the problem you are trying to solve in clear and concise terms. ❖2. List the inputs (information needed to solve the problem) and the outputs (what the ❖Algorithm will produce as a result) ❖3. Describe the steps needed to convert or manipulate the inputs to produce the outputs. ❖Start at a high level first, and keep refining the steps until they are effectively computable operations. ❖4. Test the algorithm: choose data sets and verify that your algorithm works 16.

Scene 17 (4m 51s)

Algorithm: Example One Let’s look at a very simple algorithm called find_max(). Problem: Given a list of positive numbers, return the largest number on the list. Inputs: A list l of positive numbers. This list must contain at least one number. (Asking for the largest number in a list of no numbers is not a meaningful question.) Outputs: A number n, which will be the largest number of the list. Algorithm: 1. Start 2.Set max to 0. 3.For each number x in the list l, compare it to max. If x is larger, set max to x. 4. Max is now set to the largest number in the list. 5.Stop 17.

Scene 18 (5m 22s)

Algorithm: Example Two Problem: Design an algorithm to add two numbers and display the result. Algorithm: Step 1 − start Step 2 − declare three integers a, b & c Step 3 − define values of a & b Step 4 − add values of a & b Step 5 − store output of step 4 to c Step 6 − print c Step 7 − stop 18.

Scene 19 (5m 37s)

Techniques for laying out an Algorithm ▪ Pseudo-Code ▪ Flowcharts 19.

Scene 20 (5m 44s)

Pseudocode ❖Pseudocode provides a way to outline algorithms in a human-readable format before writing actual code. ❖A pseudocode consists of natural language-like statements that precisely describe the steps of an algorithm. ➢it describe actions and focuses on the logic of the algorithm or program while avoiding language-specific elements. ➢they have no real formatting or syntax rules and cannot be compiled nor executed. ❖It is written at a level so that the desired programming code can be generated almost automatically from each statement steps are numbered. Subordinate numbers and/or indentation are used for dependent statements in selection and repetition structures. 20.

Scene 21 (6m 11s)

Key Features of Pseudocode 21 ❖ Language-Agnostic: Pseudocode is not tied to any specific programming language. It uses plain English and simple syntax to describe the logic of an algorithm. ❖ Readable: It is designed to be easily understood by humans, including those who may not be familiar with programming. ❖ Structured: Pseudocode follows a structured format, often resembling the control structures found in programming languages (e.g., loops, conditionals)..

Scene 22 (6m 32s)

Advantages of Pseudocode • Reduced complexity. • Increased flexibility. • Ease of understanding. • Clarity: Pseudocode helps clarify the steps of an algorithm, making it easier to understand and communicate. • Planning: It allows programmers to plan their approach before diving into coding, reducing the likelihood of errors. • Collaboration: Pseudocode can be shared with others (e.G., Team members, stakeholders) to ensure everyone understands the algorithm's logic. • Focus on logic: By abstracting away the syntax of a specific programming language, pseudocode allows you to focus on the algorithm's logic and structure. 22.

Scene 23 (6m 58s)

Why is pseudocode necessary? ❖The programming process is a complicated one. ❖You must first understand the program specifications. ❖Then you need to organize your thoughts and create the program. ❖ You must break the main tasks that must be accomplished into smaller ones in order to be able to eventually write fully developed code. ❖Writing pseudocode will save you time later during the construction & testing phase of a program's development. 23.

Scene 24 (7m 18s)

PSEUDOCODE ❖A pseudocode can be in the form of: • Sequence • Selection • Repetition 24.

Scene 25 (7m 25s)

PSEUDOCODE ❖Sequence • Writing one action after another. Actions are performed in the sequence in which they are written. • The computer executes the program starting at the beginning and working its way to the end. 25.

Scene 26 (7m 38s)

PSEUDOCODE • Selection • Making a choice. • Binary choice on a given boolean condition is indicated by the use of four keywords: if, then, else and endif If condition Then Sequence 1 Else Sequence 2 Endif 26.

Scene 27 (7m 50s)

Pseudocode • Repetition • Keep performing an action until some condition occurs. While condition Do Endwhile 27.

Scene 28 (7m 58s)

Pseudocode How to write pseudocode statements? There are six(6) basic computer operations: 1. Input: A computer can receive information Read, get, obtain, input Read (Information from a file) Get (Information from the keyboard) Information is stored in a variable • General format: Input name of variable • Example: Get user_name 28.

Scene 29 (8m 13s)

Pseudocode How to write pseudocode statements? 2. Output: A computer can display information write, display, print, show Write (information to a file) Display (information to the screen) • General format: Output name of variable •Example: Display scores 29.

Scene 30 (8m 27s)

Pseudocode How to write pseudocode statements? 3. Computations: A computer can perform arithmetic using actual mathematical symbols or the words for the symbols +, -, *, / Calculate, compute • Example: Add number to total Total = total + number 30.

Scene 31 (8m 40s)

Pseudocode How to write pseudocode statements? 4. Assignment: A computer can assign a value to a piece of data • Operational symbols and keywords ← / = Initialize / set / assign • Examples: Assign expression to var2 x = 5+y X ← 2 31.

Scene 32 (8m 53s)

PSEUDOCODE How to write pseudocode statements? 5. Selection: A computer can compare two piece of information and select one of two alternative actions General formats: If statement If condition Then Some action Endif Example: If temperature < 0 then Wear a jacket 32.

Scene 33 (9m 5s)

Pseudo-code How to write pseudocode statements? If-then-else statement IF condition Then Some action Else Alternative action Endif Example: If (at work) then Dress formally Else Dress casually 33.

Scene 34 (9m 15s)

Pseudo-code How to write pseudocode statements? Switch Statement Case 1: action1 Case 2: action2 etc. Default: actionx 34.

Scene 35 (9m 24s)

Pseudocode How to write pseudocode statements? Switch statement Example: Input a grade Case >= 100 Display “perfect score” Case > 89 Display “grade = A” 35.

Scene 36 (9m 33s)

Pseudocode How to write pseudocode statements? Switch statement Case > 79 Display “grade = B” Case > 69 Display “grade = C” Case > 59 Display “grade = D” Default display “grade = F” 36.

Scene 37 (9m 43s)

Pseudo-code How to write pseudocode statements? 6. Repetitions: A computer can repeat a group of actions General format: While statement WHILE condition (Is true) Some action Endwhile Example: While students ask questions Answer question 37.

Scene 38 (9m 54s)

Pseudocode How to write pseudocode statements? 6. Repetitions: A computer can repeat a group of actions General format: For statement For a number of times Some action Endfor Example: For score is less than 50 Display fail 38.

Scene 39 (10m 5s)

Pseudocode Example one - SEQUENCE Write a pseudo-code for a program that obtains two integer numbers from the User. It will print out the sum of those numbers. Pseudo-code 1. Start 2. Prompt the user to enter the first integer 3. Prompt the user to enter a second integer 4. Compute the sum of the two user inputs 5. Display an output prompt that explains the answer as the sum 6. Display the result 7. Stop 39.

Scene 40 (10m 26s)

Pseudocode Example Two - SEQUENCE Write a pseudo-code for finding average of any three numbers. Pseudo-code 1. Begin 2. Write “please enter 3 numbers to add” 3. Read num1 4. Read num 2 5. Read num 3 6. Sum = num1 + num2 + num3 7. Average = sum / 3 8. Display average 40.

Scene 41 (10m 41s)

Pseudocode • Example 3 - SEQUENCE • The area of a rectangle is the product of the rectangle’s length and width. • A rectangle program should prompt an end-user for the length and width of a rectangle. • Furthermore, it should calculate and display the area of the rectangle. Write a pseudocode for the program. 41.

Scene 42 (10m 56s)

Pseudocode • Example 3 - SEQUENCE • The area of a rectangle is the product of the rectangle’s length and width. • A rectangle program should prompt an end-user for the length and width of a rectangle. • Furthermore, it should calculate and display the area of the rectangle. • Write a pseudocode for the program. 42.

Scene 43 (11m 12s)

Pseudocode • Solution DECLARE variables length, width, area DISPLAY “Enter length of rectangle” INPUT length DISPLAY “Enter width of rectangle” INPUT width COMPUTE area = length * width DISPLAY area 43.

Scene 44 (11m 22s)

Pseudocode Example 4 - selection • Pseudocode that prints passed or failed based on student’s score. Solution IF student’s score is greater than or equal to 50 PRINT “passed” Else PRINT “failed” Endif 44.

Scene 45 (11m 33s)

Pseudocode Example 5 - Repetition A maths tutor has taught his pupils odd numbers between 1 and 30. He wants his pupils to know these numbers Write a pseudocode for an odd number program that will generate numbers between 1 and 30. 45.

Scene 46 (11m 46s)

Pseudocode Solution - Repetition DECLARE variable oddnumber SET oddnumber = 1 WHILE (oddnumber < 30) PRINT oddnumber CALCULATE oddnumber = oddnumber + 2 Endwhile 46.

Scene 47 (11m 54s)

Pseudocode Example Algorithm: Find the Maximum Number Input: List of numbers Output: Maximum number Begin Set max to first number in the list For each number in the list: If number > max then Set max to number End For Return max End 47.

Scene 48 (12m 6s)

ASSIGNMENT 1. Write an algorithm of finding an area of a square and a cube 2. Write a pseudocode to solve S = (a + b + c) / y 3. Write an algorithm and pseudocode 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. 48.

Scene 49 (12m 22s)

Flowchart ❖ A flowchart is a diagrammatic representation that depicts the steps and structure of an algorithm or program. ❖These flowcharts play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems. ❖Once the flowchart is drawn, it becomes easy to write the program in any high-level language. ❖Each step in the process is represented by a specialized symbol or a box and then connected with arrow. ❖It is a useful tool for planning each operation a program performs and the order in which the operations are to occur. 49.

Scene 50 (12m 48s)

Flowchart ➢Benefits of using flowcharts ➢Clarity: Flowcharts provide a clear and visual way to understand complex processes. ➢Communication: They help communicate the logic of a process to others, including those who may not be familiar with programming. ➢ Problem-solving: Flowcharts aid in identifying potential issues and inefficiencies in a process. ➢Documentation: They serve as useful documentation for processes and algorithms, making it easier to maintain and update them. 50.