1. Problem solving and algorithms.pptx
Document Details
Uploaded by GracefulMossAgate
Full Transcript
Introduction to Programming and Problem Solving SWE-225 This class? ‣ Problem Definition to Problem Solving ‣ Algorithms ‣ Learn to write algorithms ‣ PyCharm - Install the Python programming environment and practice basic syntax Problem Definition to Problem Solving Analysis, Design, Coding, and Te...
Introduction to Programming and Problem Solving SWE-225 This class? ‣ Problem Definition to Problem Solving ‣ Algorithms ‣ Learn to write algorithms ‣ PyCharm - Install the Python programming environment and practice basic syntax Problem Definition to Problem Solving Analysis, Design, Coding, and Testing The IPO Model ‣ The fundamental architecture of a computer system rests on the foundation of IPO Model. ‣ The Input, Process, Output (IPO) Model ‣ A computer processes input data to produce results/output. ‣ (I) Input - Data provided to the computer ‣ (P) Process - Actions taken on the input by the computer ‣ (O) Output - Results from the computer processing ‣ The model is also known as the IPO(S) model. ‣ What do you think ‘S’ stands for? IPO(S) Model ‣ Input ‣ Computer systems include methods for accepting data and instructions from inside and outside the system. ‣ Example of input devices include: Keyboard, Mouse, Disk, and Network ‣ Process ‣ Based on instructions given, the computer systems has the ability to process i.e., to change, and transform data ‣ Basic data processing operations include: ‣ Arithmetic calculations ‣ Logical & Relational decisions ‣ Data manipulation, storage, & retrieval INPU T PROCES S OUTPUT STORAG E IPO(S) Model ‣ Output ‣ Computer systems has the ability to present processed data in a form that is understood by the users ‣ Example of output devices include: Screen/Monitor, Printer, and Network ‣ Storage ‣ Computer systems has the ability to store data and programs temporarily and permanently ‣ Random Access Memory (RAM) for short-term and temporary storage ‣ Secondary storage devices like Hard Disk and USB for long-term and permanent storage INPU T PROCES S OUTPUT STORAG E Problem Solving ‣ Computers and associated programs are tools used to help in solving scientific problems ‣ Steps for Problem Solving ‣ Analyse & Design: ‣ Clearly analyse and understand the requirements ‣ Design a sequential, step-wise approach to arrive at the solution ‣ Implement & Test ‣ Implement the designed steps in a programming language (ex. Python) ‣ Test the program for different input cases, to verify if all requirements are met ‣ Maintain ‣ Modify if the problem domain or requirement changes ‣ Modify to improve performance Analyse & Design ‣ As a first step towards problem solving, we need to: ‣ Clearly understand the problem and list out: ‣ Required INPUTS ‣ PROCESS to arrive at the solution – Write an Algorithm ‣ Expected results or OUTPUT for the input and process ‣ An Algorithm ‣ is a tool used to clearly understand and design the solution to a given problem. ‣ is a step-by-step problem-solving process in which a solution is arrived at, in a finite number of steps and amount of time. ‣ Problem: Add two numbers, 83 and 2. ‣ Input: Two integers that are given 83 and 2 ‣ Process: Addition (+) ‣ Expected Output: 85 ‣ Algorithm 1.Start 2.Write 83+2 3.Stop Implement & Test ‣ Once the Algorithm is developed, it can be used to guide a programmer to write a computer program. ‣ The programmer implements the program using a programming language like Python or Java. ‣ The program is then executed by the computer and the result is displayed ‣ The result is verified with the required solution of the problem. # Program in Python # Add and Display Result def main(): print(83 + 2) main() // Program in Java // Add and Display Result public class AddNum { public static void main(String[] args){ System.out.println(83+2); } } Maintain ‣ Problem requirements and applications can change and therefore the programs can be altered or modified to adapt to the changes. ‣ Once the program is working well for a specified set of requirements, then it could be modified for various reasons, like: ‣ To use a different set of inputs ‣ To use a different process ‣ Improving performance and efficiency (faster results in shorter time) ‣ Improving readability Algorithms What are they? What does a computer do? ‣ Essentially: ‣ They perform calculations - billions of calculations per second ‣ They remember data - many gigabytes of storage ‣ What kinds of calculations? ‣ Some are built-in to the language ‣ Some are defined by you as the programmer ‣ Computers only know what we tell them What is an algorithm? ‣ An algorithm is a finite sequence of steps performing a task such that, each step is a clear and unambiguous instruction, which can be executed in finite time. ‣ The sequence in which the steps are to be executed is clearly defined ‣ The process is guaranteed to stop in a finite time ‣ After a finite number of steps have been executed ‣ The process (hence the algorithm) has a purpose ‣ There is an input to the process and an output from the process ‣ Algorithms do not need to be mathematical What are recipes? 1.A series of simple steps 2.Processes that control the flow to determine when each step should be executed 3.A way to determine when to stop ‣ Combine steps 1, 2 and 3, and you have an algorithm Examples ‣ How ‣ How ‣ How ‣ How do do do do you you you you bake a cake? book a flight ticket? get to class? calculate the area of a triangle? Computers are machines ‣ The challenge is to capture these recipes in terms of mechanical or logical processes. How? ‣ Fixed program computers have only one purpose. Usually they perform basic tasks and changing the program often requires rewiring, restructuring or redesigning the device. An example of this is a basic calculator. ‣ Stored program computers keeps or accesses programs in memory, meaning that it is capable of performing a variety of tasks. Basic machine architecture Memory Control Unit Input Arithmetic Logic Unit Output Basic primitives ‣ A primitive is the simplest type of programming language item. It may also refer to the smallest processing unit accessible by a programmer. ‣ Primitive types are basic programming language building blocks. A programmer combines primitives to create more complex operations, such as functions, procedures and methods. Creating recipes ‣ A programming language provides a set of primitive operations ‣ Primitives can be combined in complex ways to create expressions ‣ Expressions and computations have values, meaning and context in programming languages Aspects of languages ‣ Primitive or basic constructs ‣ English: words ‣ Programming language: numbers, strings, operators ‣ Syntax ‣ English: ‣ “boy chair cake” → not valid syntactically ‣ “boy eats cake” → syntactically valid ‣ Programming language: ‣ “hello”5 → not valid syntactically ‣ 6 + 5 → syntactically valid Aspects of languages ‣ static semantics is which syntactically valid strings have a meaning ‣ English: ‣ “I are hungry” → syntactically valid but has a static semantic error ‣ “I am hungry” → no error ‣ Programming language: ‣ 3.2 * 5 → syntactically valid ‣ 3 + “hi” → has a static semantic error Aspects of languages ‣ semantics is the meaning associated with a syntactically correct string of symbols with no static semantic errors ‣ English: can have many meanings “Flying planes can be dangerous” ‣ Programming languages: have only one meaning but may not be what programmer intended Where do things go wrong? ‣ syntactic errors ‣ common and easily caught ‣ static semantic errors ‣ some languages check for these before running program. If these errors are not caught, they can can cause unpredictable behaviour ‣ no semantic or syntactic errors, but a different meaning than what the programmer intended ‣ program crashes, stops running ‣ program runs forever ‣ program gives an answer but different than expected Python programs ‣ A program is a sequence of definitions and commands ‣ Commands (statements) instruct the interpreter to do something ‣ Programs can be typed directly in a shell or stored in a file that is read into the shell and evaluated Class Work ‣ Problem: Write an algorithm to display a message “Hello, How are you?” ‣ Inputs: ‣ Output: Class Work - Answer ‣ Problem: Write an algorithm to display a message “Hello, How are you?” ‣ Inputs: None (the problem has no unknowns) ‣ Output: “Hello, How are you?” ‣ Algorithm 1. Start 2. Write “Hello, How are you?” 3. Stop Class Work ‣ Problem: Write an algorithm to determine and display the sum of 8 and -2. (Assuming that 8 and -2 are numbers) ‣ Inputs: ‣ Output: Class Work - Answer ‣ Problem: Write an algorithm to determine and display the sum of 8 and -2. (Assuming that 8 and -2 are numbers) ‣ Inputs: None (the problem has no unknowns) ‣ Output: 6 ‣ Algorithm 1. Start 2. Write 8 + (-2) 3. Stop Class Work ‣ Problem: Write an algorithm to ask someone’s name and welcome the person. ‣ Inputs: ‣ Output: Class Work - Answer ‣ Problem: Write an algorithm to ask someone’s name and welcome the person. ‣ Inputs: One unknown input, ‣ Output: “Hello ” “ nice to meet you.” ‣ Algorithm 1. Start 2. Read name 3. Write “Hello ” + name + “ nice to meet you.” 4. Stop Class Work ‣ Problem: Write an algorithm to determine and display the sum of any two numbers. ‣ Inputs: ‣ Output: Class Work - Answer ‣ Problem: Write an algorithm to determine and display the sum of any two numbers. ‣ Inputs: Two unknown numbers, & ‣ Output: Sum of the two numbers given by user ‣ Algorithm 1. Start 2. Write “Enter first number: ” 3. Read number1 4. Write “Enter second number: ” 5. Read number2 6. result = number1 + number2 7. Write “Sum is: ” + result 8. Stop Class Work ‣ Problem: Write an algorithm to determine and display the square and cube of a number. ‣ Inputs: An unknown number ‣ Output: ‣ Square of number1 ‣ Cube of number1 ‣ Algorithm 1. 2. 3. 4. 5. 6. 7. 8. Start Write “Enter a number: ” Read number1 square = number1 * number1 cube = number1 * number1 * number1 Write “Square is: ” + square Write “Cube is: ” + cube Stop Class Work - Answer ‣ Problem: Write an algorithm to determine and display the square and cube of a number. ‣ Inputs: ‣ Output: Class Work ‣ Problem: Write and algorithm to determine and display the average of 3 numbers. ‣ Inputs: ‣ Output: Class Work - Answer ‣ Problem: Write and algorithm to determine and display the average of 3 numbers. ‣ Inputs: Three unknown numbers , , ‣ Output: Average of 3 numbers ‣ Algorithm 1. Start 2. Write “Enter 3 numbers: ” 3. Read number1, number2, number3 4. average = (number1 + number2 + number3) / 3 5. Write “Average is: ” +average 6. Stop Class Work ‣ Problem: Write an algorithm to determine and display the area of a triangle where the base and height is given by the user. Ensure to show the result as “Area = nnn”. ‣ Inputs: ? ‣ Output: ? ‣ Algorithm: Complete the algorithm and walkthrough with different inputs to check the validity of the algorithm. Class Work ‣ What would be the output of the following algorithm? ‣ Algorithm 1. Start 2. number1 = number2 = number3 = 0 3. number1 = 3 4. number2 = number1 * 4 5. number1 = number2 6. number3 = number3 + number2 + number1 7. Write number1 8. Write number2 9. Write number3 10.Stop Class Work ‣ What would be the output of the following algorithm? ‣ Algorithm 1. Start 2. number1 = 0 3. number2 = 0 4. number3 = 0 5. number4 = 0 6. number1 = 2 7. number2 = number1 * 2 8. number3 = number2 + number1 * 4 9. number4 = (number2 + number1) * 4 10.Write number3 11.Write number3 12.Stop Class Work ‣ Problem: Write an algorithm to evaluate the expression: f(x) = 2 15x + 4x + 2. ‣ Inputs: ? ‣ Output: ? ‣ Algorithm: Complete the algorithm and walkthrough with different inputs to check the validity of the algorithm. PyCharm An IDE for writing Python Programs Download Python 3 ‣ Navigate to https://www.python.org/ on your browser. ‣ Click on downloads in the menu and click on Python 3.x ‣ Once downloaded, install the package. Download Python 3 ‣ Click on downloads in the menu and click on Python 3.x ‣ Once downloaded, install the package. Download IDE ‣ Navigate to https://www.jetbrains.com/pycharm/ on your browser Community Version – Open Source Download and Install Add to Applications Start PyCharm ‣ Open the PyCharm application ‣ Create a New Project MyFirstProject MyFirstProject Create a Python File Write a Run a Python Program Algorithms to Programs Basic Python statements Learning Python - Answer ‣ Write an algorithm to display a message. ‣ Algorithm 1. Start 2. Write “Hello, How are you?” 3. Stop Learning Python ‣ Write an algorithm to display a message. ‣ Algorithm 1. Start 2. Write “Hello, How are you?” 3. Stop ‣ Python # Print a String def main(): print("Hello how are you?”) main() Learning Python ‣ Write an algorithm to add two numbers, 8 and -2 ‣ Algorithm 1. Start 2. Write 8 + (-2) 3. Stop Learning Python - Answer ‣ Write an algorithm to add two numbers, 8 and -2 ‣ Algorithm 1. Start 2. Write 8 + (-2) 3. Stop ‣ Python # Addition def main(): print(8 + (-2)) print("8 + (-2)") main() Learning Python ‣ Write an algorithm to ask someone’s name and greet her/him. ‣ Algorithm 1. Start 2. Read name 3. Write “Hello ” + name + “ nice to meet you.” 4. Stop Learning Python - Answer ‣ Write an algorithm to ask someone’s name and greet her/him. ‣ Algorithm 1. Start 2. Read name 3. Write “Hello ” + name + “ nice to meet you.” 4. Stop ‣ Python # Reading user input def main(): name = input("Enter name: ") print("Hello “ + name + “ nice to meet you.”) main() Learning Python ‣ Write an algorithm to add any two numbers given by user. ‣ Algorithm 1. Start 2. Write “Enter first number: ” 3. Read number1 4. Write “Enter second number: ” 5. Read number2 6. result = number1 + number2 7. Write “Sum is: ” + result 8. Stop ‣ Python ‣ # Add two numbers given by user Learning Python ‣ Write an algorithm to determine and display the square and cube of a number. ‣ Algorithm 1. Start 2. Write “Enter a number: ” 3. Read number1 4. square = number1 * number1 5. cube = number1 * number1 * number1 6. Write “Square is: ” + square 7. Write “Cube is: ” + cube 8. Stop ‣ Python ‣ # Program to find square and cube of a user given number Questions? Thanks