2.1 Designing a Program Notes

Program Development Cycle

  • Designing a program is essential before writing code; you should not jump straight into coding.
  • Tools used during design: pseudocode and flowcharts to create models of programs.
  • The program development cycle consists of five phases (as shown in figure 2.1):
    • Design the program
    • Write the code
    • Correct syntax errors
    • Compile/translate or run the program (via an interpreter, depending on the language)
    • Test the program and correct logic errors
  • Key idea: even with high-level languages like Python, the process involves more than just coding; you must ensure the program works correctly through design, implementation, and testing.

Design the program

  • A program should be carefully designed before code is written.
  • Professional programmers discuss several design techniques they will use later in this section to design Python programs.

Write the code

  • After designing, write code in a high-level language (e.g., Python).
  • Each language has its own syntax rules that must be followed.
  • Syntax rules govern how keywords, operators, and punctuation characters can be used.
  • A syntax error occurs if these rules are violated.

Correct syntax errors

  • Most code has syntax errors when first written; programmers spend time correcting them.
  • Once syntax errors and simple typing mistakes are corrected, the program can be executed or compiled.

Compile/execute

  • After syntax is correct, the program is compiled to machine language or executed by an interpreter, depending on the language.

Test the program

  • Testing determines whether logic errors exist—errors that do not stop execution but produce incorrect results.
  • Mathematical mistakes are a common source of logic errors.

Correct logic errors

  • If incorrect results are produced, the programmer debugs the code to fix logic errors.
  • Sometimes debugging reveals that the original design must be changed.
  • If necessary, the program development cycle restarts and continues until no errors remain.

The Design Process: Two core steps

  • The design process is the most important part of the cycle; it is like building a solid foundation.
  • A poorly designed program will require more fixes later.
  • The design process can be summarized in two steps:
    • Understand the task the program is to perform.
    • Determine the steps that must be taken to perform the task.

Understand the task the program is to perform

  • You must understand what the program is supposed to do before determining the steps.
  • Usually, a professional programmer gathers this understanding by working directly with the customer.
  • Customer: the person, group, or organization that asks you to write the program (the buyer or requester).
  • In practice, this often means interviewing the customer to learn the task details.
  • A follow-up interview is usually needed because not everything is mentioned in the initial meeting.
  • The programmer studies the gathered information and creates a list of software requirements.
  • A software requirement is a single task the program must perform to satisfy the customer.
  • When the customer agrees that the requirements are complete, the programmer moves to the next phase.
  • Tip: In programming class, your customer is your instructor; understand your instructor’s requirements and write your programs accordingly.

Determine the steps that must be taken to perform the task

  • After understanding the task, break it down into a series of steps that can be followed.
  • This is analogous to giving someone instructions on how to perform a task (e.g., how to boil water).
  • Example task: boil water.
    • Step 1: Pour the desired amount of water into a pot.
    • Step 2: Put the pot on a stove burner.
    • Step 3: Turn the burner to high.
    • Step 4: Watch the water until you see large bubbles rapidly rising; when this happens, the water is boiling.
  • This breakdown is an example of an algorithm: a set of well-defined, logically ordered steps that must be taken to perform a task.
  • For a programming task, you would create an algorithm listing all the logical steps required.
  • Example algorithm: calculate and display the gross pay for an hourly paid employee.
    • Step 1: Get the number of hours worked
    • Step 2: Get the hourly pay rate
    • Step 3: Multiply the number of hours worked by the hourly pay rate
    • Step 4: Display the result of the calculation
    • The calculation can be expressed as extGrossPay=extHoursWorkedimesextPayRateext{GrossPay} = ext{HoursWorked} imes ext{PayRate}
  • Note: The steps above are not yet ready to execute on a computer; they must be translated into code.

Tools to translate design into code: Pseudocode and Flowcharts

  • Programmers use pseudocode and flowcharts to translate algorithms into executable programs.

Pseudocode

  • Pseudocode is informal, has no syntax rules, and is not meant to be compiled or executed.
  • It helps focus on design without worrying about syntax errors.
  • Once the pseudocode design is satisfactory, it can be translated directly into actual code in a language like Python.
  • Example pseudocode for the pay calculation program:
    • Input the hours worked
    • Input the hourly pay rate
    • GrossPay = HoursWorked * PayRate
    • Display GrossPay
  • Each line in the pseudocode corresponds to an operation that can be implemented in Python (e.g., reading input, performing calculations, displaying output).

Flowcharts

  • Flowcharts graphically depict the steps in a program.
  • They help visualize the flow of control and data through the program.
  • There are three common symbols in the example flowchart:
    • Ovals: Terminal symbols (start and end points)
    • Parallelograms: Input and output steps (read input or display output)
    • Rectangles: Processing steps (perform a computation or data processing)
  • Arrows connect the symbols and indicate the flow of the program from start to end.
  • How to read a flowchart: start at the Start terminal, follow arrows through the symbols in order, until you reach the End terminal.

2.1 Non-interactive checkpoint questions (from the book)

  • 2.1 Who is a programmer's customer?
    • The customer is the person, group, or organization that requests and relies on the program to perform an important task; in coursework, this is often the instructor or a client.
  • 2.2 What is a software requirement?
    • A single task that the program must perform to satisfy the customer.
  • 2.3 What is an algorithm?
    • A set of well-defined, logically ordered steps that must be taken to perform a task.
  • 2.4 What is pseudocode?
    • An informal, non-executable representation of a program design used to plan the algorithm before coding.
  • 2.5 What is a flowchart?
    • A diagram that graphically depicts the steps and flow of a program.
  • 2.6 What do each of the following symbols mean in a flowchart?
    • Oval: Start/End (terminal symbols)
    • Parallelogram: Input/Output (read input or display output)
    • Rectangle: Processing (a computation or data manipulation)