Docsity

PROGRAMMING NOTES COMPILED BY P.K, Lecture notes of Engineering

Toggle sidebar
bg1
L0ESSON TWO: STRUCTURED PROG RAMMING AND PROGRAMMING TOOLS
INTRODUCTION
The first programs that were written were not structured. The program usually consisted of a
sequence of individual instructions that were executed one after another. This resulted in one
massive program consisting of several thousands of lines of code that was not grouped in any
logical way. Maintaining such programs was time consuming and expensive, since changing
even one line of code could affect the entire program.
Structured Program Design
Structuring a program breaks it down into understandable chunks. Structured programming is an
approach to writing programs that are easier to read, test, debug and modify. The approach
assists in the development of large programs through stepwise refinement and modularity.
Programs designed this way can be developed faster. When modules are used to develop large
programs, several programmers can work on different modules, thereby reducing program
development time.
In short, structured programming serves five purposes to:
i. Increase programmer productivity
ii. Program reliability (readability and execution time)
iii. Program testing
iv. Program debugging and
v. Serviceability.
Structured Programming Techniques
There are three techniques; Modular programming, Top-down Programming and bottom-up
approach.
Modular Programming
This technique involves breaking down the entire problem into smaller, more manageable units.
Features
•Each module within the application carries out a singular task.
•Each module runs independently of the other modules.
•Since each module is independent, a breakdown in any module does not greatly affect the
running of the application.
•Debugging is easier since errors can be traces to individual modules.
Advantages of modular programming
•Modules can be reused thus saving development time.
•Testing of individual modules in isolation makes tracing mistakes easier.
•An amendment to a single module does not affect the rest of the program.
•Ability to create libraries of often used routines, which are reliable and can go into other
programs.
Top-Down Approach
1
pf3
pf4
pf5

Partial preview of the text

Download PROGRAMMING NOTES COMPILED BY P.K and more Lecture notes Engineering in PDF only on Docsity!

L0ESSON TWO: STRUCTURED PROG RAMMING AND PROGRAMMING TOOLS

INTRODUCTION

The first programs that were written were not structured. The program usually consisted of a sequence of individual instructions that were executed one after another. This resulted in one massive program consisting of several thousands of lines of code that was not grouped in any logical way. Maintaining such programs was time consuming and expensive, since changing even one line of code could affect the entire program.

Structured Program Design

Structuring a program breaks it down into understandable chunks. Structured programming is an approach to writing programs that are easier to read, test, debug and modify. The approach assists in the development of large programs through stepwise refinement and modularity. Programs designed this way can be developed faster. When modules are used to develop large programs, several programmers can work on different modules, thereby reducing program development time. In short, structured programming serves five purposes to: i. Increase programmer productivity ii. Program reliability (readability and execution time) iii. (^) Program testing iv. Program debugging and v. Serviceability. Structured Programming Techniques There are three techniques; Modular programming, Top-down Programming and bottom-up approach. Modular Programming

This technique involves breaking down the entire problem into smaller, more manageable units. Features

  • Each module within the application carries out a singular task.
  • Each module runs independently of the other modules.
  • Since each module is independent, a breakdown in any module does not greatly affect the running of the application.
  • Debugging is easier since errors can be traces to individual modules.

Advantages of modular programming

  • Modules can be reused thus saving development time.
  • Testing of individual modules in isolation makes tracing mistakes easier.
  • An amendment to a single module does not affect the rest of the program.
  • Ability to create libraries of often used routines, which are reliable and can go into other programs. Top-Down Approach

The program design process is frequently tackled by adopting a top-down approach. An outline program is designed first, showing the main tasks, or components of the program and the order in which they are to be executed. Each main component is then reduced to a number of smaller, simple and more manageable components and this process continues at each level until there is sufficient detail to allow the coding stage to proceed. The process of reducing components into sequences of smaller components is often referred to a stepwise refinement and forms the basis of structured programming. Top-down approach allows a program to be designed methodically by providing a complete definition of the program at each level of complexity. Example: Exam Results Consider a class of twenty students who sat for examinations in four subjects. A student must get an average of 50% in all four subjects to pass. A report is required that will show the marks obtained by each student per subject, the average mark and whether the student has passed or not.

Name Subjects AVERAGE PASS/FAIL MATHS ENGLISH BIO PHY Jane Mary

74 68 54 42 60 PASS

Rodney

Winston

48 52 38 50 47 FAIL

Bottom-up Approach In this design approach, the application is developed starting at the bottom of the hierarchy i.e. the single task modules. As each category of programs is completed on the hierarchy, the controlling program for that category is created.

Program Design Tools Algorithms An algorithm is a sequence of steps, which results to a plan or strategy on how to go about solving a problem. There are different ways of presenting an algorithm. Some common ways include-

  • Pseudocode
  • Flow charts Pseudocode Pseudocode (or structured English) describes module design in words using syntax like programming language. It aims at providing programmers with clear and unambiguous process specifications. Some of the constructs used in pseudocode are described below. i. Begin/end construct which is used to delimit a sequence of actions. For example, a pseudocode to process exam results in the first example. Begin Exam Results Get Input data

Input/output – Input/output operations are represented in a parallelogram.

Decision – This symbol is used to indicate decision making and branching. The criterion is shown inside the symbol and the lines (paths) out show the results.

Connectors – A small circle containing a number or letter that is used to split a large flow chart into smaller parts.

Design of Exam Results using Flow Charts

YES Average >50%?Calculate AveragePrint ‘FAIL’Print ‘PASS’ EndPrint ReportCompleted all Students?Get Student

Marks

Get Student

Name

Start

NO

YES

NO

YES

The flow chart for the Exam results example is as shown on the previous page. The repetition construct is shown by using a decision symbol to loop back until the condition becomes true.

Useful tips when drawing flow charts.

i. Use standard flow chart symbols ii. (^) Try to keep logic from top to bottom and from left to right iii. Use arrow to indicate flow (direction) iv. Use connectors to reduce the number of flow lines. v. Add notes if necessary but do not clutter the diagram. vi. Lines should be ideally vertical or horizontal. Program Coding

Syntax Errors

Every programming language (like any natural language) has a set of rules concerning formal spelling, punctuation, naming of variables and other conventions that must be obeyed by the programmer. An error in language usage is called a syntax error. Special system programs called translators detect syntax error.

A program that has syntax errors can produce no results. You should look for the error in the line suggested by the compiler. Syntax errors include; Missing semi colon at the end of a statement e.g. Area = Base * Length Use of an undeclared variable in an expression Illegal declaration e.g. int x, int y, int z; Use of a keyword in uppercase e.g. FLOAT, WHILE Misspelling keywords e.g. init instead of int Logical Errors

Although the compiler will detect syntax errors, it will not find errors in programming logic. While syntax errors result from program language mistakes, logical errors occur when the program does not function as intended.

A logical error can lead to one of the two results:

  • The program will produce wrong results but will finish normally.
  • An unusual condition will be encountered (e.g. attempting to divide by zero) that leads to a premature end of the program. This type of an error is called run-time error.