Download presentation
Presentation is loading. Please wait.
Published byJayson Walker Modified over 9 years ago
1
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 1 Chapter 2 Developing a Program
2
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 2 2.1The 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
3
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 3 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?
4
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 4 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.
5
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 5 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.
6
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 6 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.
7
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 7 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
8
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 8 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 PrincipalSet Rate = PercentageRate / 100 FinalValue PercentageRate Compute final value of investment TermSet FinalValue = Principal * Frequency(1 + Rate / Frequency) ^ (Frequency * Term)
9
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 9 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
10
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 10 Example of Modules in Pseudocode Main module Display program title and brief description of program Call Input Data Module Call Perform Calculations module Call Output Results Module End Program Input Data module Prompt for Principal, PercentageRate, Term, Frequency Input Principal, PercentageRate, Term, Frequency End module Perform Calculations module Set Rate = PercentageRate / 100 Set FinalValue = Principal * (1 + Rate / Frequency) ^ (Frequency * Term) End module Output Results Module Write Principal, PercentageRate, Term, Frequency Write FinalValue End module
11
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 11 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.
12
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 12 Hierarch Chart for Brewster Example Main Module Input Data Perform Calculations Output Results
13
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 13 2.3Coding, 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
14
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 14 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.
15
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 15 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.
16
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 16 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
17
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 17 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.
18
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 18 The Flowchart for Brewster’s Thousands Ref: p52 Venit Start Input Principal, Rate, Term, Frequency End Compute Final Value = Principal * (1 + Rate/Frequency) ^ (Frequency * Term) Output FinalValue
19
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 19 C++ Coding for Brewster’s Thousands // C++ program - by M. Wojciechowski #include using namespace std; int main() { int PercentageRate, Term, Frequency; float Rate, Principal, FinalValue; // Input Datat cout << “Enter the interest rate: “; cin >> PercentageRate : cout << “Enter the principal: “; cin >> Principal : cout << “Enter the Term: “; cin >> Term : // Problem Calculation Rate = PercentageRate / 100; FinalValue = Principal * pow(1 + Rate/Frequency, Frequency*Term); // Output Data cout << “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; }
20
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 20 Test Data PrincipalPercentage Rate FrequencyTermRateFinal Value 10001246 Input Data Output Data
21
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 21 C++ Compiler Program 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
22
Chapter 2: Developing a Program Prelude to Programming Concepts and Design Copyright © 2001 Scott/Jones, Inc.. All rights reserved. 22 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.