COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot Koffman, http://www.aw.com/cssupport Course lecturer: Assoc. Prof. Stoyan Bonev, PhD
Lecture 2: Introduction to Computers, Problem Solving and Programming (continued)
Lecture Contents: Software Development Method (Software Life Cycle) – reminder and further explanations; Illustration of applying IPO model of computing process; Applying the SDM for solving problems based on linear and branch algorithms
Software Development Method – Software Life Cycle – Program Development Cycle Version 1: · Specify the problem requirements; · Analyze the problem; · Design the algorithm to solve the problem; · Implement the algorithm; · Test and verify the completed program; · Maintain and update the program.
Software Development Method – Software Life Cycle – Program Development Cycle Version 2: · Systems Analysis; · Systems Design; · Systems Implementation; · Systems Support.
Software Development Method – Software Life Cycle – Program Development Cycle Version 3: Analyze: Define the problem Design: Plan the solution to the problem Choose the interface: Select text boxes, buttons, etc Code: Translate the algorithm into a Prog Lan like C/C++ or C# or Java or VBasic Test & Debug: Locate and remove any errors in program Complete Documentation: Organize program description
Software Development Method – Software Life Cycle – Program Development Cycle Simplified Version 4 (as in E.Petroutsos, pp28): Decide what the application will do and how it will interact with the user. Design the application’s user interface according to requirements of step 1. Write the actual code behind the events you want to handle.
Software Development Method Problem Analysis - (Correct Problem) Identify data objects Determine Input/Output data Constraints on the problem Design Decompose into smaller problems Top-down design (divide and conquer) Develop Algorithm (Desk check)
Software Development Method Implementation Writing the algorithm Testing Verify the program meets requirements System and Unit test Documentation Key part in the development process
Algorithm definitions Definition 1: A step-by-step problem-solving process in which a solution is arrived at in a finite amount of time Definition 2: A procedure for solving a problem in terms of 1. the actions to be executed, and 2. the order in which these actions are to be executed is called an algorithm. Definition 3: An algorithm is a list of steps for solving a problem.
How to document algorithms? Three “notations” used to document algorithms: • Flowchart • Pseudo code • Hierarchy chart
Flowchart Also named a control-flow diagram Graphically depicts the logical steps to carry out a task and show how the steps relate to each other. Uses a set of graphic symbols to indicate what should happen at any stage in the algorithm.
Flowchart symbols
Flowchart symbols
Flowchart example
Pseudo code Uses English-like phrases with some C/C++ terms to outline the program
Hierarchy charts Show how the different parts of a program relate to each other Hierarchy charts may also be called: Structure charts HIPO (Hierarchy plus Input-Process-Output) charts Top-Down charts VTOC (Visual Table of Contents) charts
Comments When tracing a flow chart, start at the start symbol and follow the flow lines to the end symbol. Testing an algorithm at the design stage is known as desk checking. Flowcharts, pseudo code, and hierarchy charts are program planning tools that are not dependent on the programming language being used.
Applying the Software Development Method Stage: Analyze the Problem Stage: Design the algorithm IPO model of a computing process I - Input P - Process O - Output
Applying the Software Development Method Attention: Analyze the problem and design the algorithm: It is helpful to underline phrases in the problem statement that identify input and output. See the example below: Problem: Compute and display the total cost of apples given the number of kilos of apples purchased and the cost per kilogram of apples.
Applying the Software Development Method Problem: Compute and display the total cost of apples (output) given the number of kilos of apples purchased and the cost per kilogram of apples.
Applying the Software Development Method Problem: Compute and display the total cost of apples (output) given the number of kilos of apples (input-1) purchased and the cost per kilogram of apples.
Applying the Software Development Method Problem: Compute and display the total cost of apples (output) given the number of kilos of apples (input-1) purchased and the cost per kilogram of apples (input-2).
Applying the Software Development Method Problem: Compute and display the total cost of apples (output) given the number of kilos of apples (input-1) purchased and the cost per kilogram of apples (input-2).
Applying the Software Development Method (cont.) Problem input: quantity of apples purchased (in kg) cost per kilo of apples (in $ or in BGN) Problem output: total cost of apples (in $ or in BGN) After specifying inputs and outputs, develop list of formulas, giving relationship between input and output. Abstract form: Total cost = Unit cost * Number of units Detailed, concrete form: Total_cost_of_apples = Cost_per_kg * Kilos_of_apples
Abstract form of the algorithm . Begin Enter Input Data Apply formula or a list of formulas Display Result End
Abstract form of the algorithm 0. Naming memory locations to store input data, intermediate values and final results Begin Enter Input Data Apply formula or a list of formulas Display Result End
Concrete form of an algorithm 0. We need three memory cells: to store two input values and one for the result. We’ll use names QantityOfApples, CostPerKilo, and TotalCost Begin Enter data and store in QuantityOfApples, CostPerKilo TotalCost = QuantityOfApples * CostPerKilo; Display contents of TotalCost End
Applying the Software Development Method Case Study:Converting Miles to Kilometers Problem Your job requires you to study some maps that give distances in kilometers and some that use miles. You prefer to deal in metric measurements. Write a program that performs the necessary conversion.
Applying the Software Development Method Analysis The first step in solving this problem is to determine what you are asked to do. You must convert from one system of measurement to another, but are you supposed to convert from kilometers to miles, or vice versa? The problem states that you prefer to deal in metric measurements, so you must convert distance measurements in miles to kilometers.
Applying the Software Development Method Design The next step is to formulate the algorithm that solves the problem. Begin by listing the three major steps, or sub problems of the algorithm. Implementation To implement the solution, you must write the algorithm as a C++ program. Testing How do you know the sample run is correct?
Problem: Converting Miles to Kilometers Problem formulated: Design an algorithm and develop a program to convert miles to kilometers.
Problem: Converting Miles to Kilometers Underline phrases to identify input and output Design an algorithm and develop a program to convert miles (input) to kilometers (output). Input: miles Output: kilometers
Problem: Converting Miles to Kilometers Develop a formula or a list of formulas, giving relationship between input and output 1 mile is equivalent to 1,609 kilometers 1 mile = 1.609 km const float KMS_PER_MILE = 1.609; #define KMS_PER_MILE 1.609
Problem: Converting Miles to Kilometers Reminder: Abstract form of the algorithm 0. Naming memory locations to store input data, intermediate values and final results Begin Enter Input Data Apply formula or a list of formulas Display Output Result End
Problem: Converting Miles to Kilometers Reminder: Abstract form of the algorithm 0. Naming memory locations to store input data, intermediate values and final results Begin Enter Input Data Apply formula or a list of formulas Display Output Result End Applying naming conventions: we need 2 mnemonic names (identifiers): For the input data we’ll use name miles. For the result value we’ll use name kms.
Problem: Converting Miles to Kilometers Final version of the concrete algorithm 0. Naming conventions: miles for miles, kms for kilometers Begin Enter value for miles (store input data into miles). Calculate kilometers multiplying miles by 1.609 and save result (store into kms) Apply formula: kms = 1.609 * miles; Output Result (contents of kms displayed on screen). End
Problem: Converting Miles to Kilometers Describe the algorithm Converting miles to kilometers using a flowchart Also named a control-flow diagram
Problem: Converting Miles to Kilometers
Problem: Converting Miles to Kilometers Describe the algorithm Converting miles to kilometers using pseudo code Uses English-like phrases with some C/C++ terms to outline the program
Problem: Converting Miles to Kilometers 1. Read the number of miles 2. Set the number of kilometers to 1.609 * miles 3. Display the number of kilometers
Problem: Converting Miles to Kilometers Describe the algorithm Converting miles to kilometers using a hierarchy chart Show how the different parts of a program relate to each other
Problem: Converting Miles to Kilometers
The source text of a C++ program Implementing algorithm Open the handout file: look at page 2 The source text of a C++ program Implementing algorithm CONVERT MILES TO KILOMETERS
Exercise 2.1 Apply the SDM and build a linear algorithm: Problem: Converting feet and inches to meters
Exercise 2.2 Apply the SDM and build a linear algorithm: Problem: To add, subtract, multiply and divide two numeric values
Exercise 2.3 Apply the SDM and build a branch algorithm: Problem: Root (solution) of a linear equation bx + c = 0
Exercise 2.3 Apply the SDM and build a branch algorithm: Problem: Root (solution) of a linear equation bx + c = 0 (single solution x = -c/b, infinite number of solutions, no solution);
Exercise 2.4 Apply the SDM and build a branch algorithm: Problem: Roots (solution) of a quadratic equation ax2 + bx + c = 0 (two distinct solutions, two identical solutions, no real solutions).
Friedman/Koffman, Chapter 01 Before lecture end Lecture: Problem Solving More to read: Friedman/Koffman, Chapter 01 Or Coming approx. 10 slides
Chapter 1: Introduction to Computers, Problem Solving, and Programming Abstraction, and Design using C++ 5e by Frank L. Friedman and Elliot B. Koffman
1.5 Software Development Method Problem Analysis Identify data objects Determine Input / Output data Constraints on the problem Design Decompose into smaller problems Top-down design (divide and conquer) Develop Algorithm (Desk check) Algorithm refinement
Software Development Method Implementation Converting the algorithm into programming language Testing Verify the program meets requirements System and Unit test Maintenance All programs undergo change over time
1.6 Applying the Software Development Method Case Study: Converting Miles to Kilometers Problem Your summer surveying job requires you to study some maps that give distances in kilometers and some that use miles. You and your coworkers prefer to deal in metric measurements. Write a program that performs the necessary conversion.
Data Requirements Problem Input miles distance in miles Problem Output kms the distance in kilometers Relevant Formula 1 mile = 1.609 kilometers
Design Formulate the algorithm that solves the problem. Algorithm 1. Get the distance in miles. 2. Convert the distance to kilometers. 3. Display the distance in kilometers. Algorithm Refinement 2.1 The distance in kilometers is 1.609 the distance in miles Desk check!
Listing 1.2 Miles to kilometers
Implementation #include <iostream> using namespace std; int main( ) { const float KM_PER_MILE = 1.609; float miles, kms; cout << “Enter the distance in miles: “; cin >> miles; kms = KM_PER_MILE * miles; cout << “The distance in kilometers is “ << kms << endl; return 0; }
Testing Test with input data for which you can easily determine the expected results E.g. 10 miles should convert to 16.09 kilometers
Thank You For Your Attention