Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
M O D U L E 2: F U N D A M E N TA L O F P R O G R A M M I N G C O N C E P T S - P A R T I C I PA N T G U I D E
Page 1 of 25
Programming Techniques
TABLE OF CONTENTS Module Introduction...........................................................................................................4 Module Objectives.............................................................................................................5 Source Material .................................................................................................................6 Presentation: 2.1 Fundamental Programming concepts...................................................7 Algorithm ....................................................................................................................7 Overview.................................................................................................................7 Benefits of Algorithm...............................................................................................8 Analyzing an Algorithm...........................................................................................8 Algorithm Complexities...........................................................................................9 Structure of an Algorithm......................................................................................11 Basic three algorithm concepts.............................................................................11 Sequential Algorithm.............................................................................................12 Selection ..............................................................................................................12 Iteration ................................................................................................................13 Flowcharts ...............................................................................................................14 Overview...............................................................................................................14 Symbols used in Flow chart..................................................................................14 Flowcharts: Advantages and Disadvantages.......................................................15 Flowcharts: Limitations.........................................................................................16 Testing......................................................................................................................16 Overview...............................................................................................................16 Need of testing......................................................................................................16 Unit testing ...........................................................................................................16 Test Cases............................................................................................................17 Key points.................................................................................................................18 Activity 1 ..........................................................................................................................18 Activity Overview......................................................................................................18 Activity Instructions...................................................................................................19 Activity 1.0.............................................................................................................19 Activity 1.1.............................................................................................................19 Activity 1.2.............................................................................................................19 Activity 1.3.............................................................................................................19 Activity 1.4.............................................................................................................19 Presentation: 2.2 Programming Life Cycle......................................................................20 Top-down Design......................................................................................................21 Bottom-up Design.....................................................................................................22 Structured programming...........................................................................................22 Program Life Cycle...................................................................................................23 Key points.................................................................................................................24 Activity 2...........................................................................................................................24
Course Code # IDCGRF001-A Accenture 2007 All Rights Reserved Page 2 of 25
Programming Techniques
Page 3 of 25
Programming Techniques
Module Introduction
The purpose of this module is to provide the fundamentals of Programming Techniques. This module will span a day of the Programming Techniques. There are 4 presentations and 2 hands-on activities, which will allow you to apply what you have learned. There will also be a Knowledge Check at the end of this module to assess your understanding of the content presented. The presentations and activities include: Presentation: 2.1 Fundamental Programming concepts Activity 1 Presentation: 2.2 Programming Life Cycle Activity 2
Page 4 of 25
Programming Techniques
Module Objectives
After completing this module, you should be able to: Discuss about Algorithm Define Algorithm Need of Algorithm Structure and concept of Algorithm Define Flowchart Need of Flowchart Structure and concept of Flowchart Testing Need of Testing Unit Testing Activity 1 Programming Life Cycle Types of Programming Design o Top-Down Approach o Bottom-Up Approach o Linear Programming o Structured Programming Program Life Cycle Activity 2
Page 5 of 25
Programming Techniques
Source Material
The source material used to facilitate the delivery of this module is located within the Module 2
Page 6 of 25
Programming Techniques
Algorithm
Overview
Algorithm is used to simplify a given task. It is a solution to a problem that is independent of any programming language. It is an explicit step-by-step procedure for producing a solution to a given problem. This presentation covers the basic features and concepts of Algorithm. In addition it covers the need of algorithm, structure of algorithm and concepts of algorithm An algorithm is: A finite sequence of steps Each step shall be explicit and unambiguous For each input, it shall terminate in finite time with output A program is an algorithm expressed in a programming language Different definition of algorithm is: An explicit step-by-step procedure for producing a solution to a given problem In mathematics and computer science an algorithm (the word is derived from the name of the Persian mathematician Al-Khwarizmi) is a finite set of well-defined instructions for accomplishing some task which, given an initial state, will terminate in a corresponding recognizable end-state (contrast with heuristic). Algorithms can be implemented by computer programs, although often in restricted forms; mistakes in implementation and limitations of the computer can prevent a computer program from correctly executing its intended algorithm. To know more about algorithm: http://en.wikipedia.org/wiki/Algorithm
Page 7 of 25
Programming Techniques
Benefits of Algorithm
An algorithm is a definite procedure for performing certain task It is written using simple English sentences Facilitates easy development of programs o Iterative refinement o Easy to convert it to a program It helps resolving complex problems Review is easier
Analyzing an Algorithm
There are different approaches of algorithm in analyzing a problem Empirical (a posteriori) approach o It is a method consisting of programming the competing techniques and trying them on different instances o The empirical approach to choosing an algorithm consists of programming the competing techniques and trying them on different instances with the help of a computer Theoretical (a priori) approach o It determines mathematically the quantity of resources needed by each algorithm as a function of the size of the instances The theoretical approach consists of determining mathematically the quantity of resources needed by each algorithm as a function of the size of the instances. The resources of most interest are computing time and storage space, with the former usually being the more critical. We compare the algorithms on the basis of their execution times, an algorithms storage requirements. The size of an instance corresponds formally to the number of bits needed to represent the instance on a computer, using some precisely defined and reasonably compact coding scheme. (The word size to mean any integer that in some way measures the number of components in an instance.) E.g. when we talk about sorting, we usually measure the size of an instance by the number of items to be sorted, ignoring the fact that each of these items may take more than once bit to represent on a computer. Similarly, when we talk about graphs, we usually measure the size of an instance by the number of nodes or edges (or both) involved. o The advantage of the theoretical approach is that it depends on neither the computer being used, nor the programming language, nor even the skill of the programmer. It saves both
Course Code # IDCGRF001-A Accenture 2007 All Rights Reserved Page 8 of 25
Programming Techniques
the time that would have been spent needlessly programming an inefficient algorithm and the machine that would have been wasted testing it. More significantly it allows us to study the efficiency of an algorithm when used on instances of any size. This is often not the case with the empirical approach, where practical consideration my force us to test our algorithms only on a small number of arbitrarily chosen instances of moderate size. Since it is often the case that newly discovered algorithm begins to perform better than its predecessor only when they are both used on large instances, this last point is particularly important. Hybrid approach o Combination of Empirical and theoretical approach o One can also think of a hybrid approach while analyzing the algorithms, where the form of the function describing the algorithms efficiency is determined theoretically, and then any required numerical parameters are determined empirically for a particular program and machine, usually by some kind of regression. Using this approach we can predict the time an actual implementation will take to solve an instance much larger than those used in the tests. Beware however of making such extrapolations solely on the basis of a small number of empirical test, ignoring all theoretical considerations. Predictions made without theoretical support are likely to be very imprecise, if not plain wrong. To know more about efficiency of algorithm: http://en.wikipedia.org/wiki/Algorithm http://homepages.ius.edu/gmanwani/C202/algorithm%20efficiency.pdf http://www.cprogramming.com/tutorial/computersciencetheory/algorithmic efficiency1.html http://people.cs.uct.ac.za/~bmerry/manual/algorithms/efficiency.html
Algorithm Complexities
Complexities are the parameters to capture the challenges or complications Few of the algorithm complexities are captured based on : o Time Complexities Is the amount of computer time it needs to run to completion
Page 9 of 25
Programming Techniques
Page 10 of 25
Programming Techniques
Structure of an Algorithm
Step, Control and Exit o Step represents the first step in the algorithm i.e. BEGIN o Control represents the flow of control within the algorithm o Exit represents the point of exit from the algorithm Comments o Proper comments must be inserted wherever necessary to give descriptions of the steps Variables o Properly named variables should be used in the algorithm Input / Output o Inputs can be read using the Input o Output can be written using Output Syntax for writing Algorithm 1. Begin //is the starting point of an algorithm 2. Statement1 3. Statement2 . . . . . . N. End
Page 11 of 25
Programming Techniques
Sequential Algorithm
Modules are executed in some sequence/ in a linear fashion Algorithm 1: Find the average of a student given the marks he obtained in three subjects. 1. 2. 3. 4. 5. Begin Input Marks1, Marks2, Marks3 AVG = (Marks1+Marks2+Marks3) / 3 Output Average Marks =, AVG END
Selection
To perform an operation based on the choice or selection of the individual is achieved using selection algorithm. There are two main types of selection algorithm 1. Single Alternative 2. Multiple Alternative Single Alternative Having a single condition criteria to qualify any test Multiple Alternative Having a multiple conditions to qualify any test
Mainly used with simple and Used when more than one condition single condition checks need to be checked and different statements need to be printed
Table 1: Example of selection
Algorithm to find the average of the marks a student has obtained in three subjects, test whether he passed or failed; for passing, average should not be less than 50: Algorithm 2: Single Alternative 1. 2. 3. 4. 5. Begin Input Marks1, Marks2, Marks3 Avg = (Marks1+Marks2+Marks3)/3 If (Avg >= 50) Then Output Passed End
Page 12 of 25
Programming Techniques
Algorithm 3: Multiple Alternative 1. 2. 3. 4. 5. 6. 7. 8. Begin Input Marks1, Marks2, Marks3 Avg = (Marks1+Marks2+Marks3)/3 If (Avg >= 50) Then Output Passed Else Output Failed End
Iteration
Algorithm 4: to read marks of three subjects. Find the average scored by the student, and print Pass if the score is more than 50 otherwise print Fail for N number of Students 1. 2. 3. 4. 5. 6. 7. 8. Begin Print Enter number of students Input N Repeat step 5 thru step 8, N TIMES Print Enter Marks of 3 subjects Input marks1, marks2, marks3 Avg = (marks1+marks2+marks3)/3 If (Avg >= 50) Then Print Passed Else Print Failed End-if 9. End
Page 13 of 25
Programming Techniques
Flowcharts
Overview
This presentation covers the fundamentals Flow chart. It the need of flowchart, different symbol used in the flowcharts etc. The flowchart can be defined as a diagram of the problem solving steps that is written by the programmer. It can also be defined as the geometric representation of an algorithm It is a diagram consisting of a set of symbols (such as rectangles or diamonds) and connecting lines that shows step-by-step progression through a procedure, process or system A flowchart (also spelled flow-chart and flow chart) is a schematic representation of a process. They are commonly used in business/economic presentations to help the audience visualize the content better, or to find flaws in the process. To know more about flowchart: http://en.wikipedia.org/wiki/Flowchart
Page 14 of 25
Programming Techniques
In Figure 1: Symbols used in flowchart A represents the start and End points B Computation step or logical step C Input /output Operation D Connector EDecision FLoop G Connector Flowchart 1: To read marks of three subjects. Find the average scored by the student, and print Pass if the score is more than 50 otherwise print Fail for N number of Students
Start
Enter m1,m2,m3
Avg=m1+m2+m3/3
end
Disadvantage
Page 15 of 25
Programming Techniques
Flowcharts: Limitations
Complex logic Alterations and Modifications
Testing
Overview
Testing is the process of running a system with the intention of finding errors. Enhances the integrity of a system by detecting deviations in design and errors in the system. Aims at detecting error-prone areas, which helps in the prevention of errors in a system. Testing also adds value to the product by confirming to the user requirements. Testing is Oriented to 'detection' Testing is a process used to help identify the correctness, completeness and quality of developed computer software. Testing is the process of running a system with the intention of finding errors It enhances the integrity of a system by detecting deviations in design and errors in the system. It aims at detecting error-prone areas, which helps in the prevention of errors in a system. Testing also adds value to the product by conforming to the user requirements. It is Oriented to 'detection'
Need of testing
Because software is likely to have faults To learn about the reliability of the software To fill the time between delivery of the software and the release date To prove that the software has no faults Because testing is included in the project plan Because failures can be very expensive To avoid being sued by customers
Unit testing
A unit test is a method of testing the correctness of a particular module of source code Unit testing is also known as Component/ Module/ Program Testing It is the most 'micro' scale of testing
Page 16 of 25
Programming Techniques
The goal of unit testing is to isolate each part of the program and show that the individual parts are correct Most thorough look at detail o Entry criteria: After completion of each screens/modules, unit testing is done o Exit criteria: Code is peer reviewed. Basic functionality is achieved. All requirements been tested or verified. No errors in calculations, formulas or rounding. If any bugs remain should not harm the basic functionality. No data corruption errors No Incorrect data, or data recorded in the wrong field, account or database
Test Cases
Test case is a set of test inputs, execution conditions, and expected results developed for a particular objective, such as to exercise a particular program path or to verify compliance with a specific requirement Test cases are written for each unit in the main program A program will execute all the test cases and record the results .it can be called as a test file. Test cases will have Description, inputs, expected outcomes Testing Example 1: To read marks of three subjects. Find the average scored by the student, and print Pass if the score is more than 50 otherwise print Fail for N number of Students
Page 17 of 25
Programming Techniques
1. Begin 2. Output Enter number of students 3. Input N 4. Repeat step 5 thru step 8, N TIMES 5. Output Enter Marks of 3 subjects 6. Input marks1, marks2, marks3 7. Avg = (marks1+marks2+marks3)/3 8. If (Avg >= 50) Then Output Passed Else Output Failed End-if 9. End Test case for this can be written like this for Testing Example 1: Step Action/Description Input Data Expected number outcome 1.001 Enter a Character: Invalid character input 1.002 Enter negative Number:-x Negative number input 1.003 Enter a special Special Invalid character character: x input
Key points
Algorithm is detailed sequence of actions to perform to accomplish some task Algorithms facilitates easy development of programs Flowchart is a graphic representation of the logic or steps in a program or system Testing is the process of running a system with the intention of finding errors. Unit testing is used to isolate each part of the program and show that the individual parts are correct.
Activity 1
Time: 30 minutes
Activity Overview
During Activity 1 you will practice simple logic formulation. The activity
Course Code # IDCGRF001-A Accenture 2007 All Rights Reserved Page 18 of 25
Programming Techniques
Activity Instructions
Activity 1.0
Write an algorithm, flow chart & test cases to find the sum of first 50 integers (It uses Iterational algorithm technique.)
Activity 1.1
Write an algorithm, flow chart & test cases to search an element in a table of N values (Uses iterative and selection algorithm technique)
Activity 1.2
Write an algorithm, flowchart & test cases to find the sum of N even numbers (Uses iterative algorithm technique)
Activity 1.3
Write an algorithm, flowchart & test cases to find a Fibonacci series (Fibonacci series is of the form 0,1,1,2,3,5Generate the series up to N terms)
Activity 1.4
Write an algorithm and flowchart & test cases to find the maximum & minimum among three numbers entered from the keyboard. (Use selection technique.)
Page 19 of 25
Programming Techniques
Overview
This presentation explains about different stages in the life cycle of a program A program is an algorithm expressed in a programming language The act of creating software or some other set of instructions for a computer Computer programming is the craft of implementing one or more interrelated algorithms using a particular programming language to produce a concrete computer program
WA HT
The presentation introduces different program design approaches And explains the problem situations where a particular approach can be used
R q ir eue mn e ts G th r a ein
Page 20 of 25
Programming Techniques
Top-down Design
Based on the fact that large problem become more manageable if they are divided into a number of smaller and simpler tasks which can be tackled separately. Top down design approach is performed in a special way. The main program is written first. It is tested before sub-programs are written. To do this, actual sub programs are replaced with stubs. The stubs simply test to see if the data is passed correctly. After the main program is written and checked, each module is written and tested in turn. If the modules run properly, then it is tested with the main program. If the module and the main run properly then the next module is written and checked and so on A technique for decomposing a problem into subtasks Top Down design is iterative many levels of decomposition o Strengths of top-down design easy to design can defer implementation details suitable for natural hierarchical problems Example 1 : For Top- Down Design - Algorithm to find the number of days between two given dates, using functions date1 = ( d1, m1, y1), date2 = (d2, m2, y2) Assume date1 is earlier or same as date2. Case 1: y1 < y2 total_days = s_y_days + e_y_days + m_y_days where: s_y_days = no of remaining days in m1 + the total of days in the months from (m1+1) to 12 e_y_days = (no of days covered in m2)-1 + the total of days in the months from 1 to (m2-1) m_y_days = total no of days in the years from y1+1 to (y2-1) Case 2: y1 == y2
Page 21 of 25
Programming Techniques
total_days = s_m_days + e_m_days + m_m_days where: s_m_days = no of remaining days in m1 e_m_days = (no of days covered in m2) - 1 m_m_days = total no of days in the months from (m1+1) to (m2-1)
Bottom-up Design
Write the most basic subroutines in the hierarchy first and then use them to make more sophisticated subroutines, The pure bottom-up approach is generally not recommended because it is difficult to anticipate which low subroutines will be needed for any particular program, In this approach it is usually assumed that the basic routines created will be general enough to be used more than once. This approach is designed to generate reusable code This is more like a basic instruction in the programming language than a large scale program component
Linear programming
Straightforward programming in sequential manner. This does not involve any decision making. General model of the linear program is: Read a data value. Compute an intermediate result. Use the intermediate result to compute the desired answer. Print the answer. Stop
Structured programming
The technique for writing a program using decisions, branching points, loops, etc. is called structured programming. Which in turn result in more complex programs. There are procedures that can be used for writing those complex programs that makes them much less error prone and much easier in debug.
Page 22 of 25
Programming Techniques
Requirement Gathering - Gathering all the required information to understand the problem in hand Algorithm - An simple English, language independent pseudo code is generated. Test Plan - Gives a plan to test the solution. It involves test cases, test scripts and test data Coding - Enter the source code using some standard editor, care must be taken for proper formatting, indenting and using comment entries. Compilation -The compiler program scans the source code for SYNTAX errors, and if it does not find any errors, it converts the source code to OBJECT code. Link - The object files are combined with other library files to create the EXECUTABLE file. Library - It is a catalogue of previously developed objects. They contain procedures for input, output, math, etc. A user can create their own libraries Testing - Synthesize test data such that it will evaluate the different criteria. The testing should be NEGATIVE testing. Debugger - It is a very useful tool to test the program as the intermediate results, new values of the variables, etc are visible
Page 23 of 25
Programming Techniques
Key points
A program is an algorithm expressed in a programming language Programming is the craft of implementing one or more interrelated algorithms using a particular programming language Large problem become more manageable if they are divided into a number of smaller and simpler tasks which can be tackled separately. Straightforward programming in sequential manner is Linear Programming
Activity 2
Time: 30 minutes
Activity overview
Activity 2 gives you an idea about application of top-down approach
Activity 2.0
Design the Payroll System using Top-down approach.
Page 24 of 25
Programming Techniques
Hints/Tips
1. Remember motivation comes from within. Take responsibility for your own learning. 2. Take notes from each presentation and activity. 3. If you dont understand something, ask questions. 4. Participate in course discussions. 5. Practice what youve learned to keep your skills sharp.
Page 25 of 25