Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.

Slides:



Advertisements
Similar presentations
Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264.
Advertisements

Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Writing a Good Program 6. Pointers and Arrays
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
1 Writing a Good Program 5. Objects and Classes in C++
Computer Science 1620 Programming & Problem Solving.
1 Writing a Good Program 5. Objects and Classes in C++
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Programming Introduction to C++.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
1 ENG236: ENG236: C++ Programming Environment (2) Rocky K. C. Chang THE HONG KONG POLYTECHNIC UNIVERSITY.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Introduction to C++ Programming Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides.
1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
Chapter 5: Control Structures II (Repetition)
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Chapter 6: Functions Starting Out with C++ Early Objects
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Describe the Program Development Cycle. Program Development Cycle The program development cycle is a series of steps programmers use to build computer.
CONDITIONAL STATEMENTS OVERVIEW.  Many times we want programs to make decisions  What drink should we dispense from the vending machine?  Should we.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
CPS120: Introduction to Computer Science Decision Making in Programs.
30/10/ Iteration Loops Do While (condition is true) … Loop.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
C++ Classes and Data Structures Jeffrey S. Childs
The Software Development Process
1 CS161 Introduction to Computer Science Topic #9.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
User-Written Functions
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Introduction to C++ October 2, 2017.
Mr. Shaikh Amjad R. Asst. Prof in Dept. of Computer Sci. Mrs. K. S
Computing Fundamentals
Programming Introduction to C++.
CS 144 Advanced C++ Programming January 31 Class Meeting
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007

Computer Programming and Basic Software Engineering 4. Basic Software Engineering Design a Structured Program

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 3 Seldom do we have a program worked the first time it is written Need debugging  To find out and correct the errors in a program May need many times of iteration for a large program To reduce the number of iteration, need a structural method for program development.

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 4 Debugging a big program is exponentially more difficult than a small program. If a big program is to be developed, always try to break it down to many small programs and debug them independently.  Divide and Conquer Difficulty in debugging Program size

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 5 int main() { : } int main() { : } int main() { : } int function1() { : } int function2() { : } int main() { : } int function1() { : } int function2() { : } It is a very bad habit to write a program in main() only  since it makes the program very big. Always break it down by calling functions from main(). 

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 6 Usually main() is only used to indicate the program flow. The actual task is done by the functions called by main(). By looking at the sequence of functions called by main(), people basically understand what is going on in a program. It gives the skeleton of a program.

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 7 #include using namespace std; bool menu(); //function prototype int main() {bool exitflag = false; //flag is bool while (exitflag == false) {exitflag = menu(); } return 0; } In the example program below, people expect this program will repeatedly show a menu. If interested in the details of the menu, he can further read the function menu().

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 8 Improve the Readability of Programs In practice, the time a programmer spends in documentation is more than writing the program. A real program is never developed by a single person, co-operations are needed between programmers or even teams of programmers. Besides, a program developed by a team of programmers often needs to be maintained by another team. It is essential that a program is properly documented to allow team work.

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 9 Documenting a Program - readme After developing a program, a readme file is often prepared in practice to indicate the following: Background information of the program (e.g. objectives, version no., development date, developer name) How to use the program (e.g. command line options) Additional resource required (e.g. hardware, driver, etc.) Bug fixed as compared to previous version(s) Possible conflict with other programs.

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 10 Within a program - comments Commenting a program is the responsibility of every programmer. Need meaningful comments.  Not explaining something people know already, but something people will have difficulty to understand.

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 11 // The main function int main() { cout << "Hello World!\n"; // cout Hello World return 0; // return a number 0 } Totally meaningless comment! For example

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 12 // It is to demonstrate the basic structure of a C++ // program // Usage: HelloWorld int main() { cout << "Hello World!\n"; // print the string Hello World! on screen return 0; // Indicate to the system the execution is // successful } Tell something hidden in the program! See the lines for comments are more than the codes

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 13 How to comment? At the beginning of each program, the following comments are often required: Background information of the program (Objectives, version no., development date, developer name, etc.) Usage (e.g. command line options) Additional resource required (e.g. if any other projects/files are required for compiling this program, e.g. header files)

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 14 How to comment? At the beginning of each function, the following information is needed Objective of this function Other functions that it will call Passed parameters Return parameters Global variables that have been made use of.

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 15 How to comment? Inside a function, the following information is needed The use of every local variable Explanation of any tricky part that other people may have difficulty in understanding.

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 16 Case study – Developing a program step-by-step Write a program that repeatedly asks the user to select one of the following three options: 1. On choosing ‘ a ’, ask the user to input a series of positive numbers and show the mean of them; 2. On choosing ‘ b ’, ask the user to input a series of positive numbers and show the variance of them; 3.On choosing ‘ q ’, quit the program.

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 17 Step 0 – Prepare a flowchart Start Yes No A Ask user to choose a, b or q To better visualize the problem, we may develop a flowchart for it Yes No D Yes C B End User chooses a ? User chooses b ? User chooses q ? Skeleton Menu

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 18 A D C B Input integers and calculate the mean Input integers and calculate the variance Generate error message

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 19 // This program is to compute the mean or variance // of a series of positive numbers input by user // Usage: Mean_Var // Version: 1 // Date: Feb 14, 2006 // Author: Frank #include using namespace std; bool menu();// Show a menu to user and ask for input int main() {bool exitflag = false; // A flag to indicate if user wants to quit while (exitflag == false) {exitflag = menu(); // If user chooses 'q', menu() returns true } return 0; } Step 1 – construct the skeleton Flow Chart

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 20 Step 2 – Add stubs (i.e. just for testing) The above program cannot be compiled and executed since the required function menu() has not be implemented We need to ensure the skeleton is correct before we proceed to implement the functions – add stubs // A stub // Input parameter: Nil // Return parameter: Just a constant bool menu() { return true; //Loop 1 time } Just enough for the program skeleton to compile and execute

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 21 // This program is to compute the mean and variance // of a series of positive numbers input by user // : #include using namespace std; bool menu();// Show a menu to user and ask for input int main() {: : return 0; } // A stub // Input parameter: Nil // Return parameter: Just a constant bool menu() {return true; } So the whole program at this stage is just like that

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 22 Step 3 – Implement Functions If the skeleton has been proved to be correct, start to implement functions. The original comments may need to be updated when any change is made to the original codes as a result of the implementation of functions. Similar to developing main(), it is desirable to further break a big function into smaller functions. Add stubs again when needed.

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 23 // Show a menu to user and ask for input // Input: Nil // Return: true if user chooses 'q', otherwise false void average(char); // Ask for a series of numbers and // compute their mean or variance bool menu() { char choice; // To store the command of user cout<<"[a] Mean\n"<<"[b] Variance\n"<<"[q] Quit\n"; cout << "Please enter your choice: "; cin >> choice; switch (choice) { case 'a': average('a'); break; case 'b': average('b'); break; case 'q': return true; // Shall quit menu() here default : cout << "Wrong input!\n"; break; } return false; } Flow Chart

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 24 // A stub // Input parameter: char command – // if 'a', do mean; if 'b', do variance // Return parameter: Nil void average(char command) { } menu() cannot be compiled or executed since average() has not been implemented To ensure menu() is correct, we need to compile and execute it. To solve the problem, a stub is added for average():

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 25 Step 4 – Implement the sub-functions If the major functions have been proved to be correct, start to implement sub-functions The original comments may need to be updated when any change is made to the original codes as a result of the implementation of functions. Similar to developing menu(), it is desirable to further break a big function into smaller functions. Add stubs again when needed. It is desirable to have each function contained at most 20 lines of codes. Divide and conquer average().

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 26 // Get positive nos., find mean or variance // Input parameter: char command - // 'a'-> mean; 'b'-> variance // Return parameter: Nil void average(char command) { double input = 0;// Store data input by user double sum = 0; // Store temporary sum int count = 0; // Count no. of data entered while (input >= 0) // If negative no., quit {cout << "Please enter your data: "; cin >> input; if (input < 0) continue; // Leave average() on -ve input count+=1; if (command == 'a') // calculate and show the current mean { sum = sum + input; cout<<"\n\nThe current mean is "<<sum/count<<endl; } else // calculate and show the current variance { sum = sum + input*input; cout<<"\n\nThe current variance is "<<sum/count<<endl; } // it is in fact not the real variance } }

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 27 Exercise 4.1 By following the steps as described above, you are requested to develop the skeleton of a personnel database program. The program should be a console application that repeatedly asks users to do one of the following in the main menu: a. Enter/modify record b. Show all records q. Quit

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 28 For every user command input, call a function. You don’t need to implement the details of the functions, just do the following: If user chooses ‘ a ’, show a message “ Please enter record: ” call a function and get back to the main menu. If user chooses ‘ b ’, show a message “ All records are shown below ” call a function and get back to the main menu. If user chooses ‘ q ’, show a message “ Thank you. Goodbye!!! ” and then the program will quit. Make sure your program follows the style as shown in the case study (Do NOT use main() to do everything) Make sure your program is commented appropriately using the method as described above.

Computer Programming and Basic Software Engineering 4. Basic Software Engineering 29 Acknowledgment The slides are based on the set developed by Dr. Frank Leung (EIE).