Chapter 2 - Control Structures - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 2 - Control Structures

Description:

For palindromes, the problem becomes: What are the steps necessary to test the string? ... Determine whether the string is a palindrome ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 8
Provided by: kal772
Category:

less

Transcript and Presenter's Notes

Title: Chapter 2 - Control Structures


1
Algorithms
2
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

3
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

4
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?

5
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)

6
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

7
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.
Write a Comment
User Comments (0)
About PowerShow.com