# Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.

## Presentation on theme: "Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing."— Presentation transcript:

Algorithms

Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing a program: –Know your “building blocks” (e.g. data types, data structures, control structures, selection structures, etc) –Use good programming principles for efficiency and correct results

Pseudocode What is pseudocode? –Artificial, informal language that helps us develop algorithms –Similar to everyday English –Not actually executed on computers –Helps us “think out” a program before writing it Converts into a corresponding program Consists only of executable statements

Formulating Algorithms with Top-Down, Stepwise Refinement First, define the problem in words: For palindromes, the problem becomes: What are the steps necessary to test the string?

Formulating Algorithms with Top-Down, Stepwise Refinement, Step 2: Top-down, stepwise refinement –Begin with a pseudocode representation of the problem: Determine whether the string is a palindrome –Divide the problem into smaller tasks and list them in order: Initialize variables and arrays Input the string Test the string Return the result Many programs have four phases: –Initialization: declare & initialize all program variables –Input: input data values and adjust any program variables –Processing: manipulate the data –Termination: calculate and print the final results (output)

Formulating Algorithms with Top-Down, Stepwise Refinement, Step 3: Refine the initialization phase from Initialize variables to: set any counters to zero determine the data type & length of the array and declare it Refine test the string to Check first and last elements While the first and last elements are the same, continue If they differ, exit and the string is not a palindrome Otherwise, test until all pairs are matched

Formulating Algorithms with Top-Down, Stepwise Refinement, Step 4: Refine Print the result to If the string is a palindrome Print “The string … is a palindrome.” else Print “The string … is not a palindrome.”

Download ppt "Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing."

Similar presentations