120 likes | 244 Vues
The Design (Algorithm). Do this repeat if something then do this else do the other Print “all done”. DESIGN. Reduce cognitive load separating design and coding using top-down design. IMPLEMENT. From problem to program. In “real world”…. Problem in Natural language
E N D
The Design (Algorithm) Do this repeat if something then do this else do the other Print “all done” DESIGN • Reduce cognitive load • separating design and coding • using top-down design IMPLEMENT From problem to program • In “real world”… • Problem in Natural language • Top Down Design in pseudo-code • Program in computer code(code in almost any computer language.) The Problem Requirements: A program that does this and that and theother. It must take data fromthis source and The Program BBS 514 - Yapısal Programlama (Structured Programming)
Steps of Software Development • Problem Understanding • Read the problem carefully and try to understand what is required for its solution. • Analysis • Identify problem inputs and outputs. • Design • Develop a list of steps (algorithm) to solve the problem • Refine steps of this algorithm. (Divide and Conquer) • Verify that the algorithm solves the problem, i.e. the algorithm is correct. • Implementation • Implement the algorithm as a (java) program. • You have to know a specific programming language (java) • Convert steps of the algorithm into programming language statements. • Testing and Verification • Test the completed program, and verify that it works as expected. • Use different test cases (not one) including critical test cases. BBS 514 - Yapısal Programlama (Structured Programming)
Algorithm • An algorithm is a design written in pseudo code. (in English, but a formal style) • Algorithms contains: • Sequence • Decision • Repetition BBS 514 - Yapısal Programlama (Structured Programming)
Everyday Algorithms Problem: To drink a glass of milk. Refine step 3: 1. Open the refrigerator 2. Get the milk 3. Close the refrigerator • Algorithm: • Enter the kitchen • Get a glass • Get the milk from the refrigerator • Fill the glass with milk • Drink it Refine step 4: 1. while the glass is not full 1.1. Pour some milk into the glass Refine step 1: 1. Walk to the kitchen door 2. If the door is closethen 2.T. Open it 3. Walk into the kitchen BBS 514 - Yapısal Programlama (Structured Programming)
Area-Circumference Problem Requirements: Design an algorithm to find and report the area and circumference of a circle whose radius the user gives. Example interaction Welcome to circle computer… Please enter the radius: 5 The area of a circle of radius 5 is 78.55 and its circumference is 31.42 BBS 514 - Yapısal Programlama (Structured Programming)
Area-Circumference Problem To find area & circumference of circle… • Print welcome message • Ask for & get radius from user • Compute area as pi.radius.radius • Compute circumference as 2.pi.radius • Report area, circumference & radius Once we are sure that this is correct, move on to solve any non-trivial sub-problems. Identify preconditions & ensure they are satisfied. BBS 514 - Yapısal Programlama (Structured Programming)
Area-Circumference Problem (cont.) Solve… 2. Ask for & get radius from user • Ask (prompt) the user to enter radius • Get radius value from user Solve… • Report area, circumference & radius • Print msg “The area of a circle with radius ” • Print radius value • Print msg “ is ” • Print area value and move to next line • Print msg “ and its circumference is ” • Print circumference value • Print blank line BBS 514 - Yapısal Programlama (Structured Programming)
Calculating the average score of an exam Problem: Calculating the average score of an exam Inputs : Scores of an exam Output: Average score of the exam Algorithm: 1. Set count to zero 2. Set sum to zero 3. while there is an exam sheet do 3.1. Read the score on the sheet 3.2. Add the score on the sheet to sum 3.3. Add one to count 3.4. Move to the next sheet 4. Compute average as sum/count 5. Display average BBS 514 - Yapısal Programlama (Structured Programming)
Prime Number Problem: Deciding that a given integer number is prime or not. Input : An integer number N Output: A sentence indicating that N is prime or not. Algorithm: 1. Read the number N 2. Test whether N is prime or not 3. Display the result. Refine step 3: 1. ifPrimeFlag = truethen 1.T. Display that N is prime else 1.F. Display that N is not prime Refine step 2: 1. set counter to 2 2. set PrimeFlag to true 3. while (counter < N and PrimeFlag = true) do 3.1. ifN is divisible by counterthen 3.1.T. set PrimeFlag to false 3.2. Increment counter by 1 BBS 514 - Yapısal Programlama (Structured Programming)
Factorial Problem: Calculating N! Inputs: A positive integer N Output: N! Algorithm: 1. Read a positive integer N 2. Calculate NFactorial 3. Display NFactorial Refine step 2: 1. Set NFactorial to 1 2. Set counter to 2 3. while (counter <= N) do 3.1. Multiply NFactorial by counter 3.2. Increment counter by 1 BBS 514 - Yapısal Programlama (Structured Programming)
Sequence Decision • step • step • step • step n. if condition then nT step else nF step Any step can be replaced with one of the other types indent Repetition n. while condition do step n. do step while condition n. for so many times do step indent indent Types & Layout (of algorithm steps) BBS 514 - Yapısal Programlama (Structured Programming)