0% found this document useful (0 votes)
6 views

Introduction To Program Design Notes

Uploaded by

psagini96
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Introduction To Program Design Notes

Uploaded by

psagini96
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

KISII UNIVERITY

BIT 229: INTRODUCTION TO PROGRAM DESIGN


Topic 1

1.1 Introduction to Computers


 A computer is a programmable machine that accepts inputs, stores/processes the inputs to produce outputs
(Input Process Output)
 A computer comprises of the following:
 CPU - central processing unit „
 Where decisions are made, computations are performed, and input/output requests are
delegated
 Memory
 Stores information being processed by the CPU
 Input devices
 Allows people to supply information to computers
 Output devices
 Allows people to receive information from computers

CPU
 Brains of the computer
 Arithmetic calculations are performed using the Arithmetic/Logical Unit or ALU
 Control unit decodes and executes instructions
 Arithmetic operations are performed using binary number system.

Input and Output Devices


 Accessories that allow computer to perform specific tasks
 Receive information for processing
 Return the results of processing „

Common input and output devices:


 Speakers
 Mouse
 Scanner
 Printer
 Joystick
 CD-ROM
 Keyboard
 Microphone
 DVD

Memory
 Computer memory is any physical device capable of storing data / information and instructions.
 Computer memory is the storage space in computer where data is to be processed and instructions required
for processing are stored.
 The memory is divided into large number of small parts called cells.

 Memory is primarily of three types:


 Cache Memory
 Primary Memory/Main Memory
 Secondary Memory

1
1.2 Evolution of programming
Five Generations of programming languages have been defined. These ranges from machine level languages
(1GL) to languages necessary for AI & Neural Networks (5GL).

i. First Generation Programming Language (1GL)

First generation of programming language refers to machine language. Machine language is lower level
language which uses object code (sometimes also known as machine code). Object code is the combination
of binary digits (0, 1).

ii. Second Generation Programming Language (2GL)

Second generation of languages is also low level language which is known as assembly language.
Assembly languages are the interface between Machine level languages and High level languages. They are
written using mnemonics.

iii. Third Generation Programming Language (3GL)

Third Generation programming languages are High level Programming languages like JAVA & C.

iv. Fourth Generation Programming Language (4GL)

This is the set of current generation programming languages.


These languages are similar or closer to human languages.

General characteristics of 4GL are:


 Closer to human languages
 Portable
 Database supportive
 Simple and requires less effort than 3GL
 Non procedural

Different types of 4 GL are:


 Query Generator
 Report generator
 Form Generator
 Application Generator
 GUI Generator
 Relational Database Manager.
Sometimes referred to as scripting languages

v. Fifth Generation Programming Language (5GL)


Languages used for writing programs for Artificial Intelligence, Neural Network, and Plasma Computing
etc

2
1.3 Types of Programming Languages

i. Procedural Programming Languages


Procedural programming specifies a list of operations that the program must complete to reach the desired state.

Each program has a starting state, a list of operations to complete, and an ending point. This approach is also
known as imperative programming.

By splitting the programmatic tasks into small pieces, procedural programming allows a section of code to be
re-used in the program without making multiple copies. It also makes it easier for programmers to understand
and maintain program structure.

Two of the most popular procedural programming languages are FORTRAN and BASIC.

ii. Structured Programming Languages


Structured programming is a special type of procedural programming.
It provides additional tools to manage the problems that larger programs were creating.
Structured programming requires that programmers break program structure into small pieces of code that are
easily understood.

It is often associated with a “top-down” approach to design. The top-down approach begins with an initial
overview of the system that contains minimal details about the different parts. Subsequent design iterations then
add increasing detail to the components until the design is complete.
The most popular structured programming languages include C, Ada, and Pascal.

iii. Object-Oriented Programming Languages


Object-oriented programming is one the newest and most powerful paradigms. In object-oriented programs,
the designer specifies both the data structures and the types of operations that can be applied to those data
structures.
This pairing of a piece of data with the operations that can be performed on it is known as an object.

A type of programming in which programmers define not only the data type of a data structure, but also the
types of operations (functions) that can be applied to the data structure. In this way, the data structure becomes
an object that includes both data and functions. In addition, programmers can create relationships between one
object and another. For example, objects can inherit characteristics from other objects.
One of the principal advantages of object-oriented programming techniques over procedural programming
techniques is that they enable programmers to create modules that do not need to be changed when a new type
of object is added.

A programmer can simply create a new object that inherits many of its features from existing objects. This
makes object-oriented programs easier to modify.

To perform object-oriented programming, one needs an object-oriented programming language (OOPL). Java,
C++ and Smalltalk are three of the more popular languages, and there are also object-oriented versions of
Pascal.

3
Topic 2

1.4 Steps of Learning Programming

Alphabets
Digits
Special symbols

Box 1
 Understand the language’s character set
 Alphabets
 A-Z and a-z
 Digits 0-9
 Allowable special symbols
Box 2
 Variable
 Named memory location whose contents may vary during program execution
In the equation y=mx+c
y and x are variables
 Constant
 Named memory location whose contents cannot vary during program execution
In the equation
−b ± √ b2−4 ac
x=
2a
a, b and c are constants
 Keywords
 Reserved words that have implicit meaning to the compiler/computer
 Special words that have already been explained to the computer
Box 3
 Instructions
There are general four different types of instructions
i. Type declaration instructions
 Data type – Format in which data is manipulated or stored in the computer
 Before any data is used in a program, its type has to be declared

Eg
int A, char fname, double Area, string department.

ii. Arithmetic / Logic instructions


 Operations between one or more operands

 Arithmetic operators – act on two operands

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division

4
% Modulus(remainder after integer division)

 Unary operators
 act upon a single operand to produce a new value
 usually precede their single operand, though some can appear after the operand

Operand Meaning
- Unary minus(to indicate negative numbers or
expressions)
+ Unary plus(to indicate positive numbers or
expressions)
++ Increment by 1
-- Decrement by 1

Logical Instruction
 To perform logical/logic instructions, relational and logical operators are necessary
 The result of s logical instruction is a Boolean value 0 or 1 (True or false)

Relational operator

Operand Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to

Equality operators

Operand Meaning
== Equal to
!= Not equal to

Logical operators (Logical Connectives)

 Act upon operands that are themselves logical expressions


 Net effect is to combine the individual logical expressions into more complex conditions that are
either true or false.

Operand Meaning
&& And
|| Or

iii. Input / output Instruction


 Most programming languages use functions (methods) that permit the transfer of information
between the computer and the standard input/output devices.

iv. Control Instructions


 There are basically two broad categories of control instructions
 Decision (branching ) statements eg if, if…else, switch
 Loops(repetition) structures eg for, while, do…while

5
Box 4

Program

 Writing instruction that tells a computer what to do


 Writing a program is an art like swimming
 One can learn how to swim in a swimming pool, flowing river, lake or ocean
 It depends on the passion of someone

6
Topic 3

2.0 Program Design

 A computer program is a set of instructions written in a programming language for solving a


particular problem.
 It is essential that the overall program strategy be completely mapped out before any of the
detailed programming actually takes place. This permits one to concentrate on the general
program logic, without being concerned with the syntactic details of the actual instructions.

2.1 Software development cycle


 Software- Instructions and documentation
 There are two broad categories of software :
 System software
 Programs that support the execution and development of other programs „
 Two major types:
 Operating systems
 Resource allocator
 Kernel system
 Translation systems
o Set of programs used to develop software
o A key component of a translation system is a translator
o Some types of translators:
 Editor „
 Compiler
 Linker
 Loader
 Debugger
 Viewer
 Application software
 Programs designed to perform specific tasks that are transparent to the user.
 Common application software:
 Word processors
 Desktop publishing programs
 Spreadsheets
 Presentation managers

7
2.2 Software Development

Source Program

Compile Library routines

Edit Link

Other object files


Think Load

Execute

Figure 2.0 Software Development Cycle

 In the initial stages of program development, it is important to first to construct an algorithm.


 An algorithm is a finite set of instructions that is followed accomplishes a particular task.
 Algorithm can be presented using two ways:
 Pseudocode
 English like statements for solving a problem
 Flowchart
 Graphical representation of solving a specific problem.
 After designing a program using either a Pseudocode or flowchart, a source code is written in a
programming language using an editor.
 The program is then compiled and then executed using an appropriate compiler.
 If the program has errors they will be displayed and debugging the program undertaken then the
program compiled and executed again. The process is repeated until appropriate inputs generate the
desired outputs.
 If the program is correct, the different types of tests are undertaken.

8
Topic 4

2.3 Data Types


 Format in which data is stored in the computer memory.

Data Type

Basic/ Primary/primitive Secondary Data types

char int float double Linear data Structure Non-linear data structure

Array stack queue Linked list Graphs Trees

 It is important to understand the size and memory requirement for basic data types.

2.4 Identifiers

 Identifiers are the names that are given to various program elements, such as variables, functions,
arrays etc
 Rules for forming identifiers :
 Identifiers may consist of letters, digits and the underscore
 The first character should not be a digit
 Uppercase and lowercase letters are distinct
 No special symbols other than the underscore can be used in identifiers
 Keywords are not used as identifiers

2.5 Constants

 There are basically four different types of constants:


 Integer constants
 Floating point constants
 Exponential constants
 Fractional constants
 Character constants
 String constants

9
2.6 Expressions and Statements

Declarations

 Before any variable or constant is used in a program, its type as to be specified


 Data type is the format which data is stored in a computer memory

o Example

int a;
float radius, area, circumference;
char ch;
char name[20], dept[20], supervisor[25];
string name = “Kisii University”;

Expressions
 An expression represents a single data item such as a number or a character
 Expression may consist of a single entity, such as a constant, a variable, an array element or a
reference to a function
 Expression may consist of some combination of entities interconnected by on or more operators
 Expression can represent logical conditions that are either true or false

Examples

a+b
y=x
mx + c
c=a +b
x<= y
x==y
++i
i=i+1

Statements
 A statement causes the computer to carry out some action
 Types of statements:
 Expression
 consists of an expression commonly followed by a semi colon
 Example
 a=3;
 y=mx+c;
 i++;
 printf(“area =%f”, area);
 Compound
 Consists of several individual statements enclosed within a pair of braces { }
 The individual statements may themselves be expressions statements, compound
statements or control statements

10
 Compound statements do not terminate with a semicolon

 Example
{
pi=3.141593;
circumference=2 * pi * radius;
area= pi * radius * radius;
}
 Control
 Used to create program features such as:
 Logical tests
 Loops
 Branches

 Example
While (count <= n) {
printf(“ x= “);
scanf(“%f” , &x);
sum += x;
++count;
}

Symbolic constant

 A name that substitutes for a sequence of characters


 The characters may represent a numeric constant, a character constant or a string constant
 Are usually defined at the beginning of a program

Example

#define TAXRATE 0.25


#define TRUE 1
#define FALSE 0
#define FRIEND “ Benji”

11
Topic 5
3.0 Programming Fundamentals
Operators

 In addition to operators studied in section 1.4, most programming languages also support the
following operator:
 Assignment operators
 Most commonly used is =
 Identifier = expression
Example
a=3;
x=y;
sum=a+b;
area= length * width;
- Multiple assignments
 Identifier 1 = Identifier 2 = … = an expression
Example
i=j=5;
- The Conditional Operator
- Simple conditional operations can be carried out with the conditional operator ( ? : )
- expression 1 ? expression 2 : expression 3
 expression 1 is evaluated first. If it is true then expression 2 is evaluated and
it becomes the value of the conditional expression. However, if expression 1
is false then expression 3 is evaluated and become the value of the
conditional expression
Operator precedence Groups
 Precedence is the order in which operations are performed
Operator Category Operators Associativit
y
Unary operators - ++ -- ! R -> L
Arithmetic multiply, divide and remainder * / % L->R
Arithmetic add and subtract + - L->R
Relational operators < <= > >= L->R
Equality operators == != L->R
Logical and && L->R
Logical or || L->R
Conditional operator ? : R -> L
Assignment operators = += -= *= /= %= R -> L

5.0 Functions
 A function is a self- contained program segment that carries out some specific well-
defied task.
 Every program contains one or more functions
 There are two broad categories of function:

12
 Library/inbuilt functions
 Come with the compiler
 It is important to understand/know library functions of a programming

 User defined functions


 Defined by the programmer to carry out its intended task/action when
accessed/called from some other portion of the program
 If a program contains multiple functions, their definitions may appear in
any order, though they must be independent of one another.
 Terminologies associated with functions :
 Function prototype/declaration
 Function definition
 Accessing a function/ function call
 Actual arguments..arguments appearing in the function call
 Formal arguments/arguments/actual parameters..appear in the
first line of the function definition
o The number of actual arguments must be the same as the
number formal arguments and each actual argument must be
of the same data type as its corresponding formal argument
 Return value—some functions return a value while others do not
5.1 Recursion
 Recursion is a process by which a function calls itself repeatedly until some specified condition
has been satisfied.
 The process is used for repetitive computations in which each action is stated I terms of a
previous result.

Topic 6

6.0 Data input and output


 Many programming languages make use of library / inbuilt functions for input/output operations.
 The library functions permit the transfer of information between the computer and the standard input /
output devices (eg keyboard and a CRT monitor)
 The input/output functions can be accessed from anywhere from within a program by writing the
function name, followed by a list of arguments enclosed in parenthesis.
 The arguments represent data items that are sent to the functions.
 Some input/output functions do not require arguments though the empty parenthesis will be necessary
 Some programming languages include a collection of header files that provide necessary in support
of various library functions. Each file contains information in support of a group of related library
functions.

7.0 Control Structures

 A realistic program may require that a logical test be carried out at some particular point within
the program.
 There are two broad categories of control statements:
 Decisions/branching/selection

13
 Loops/repetitions
 Recall relational operators and logical expressions
 To form logical expressions, relational operators (<, <=, >, >=) and the two equality
operators (== and !=) are required.
 Example of logical expressions
 count <= 100
 sqrt (a+b+c) 0.0005
 answer == 0
 balance >= cutoff
 ch1 < ‘T’
 letter != ‘x’
 The logical connectives /logical operators (&& (AND) and || (OR)) and the unary
negation operator ! are also used in the construction of relational expressions
 Example
 (count <= 100) && (ch1 != ‘*’)
 (balance < 1000.0) || ( status ==’M’)
 !((pay >= 20000.0) && (status ==’S’))
 The conditional ?: also makes use of an expression that is either true or
false eg status = (balance == 0)? (discount =1000.0) : (rate =0.6)

Topic 7

7.1 Decisions
 When one of several possible actions is carried out depending on the outcome of the logical test
(Branching) or when one group of statements is selected from several available groups
(selection).
 Major decision statement and general syntax :
 if statement
 if(expression) statement or

if(expression)
{
statement 1;
.
.
.
Statement n;
}
 if …else statement
 if (expression)
{
statements;
}
else
{

14
statements;
}

 switch statement
 causes a particular group of statements to be chosen from several available
choices (groups)
 the selection is based upon the current value of an expression which is
included within the switch statement.
 switch (expression)
{
statements;
}
 switch(expression)
{
case expression 1: { statements;}
break;
case expression 2:{statements;}
break;
.
.
.
break;
case expression n:{ statements;}
break;
default:{statements;}

 break statement
 break statement is used to terminate loops or exit from a switch.

Topic 8

7.2 Loops
 Group of instructions are executed repeatedly until some logical condition has been satisfied.
 There are three major types of loop structures :
 The while loop
 Is used to carry out looping operations in which a group of statements is executed
repeatedly until some condition has been satisfied.
 General syntax
while (expression){
staments;}
 The do…while loop
 When a loop is constructed using the while loop, the test for continuation of
the loop is carried out at the beginning of each pass.
 Sometimes it is desirable to have a loop with the test for continuation at the
end of each pass -> do…while loop will suffice.

15
 General syntax
do
{
statements;
}while(expression);
 The statements are executed at least once before the loop test is carried out.

 The for loop


 Most commonly used loop structure
 It includes an expression that specifies an initial loop value for an index,
another expression that determines whether or not the loop continues, and a
third expression that allows the index to be modified at the end of each pass.
 General syntax
for(initialization_expression; test_expression; update_expression)
{
statements;
}
 Initialization
 Assignment expression
 used to give the index an initial value
 The index controls the looping action
 It executed once
 Test expression
 A logical expression
 Represents a condition that must be true for the loop to
continue
 It is evaluated and tested at the beginning of each pass
through the loop.
 Update expression
 Unary expression or assignment expression
 Used to alter the value of the parameter initially assigned by
initialization expression
 Is evaluated at the end of each pass
 The continue statement
 Is used to by pass the remainder of the current pass through a loop. The loop does not
terminate when a continue statement is encountered. Rather, the remaining loop statements
are skipped and the computation proceeds directly to the next pass through the loop.

16
Topic 9

8.0 Arrays
 Many applications require the processing of multiple data items that have common
characteristics.
 An array is a collection of homogeneous data items.
 The individual data items can be characters, integers, floating point numbers etc. However, they
must be of the same type.
 Secondary data type which is indexed
 Each array element is referred to by specifying the array name followed by one or more
subscripts (thus arrays are sometimes referred to as subscripted data type), with each subscript
enclosed in square brackets.

X
10 20 30 40 50 …

X[0] X[1] X[2] X[3] X[4] …

 X is a one dimensional array, X is the name of the array


 In most programming languages, Array indexing starts from zero(0)
 X[i] refers to the ith element of array X
 In a two dimensional array, X[i][j] is the element at row i, column j.

8.1 Defining an array


 Arrays are defined in much the same manner as ordinary variable, except that each array
name must be accompanied by a size specification(maximum number of elements)
 Example
data type array_name[expression]
eg int X[100] - X is an array of integer numbers with a maximum size of 100 integer
numbers.
 Operations that can be performed on arrays( i.e created array) :
 Traversing
 Insertion
 Deletion
 Search
 Update
 Sort
 merge

8.2 Multidimensional Array


 A two dimensional array requires two pairs of square brackets, a three dimensional array will
require three pairs of square brackets and so on.
 Multi dimensional arrays are defined in much the same manner as one dimensional array.
 Example
 int matA[20][15], matB[20][15]

17
 double records[100][66][255]
9.0 Structures
 Consists of heterogeneous data items.
 A single structure might contain integer elements, floating – point elements and character
elements
 Individual structure elements are referred to as members.

Topic 10

9.1 Defining a structure


 Structure declaration/definition is in terms of its individual members.
 The composition of a structure may be defined as follow:
struct tag {
member 1;
member 2;

member n;
};
 struct is a required keyword; tag is a name that identifies the structure and member 1,
member 2,...member m are individual member declarations.
 Member names within a particular structure must be distinct from one another,
though a member name can be the same as the name of a variable that is defined
outside of the structure.
Example
 Composition of an account

Account (Structure)
(Member)
Acct_No
(Member)
(Member)

Acct_Type (Member)

Cust_Name[50]

Balance

 Typical structure declaration/definition


struct Account {
int Acct_No;
char Acct_Type;
char Cust_Name[50];

18
float Balance;
};
 A structure is an abstract data type (ADT) and its variable is declared the same way as
primary data types:

struct Account oldcustomer, newcustomer;


 Oldcustomer and newcustomer are variables of Account type.

9.2 Processing a Structure


 The members of a structure are processed individually as separate identities.
 A structure member can be accessed by using the dot (.) operator.

Example

struct Account {
int Acct_No;
char Acct_Type;
char Cust_Name[50];
float Balance;
};
struct Account Customer;

 Customer is a variable of the type Account


 To access Customer’s Balance
Customer.Balance

Topic 11

10.0 Preparing and Running a program


 After choosing the language to write a program, it will be necessary to understand the integrated
development environment (IDE) the programmer is going to use.
 Some of the compilers are open source yet others are proprietary.

10.1 Planning a Program


 It is essential that the overall program strategy be completely mapped out before any of the detailed
programming actually takes place.
 Once the overall program strategy has been clearly established, the details associated with individual
program statements can be considered.
 With large programs, the process of planning (Analysis and design) might be repeated several times
with more programming details being added at each stage.
 In the initial stages of program development, the amount of programming language is minimal
consisting only of major program components, such as function headings, function references,
compound statements and portion of control statements describing major program structures.
 Additional deatails may be provided by the use of algorithms i.e :

19
 Pseudocode
 Flowchart
 http://www.tmv.edu.in/pdf/distance_education/bca%20books/bca%20ii
%20sem/bca-222%20%27c%27%20%27programming.pdf
Topic 12

10.2 Error Diagnostics


 Programming errors often remain undetected until an attempt is made to compile or execute the
program.
 The presence of syntactic (or grammatical) errors will become readily apparent once the Run
command has been issued, since these errors will prevent the program from being compiled or
executed successfully.
Exercise
 Describe the different types of programming errors

20

You might also like