Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001...

22
Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 1 Chapter 2: Developing a Program Prelude to Programming Prelude to Programming Concepts and Design Concepts and Design Chapter 2 Chapter 2 Developing a Program Developing a Program

Transcript of Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001...

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

1

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

Chapter 2Chapter 2

Developing a ProgramDeveloping a Program

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

2

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

2.1 The Program Development Cycle

• Problem solving principles– Completely understand the

problem

– Devise a plan to solve it

– Carry out the plan

– Review the results

• Writing a program– 1) Analyze the problem

– 2) Design the program

– 3) Code the program

– 4) Test the program

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

3

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

1) Analyze the Problem

• Brewster’s Thousands– The problem: Brewster wants to invest money at a

local bank. There are many options such as interest rates, terms of deposit, compounding frequencies. He needs a program to compute, for any given initial investment, the final maturity (value) of the deposit.

• What are the inputs? (given data)

• What are the outputs? (required data)

• How will we calculate the required outputs from the given inputs?

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

4

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

2) Design the Program

Create an outline of the program• An algorithm – a step by step procedure that will

provide the required results from the given inputs.

– Algorithms in real life: Instructions on how to make a cake, use the bank’s ATM, etc.

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

5

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

3) Coding the Program

• Once the design is completed, write the program code.

• Code is written in some programming language such as BASIC, Pascal, C++, Java, etc.

• In Vendit, we write code in pseudo-code, developing the skills to be used when studying the specific languages.

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

6

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

4) Testing the program

• Locate any errors (bugs)• Testing is done throughout the development cycle• Desk-checking, or code walkthrough is

performed to locate errors in the code.– Pretend you are the computer and execute your own

code.

• Ultimate test is to run the program to see if the outputs are correct for the given inputs.

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

7

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

2.2 Program Design

• Modular programming– Determine the major tasks that the program must

accomplish. Each of these tasks will be a module.

– Some modules will be complex themselves, and they will be broken into submodules, and those submodules may also be broken into even smaller modules.

– This is called top-down design

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

8

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

The first illustration of top down design describes the 3 fundamental tasks that are required in the Brewster example:

– Input

– Perform Calculations (Process)

– Output

Input Perform Calculations Output

Input variables: Compute Rate of interest Display

Principal Set Rate = PercentageRate / 100 FinalValue

PercentageRate Compute final value of investment

Term Set FinalValue = Principal *

Frequency (1 + Rate / Frequency) ^ (Frequency * Term)

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

9

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

A Code Module

• Performs a single task

• Is self-contained and independent of other modules

• Relatively short – less than 1 page

• Calling module

• Called module

• Transfer of control

• Main is the controller of all sub-modules

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

10

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

Example of Modules in PseudocodeMain module

Display program title and brief description of programCall Input Data ModuleCall Perform Calculations moduleCall Output Results ModuleEnd Program

Input Data modulePrompt for Principal, PercentageRate, Term, FrequencyInput Principal, PercentageRate, Term, FrequencyEnd module

Perform Calculations moduleSet Rate = PercentageRate / 100Set FinalValue = Principal * (1 + Rate / Frequency) ^ (Frequency * Term)End module

Output Results ModuleWrite Principal, PercentageRate, Term, FrequencyWrite FinalValueEnd module

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

11

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

Hierarchy Chart

• Like an organization chart – shows position of modules in the program.

• Depicts what modules exist and how they are related.

• Large programs need a “map” for documentation.• One page of code per module – keeps the program

manageable.• We will have very small modules while getting

comfortable using these tools.

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

12

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

Hierarch Chart for Brewster Example

Main Module

Input Data

Perform Calculations

Output Results

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

13

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

2.3 Coding, Documenting, and Testing

• Coding – done in a specific programming language. We will use

pseudocode.

– should only begin after a solid design exists.

• Documenting– Code needs to contain documentation that describes to

the reader what the code is doing – called comments

– This kind of documentation is for the programmers to read

– There is also User Documentation that is external to the code and may take the form of a User’s Guide

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

14

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

• Testing– Create test data that will be used to check the program’s

correctness. For example, if the following test data is used in the Brewster example, does the program generate the expected results? (Determine the expected results before the code is written.)

• Principal = 1000• PercentageRate = 12• Frequency = 4• Term = 6

If a Desk check of the code gets the same answer as the expected results then the code has been tested, and is likely correct.

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

15

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

Types of Errors

• Syntax – wrong grammar, i.e., breaking the rules of how to write the language– Forgetting punctuation, misspelling keyword

– The program will not run at all with syntax errors

• Logic - the program runs, but does not produce the expected results.– Using an incorrect formula, incorrect sequence of

statements, etc.

– These errors are detected during the desk-check, of the program; an important part of the cycle.

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

16

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

2.4 Structured Programming

• A method for designing and coding programs in a systematic, organized manner.

• It combines the principles of top-down design, modularity and the use of the three accepted control structures of sequence, repetition and selection.

• Sequence, repetition and selection can be expressed in pseudocode, or with Flowcharts

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

17

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

Flowcharts

• Another tool for programmers to design programs– Describe the flow of a program module’s execution

with diagrams.

– (Completely different from hierarchy charts).

– Connected symbols are used to describe sequence, repetition, and selection.

– Some prefer to use flow charting to learn how to express algorithms, and others prefer to use pseudocode.

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

18

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

The Flowchart for Brewster’s Thousands

Ref: p52 Venit

Start

Input Principal, Rate,Term, Frequency

End

ComputeFinal Value = Principal *

(1 + Rate/Frequency) ^ (Frequency * Term)

Output FinalValue

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

19

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

C++ Coding for Brewster’s Thousands// C++ program - by M. Wojciechowski#include <iostream>#include <cmath>

using namespace std;

int main(){

int PercentageRate, Term, Frequency; float Rate, Principal, FinalValue;

// Input Datatcout << “Enter the interest rate: “;cin >> PercentageRate :cout << “Enter the principal: “;cin >> Principal :cout << “Enter the Term: “;cin >> Term :

// Problem CalculationRate = PercentageRate / 100; FinalValue = Principal * pow(1 + Rate/Frequency , Frequency*Term);

// Output Datacout << “Given the principal is “ << Principal << “ and interest rate is “ << Rate

<< “ and the term is “ << Term << “ and the interest is paid “ << Frequency << “ times per year.” << endl;

cout << “Final value is “ << FinalValue << endl;

return 0;}

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

20

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

Test Data

Principal Percentage

Rate

Frequency Term Rate Final

Value

1000 12 4 6

Input Data Output Data

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

21

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

C++ CompilerProgram Editor

Executable Program

Linkage

C++ Compiler

Preprocessor

Hwk1.cpp

Produce an object file Hwk1.obj

link all the object file in order to produce an executable program

Hwk1.exe

Copyright © 2001 Scott/Jones, Inc..All rights reserved.

22

Chapter 2: Developing a Program

Prelude to ProgrammingPrelude to ProgrammingConcepts and DesignConcepts and Design

Control Structures

• SequenceSequence –in sequential order. – The simplest of control structures – start at the

beginning and continue in sequential order.

• RepetitionRepetition – repeat statements more than once– Called a loop, it needs a stop condition, I.e, the

program will continue to loop until some condition is met.

• SelectionSelection – selectively execute statements– Called a branch, it requires a condition to determine

when to execute statements.