Chapter 2: Developing a Program Chapter 2 Developing a Program Prelude to Programming Concepts and Design 1 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program 2.1 The Program Development Cycle • Problem solving principles • Writing a program – Completely understand the problem – Devise a plan to solve it – Carry out the plan – Review the results Prelude to Programming Concepts and Design – 1) Analyze the problem – 2) Design the program – 3) Code the program – 4) Test the program 2 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program 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? Prelude to Programming Concepts and Design 3 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program 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. Prelude to Programming Concepts and Design 4 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program 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. Prelude to Programming Concepts and Design 5 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program 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. Prelude to Programming Concepts and Design 6 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program 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 Prelude to Programming Concepts and Design 7 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program 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 Input variables: Principal PercentageRate Term Frequency Perform Calculations Output Compute Rate of interest Display Set Rate = PercentageRate / 100 FinalValue Compute final value of investment Set FinalValue = Principal * (1 + Rate / Frequency) ^ (Frequency * Term) Prelude to Programming Concepts and Design 8 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program 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 Prelude to Programming Concepts and Design 9 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program 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 Prelude to Programming Concepts and Design 10 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program 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. Prelude to Programming Concepts and Design 11 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program Hierarch Chart for Brewster Example Main Module Input Data Prelude to Programming Concepts and Design Perform Calculations 12 Output Results Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program 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 Prelude to Programming Concepts and Design 13 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program • 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. Prelude to Programming Concepts and Design 14 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program 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. Prelude to Programming Concepts and Design 15 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program 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 Prelude to Programming Concepts and Design 16 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program 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. Prelude to Programming Concepts and Design 17 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program The Flowchart for Brewster’s Thousands Start Input Principal, Rate, Term, Frequency Compute Final Value = Principal * (1 + Rate/Frequency) ^ (Frequency * Term) Output FinalValue Ref: p52 Venit Prelude to Programming Concepts and Design End 18 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program 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 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; } Prelude to Programming Concepts and Design 19 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program Test Data Output Data Input Data Principal Percentage Rate Frequency Term 1000 12 4 Prelude to Programming Concepts and Design Rate Final Value 6 20 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program C++ Compiler Hwk1.cpp Program Editor Preprocessor C++ Compiler Produce an object file Hwk1.obj link all the object file in order to produce an executable program Linkage Hwk1.exe Executable Program Prelude to Programming Concepts and Design 21 Copyright © 2001 Scott/Jones, Inc.. All rights reserved. Chapter 2: Developing a Program Control Structures • Sequence –in sequential order. – The simplest of control structures – start at the beginning and continue in sequential order. • Repetition – 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. • Selection – selectively execute statements – Called a branch, it requires a condition to determine when to execute statements. Prelude to Programming Concepts and Design 22 Copyright © 2001 Scott/Jones, Inc.. All rights reserved.