Introduction To Program Design Notes
Introduction To Program Design Notes
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.
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.
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).
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).
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.
Third Generation programming languages are High level Programming languages like JAVA & C.
2
1.3 Types of Programming Languages
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.
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.
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
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.
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
Operand Meaning
&& And
|| Or
5
Box 4
Program
6
Topic 3
7
2.2 Software Development
Source Program
Edit Link
Execute
8
Topic 4
Data Type
char int float double Linear data Structure Non-linear data structure
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
9
2.6 Expressions and Statements
Declarations
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
Example
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
Topic 6
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.
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 …
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
Account (Structure)
(Member)
Acct_No
(Member)
(Member)
Acct_Type (Member)
Cust_Name[50]
Balance
18
float Balance;
};
A structure is an abstract data type (ADT) and its variable is declared the same way as
primary data types:
Example
struct Account {
int Acct_No;
char Acct_Type;
char Cust_Name[50];
float Balance;
};
struct Account Customer;
Topic 11
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
20