Sunteți pe pagina 1din 60

UNIT 1

Topics
Planning the Computer Program Flowchart Types of Logic used in Flowchart Examples Hierarchy of Programming languages Classification of programming language Popular programming language Program development process Characteristics of a Good Program Error in Programming

Algorithm
a precise rule (or set of rules) specifying how to solve some problem the set of simple instructions that combine to accomplish a task.

Programming
Program a set of detailed, step-by-step instructions that directs the computer to process data into information. Programming language a set of rules (syntax) and commands that provides a way of telling the computer what operations to perform. Programmer designs algorithm and converts it into computer instructions and tests programs as developed.

Program Development Life Cycle


Six main phases
1. 2. 3. 4. 5. 6.

Define the problem Design the solution Code the program Test the program Document the program Implement the program
The phases will be reviewed over next few slides

1. Define the Problem


Review program specs Meet with analysts / users Identify program components (IPO Chart Input, Processing, Output)

2. Design the Solution


Design an algorithm. An algorithm is a finite series of logical steps required to solve a particular problem. Finite means that the beginning and end are clearly defined. Design tools
Use flowcharts and pseudocode. Use the appropriate programming flow control structures in the design
7

Flowcharting
A flowchart is a pictorial representation of an algorithm or logical steps. Each step is represented by a symbol and the arrows indicate the flow and order of the steps. The shape of the symbol indicates the type of operation that is to occur. Flowcharts may help the more visual students learn and understand logic.
8

Basic Flowchart Symbols


Input or Output

Begin or End

Processing

Decision

Branch or Direction of Flow


9

Program logic
Read the file If condition satisfy do the process Else do another process Go to first step to read the next record. Stop if all records are read.

Types of Logic
Straight Sequential Execution Transfer of Control Looping

Straight Sequencial Execution

13

Flow Control Structures


Flow control is the order in which statements are executed. There are control structures.
1. Selection Control
Also referred to as branching (if and if-else)

2. Case Control (similar to selection) 3. Repetition Control (loops)

14

Selection Control (branching)

15

Case Control

16

Repetition Control (loops)

17

Should be written before a program is coded. Read from top to bottom unless a branch alters the normal flow. Each symbol represents a specific function. The note inside each symbol indicates the specific operation.

Advantages of flowchart: Communication: Flowcharts are better way of communicating the logic of a system to all concerned. Effective analysis: With the help of flowchart, problem can be analysed in more effective way. Proper documentation: Program flowcharts serve as a good program documentation, which is needed for various purposes. Efficient Coding: The flowcharts act as a guide or blueprint during the systems analysis and program development phase. Proper Debugging: The flowchart helps in debugging process. Efficient Program Maintenance: The maintenance of operating program becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that part

Disadvantages of flowchart: Complex logic: Sometimes, the program logic is quite complicated. In that case, flowchart becomes complex and clumsy. Alterations and Modifications: If alterations are required the flowchart may require re-drawing completely. Reproduction: As the flowchart symbols cannot be typed, reproduction of flowchart becomes a problem. The essentials of what is done can easily be lost in the technical details of how it is done.

Flow Chart Example 1


Draw a flowchart to find the sum of first 50 natural numbers.

Flow Chart Example 2


Draw a flowchart to find the largest of three numbers A, B, and C.

Draw a flowchart to find the selling price of an item, given percentage loss or gain and the cost price of an item. Draw a flowchart to compute and print the grades for an examination. Input is roll number, and marks of 5 subjects out of 100. Draw a flowchart to calculate the value of X power N, where X is an integer and N has a +ve integer. Draw a flowchart to find the sum of first 50 odd numbers.

Pseudocode
Pseudocode is a mixture of programming code and English like statements. Used when designing algorithms. When designing, we dont necessarily want to be concerned about specific method names. We want to concentrate on the design and logic. Later we can look up specific properties or method names.
30

3. Code the Program


Translate solution algorithms into a programming language. Enter program code into programming development tool using correct syntax.
31

4. Test the Program


Remove syntax and logic errors Syntax error - when code violates language rules, errors are displayed by the programming development tool. Logic error when program does not generate expected output when using the test data. This time error must be found by the programmer. Logic Errors are also call bugs, so finding and fixing logic errors is call debugging. Debug Utility some errors are difficult to find, so programmers use a debugger that allows them to walk through the code as each statement is executed, so that they can verify the flow and see the values in variables as they 32 change in memory.

5. Document the Program


Programmer name and date A narrative description of the program Include comments within the source code Flowcharts and/or pseudocode Layouts of input and output records Testing procedures Most of the items would be electronic copies and saved in the project library.
33

6. Implement & Maintain the Program

34

Programming Language
A programming language is a set of written symbols that instruct the computer hardware to perform specified tasks. Basic instructions
Input/output instructions Arithmetic instruction Logic instruction Control instruction Data movements instructions Specification instructions

Hierarchy of programming language


Declarative language

High level language

Assembly language

Machine language

Harware

Declarative language
Command languages which tells what to do instead of how to do it. Example SAS and SPSS.

Programming Languages (cont.)


2. High-level languages (machine independent)
Codes similar to everyday English High-level languages: Write program instructions called statement that resemble a limited version of English. e.g., the statement value = value + delta. Portable, meaning it can be used on different types of computers without modifications. Compilers translate them to machine languages. Examples are FORTRAN, PASCAL, COBOL, C, C++, BASIC etc. Use mathematical notations (translated via compilers)
Example: grossPay = basePay + overTimePay

Example: Statement:

a= a + b

Before b

10 7

a After b

17 7

Interpreter
A translator which translates a program into machine language one line at a time

Compiler
Translate a whole program at once into machine language. It can be executed any time. The source code remains intact and be updated.

Assembly languages (machine dependent)


Uses mnemonics to represent operation codes Uses descriptive names for operations and data, e.g. , LOAD value, ADD delta, STORE value. Assemblers will translate these to machine languages. Intermediate level. Somewhat descriptive, but basically following the machine instructions.
Example:

LOAD BASEPAY
ADD OVERPAY STORE GROSSPAY

Machine languages (machine dependent)


Native tongue of a particular kind of computer. Each instruction is a binary string. The code is used to indicate the operations to be performed and the memory cells to be addressed. This form is easiest form of computers to understand, but is most difficult for a person to understand. Strings of numbers giving machine specific instructions

Example: +1300042774 +1400593419 +1200274027

Fourth generation languages


Users can create programs with less effort non-procedural language Allows users to specify what the output is. No need to specify how the data to be processed. Objectives
Increase the speed of developing programs Minimize users efforts to obtain information from computer Decrease skill level required of users Reduce errors to make programs that are easy to change Database Query Language. Report Generator Application Generator

Popular Programming Languages


BASIC (Beginners All-purpose Symbolic Instruction Code 1960s) COBOL (Common Business Oriented Language 1960s) C - Used to develop UNIX in 1970s; runs on most operating system now C++ - A better C, Object oriented; (1980s) Java - Similar to C++; Became popular because of web development applets, Programs run on multiple platforms (Win, Unix, Mac), Originally named Oak (1991) and renamed to Java in 1995 Visual Basic Microsoft created language so that graphical user interfaces could be created for their new operating system Windows (1990s) C# (c-sharp) Microsofts newest language (2002); syntax is similar to Java, but uses the VB library. See next slide for examples
43

Examples of Programming Languages


C#

Visual Basic

COBOL Java

44

Classification of programming languages


Procedure oriented languages Vs. Non-procedure oriented languages
NPL describes the task without prescribing a set of executable operation to be performed in sequence. Query languages, report writers, interactive database programs, spreadsheets and application generators are examples of non-procedural languages. Eg. List out all employees name.

Problem oriented language, problem solving language


Any language which is easier for writing a solution to a particular problem.

Application oriented language


ALHARD: an application-oriented language for hardware simulation

Reference language, publication language, H.w language.


Has unique character for each concept or character in the language. And consists of a fixed set of symbols. Publication language handles to handle exponents, spaces and greek letters. H/w lang. which the machine can understand.

Popular programming languages


FORTRAN PASCAL COBOL ALGOL RPG (Report Program Generator) PL/1 BASIC APL AUTOCODER SNOBOL C LISP MODULA II ADA PROLOG

CHARACTERISTICS OF A GOOD PROGRAM

Simplicity Testability Efficiency Modularity Transferability Generality Flexibility

Program development process


Problem definition Program design
MODULAR PROGRAMMING Top down or bottom up approach Structured programming

Coding Debugging Testing Documentation Maintenance

Guidelines for writing the coding


Use meaning names and labels Clarity and simplicity of expressions Keep short modules Keep names and labels distinct Make modules fairly general Avoid obscure constructions. Avoid jumps when possible Place definitions in groups at the start of the program Use names instead of specific addresses or data Use comments and indentation.

Tools for debugging programs


Simulators Logic analyzers Breakpoints Trace routines Memory dumps Software interrupts

Common errors
Initialize variables counters and pointers Inverting conditions, such as jumping on zero instead of on not zero Reversing the order of operand Jumping on conditions that have been changed Confusing address and values Ignoring Overflow

Rules for program testing


Make a test plan part of the program design Check all trivial and special cases Select test data on random basis Document the testing Use the ma and mini values of all variables as test data Use statistical methods in planning and evaluating complex tasks.

Events that cause changes in the original program

Newly discovered bugs Specification changes Specification expansion New equipment

Errors in programming
3 categories
Syntax errors
Misspelled keywords, missing commas Undefined

execution errors
Division by zero Number calculated exceeds the computer capacity Loss of significant digits

Logical errors
Sequence of statement

Trapezoidal rule

Exercise: Approximate using Simpson's Rule with n = 4. Answer x = (3 - 2)/4 = 0.25 y0 = f(a) = f(2) = 1/(2 + 1) = 0.3333333 y1 = f(a + x) = f(2.25) = 1/(2.25+1) = 0.3076923 y2 = f(a + 2x) = f(2.5) = 1/(2.5+1) = 0.2857142 y3 = f(a + 3x) = f(2.75) = 1/(2.75+1) = 0.2666667 y4 = f(b) = f(3) = 1/(3+1) = 0.25

S-ar putea să vă placă și