Basics of Programming
Basics of Programming
Introduction
This module is divided into 11 Lectures. The module introduces students to basic
programming technology, concepts and best practices. It will also enable students to
design write and test logical structures in order to solve computer problems as well as
enhance usage of computers. The module will provide the learner with a general body
of knowledge about programming languages and the ability to reason critically about
programming languages in software engineering work. Moreover, this course serves
as a background for further study in areas of compiler and interpreter construction as
well as software design and development.
Assignments/self tests
There are two (2) assignments and two (2) continuous assessment tests that the
learner will undertake in the course of the study. The coursework (continuous
assessment tests and assignments) will account 30% of the final score.
Examinations
The examination will be a sit-in and will contribute 70% of the final score.
Recommended Reading
[1].An introduction to programming by Wu Thomas, Norman and Theodore: McGrill
(1997).
[2]. Computer Fundamentals and Programming in C, Pradip Dey and Manas Gosh
(2008)
Additional Reading
[1].C programming language by Brian W. Kernighan, Dennis Ritchie, 2nd ed: prentice
hall(2003).
[2]. C how to program by H.M. Deitel and P.J.Deitel, 3rd ed: Prentice hall(2001).
[3]. An introduction to algorithms by Cormen etal: MIT Press(2000).
Objectives
After successful completion of this module the learner will be able to demonstrate
satisfactory performance in the following areas:
▪ Placing context development in programming languages and reasons for
evolutions.
▪ Recognize and apply appropriate ways to control and manipulate data
▪ Break down and formulate data into structures appropriate for the problem to
be addressed.
▪ Define and apply a range of testing approaches to check correctness of
programming code.
▪ Put programming activities into the context of software development as a
whole with emphasize on the importance of documentation.
Lesson 1: Introduction
Lesson 1: Introduction
1.1. Introduction
Programming methodology is the study of analysis, design and implementation of
programs (software).Organizations and individuals are continually searching for
more efficient ways to perform the software development process. One way of
reducing the time and cost of development is to standardize software programs and
programming process. The benefits of standardized programs are that they are easier
to code, maintain, debug and modify. In the recent years, a variety of techniques
(paradigms) have appeared to minimize the way programmers design and develop
software.
1.2. Lesson objectives
By the end of this lesson, the learner will be able to:
▪ Define a program
▪ Differentiate between a program and an algorithm
▪ Describe some basic terms used in programming field
1.3. Lesson outline
This lesson is organized as follows:
1.1. Introduction
1.2. Lesson objectives
1.3. Lesson outline
1.4. Definitions
1.5. System development life cycle(SDLC)
1.6. Program development life cycle(PDLC)
1.7. Algorithm development activities
1.8. Algorithms and object oriented development approach
1.9. Revision questions
1.10. Summary
1.11. Suggested reading
1.4. Definitions
Programming: This is the process of writing a set of instructions that can be
interpreted and executed by a computer. Programming is one of the stages of system
development. Recall (Problem definition, Requirements collection, Requirements
analysis, system design, coding (programming), implementation and maintenance).
Program: A program, also known as computer program or software program, is an
organized set of instructions that the computer can execute. There are a number of
facets (qualities) to good programs:
2.1. Introduction
In our last lesson we introduced programming concepts like program, programmer,
bug, syntax among others as well as program development life cycle. In our lesson
today we will be discussing programming paradigms. Paradigm refers to a way of
organizing thoughts. Programming paradigm in particular refers to basic structuring
of thoughts underlying the programming activity. There are four basic programming
paradigms: Imperative, functional, object oriented and logic paradigms.
2.2. Lesson objectives
By the end of this lesson the learner will be able to:
▪ State and describe major programming paradigms.
▪ Discuss the structured programming paradigm
2.3. Lesson outline
This lesson is structured as follows:
2.1. Introduction
2.2. Lesson objectives
2.3. Lesson outline
2.4. Imperative paradigm
2.5. Functional paradigm
2.6. Object oriented paradigm
2.7. Logical paradigm
2.8. Structured programming
2.8. Revision questions
2.9. Summary
2.10. Suggested reading
2.4. Imperative paradigm
Programming involves ordered steps for solving a problem (procedural
programming).
Computations are performed through a guided sequence of steps, in which these
variables are referred to or changed. The order of the steps is crucial, because a given
step will have different consequences depending on the current values of variables
when the step is executed.
[i].Based on digital hardware technology and the ideas of Von Neumann
3.1. Introduction
In our previous lesson, we discussed various programming paradigms. In our today’s
lesson we will be discussing program development life cycle and then summarize this
into three phases often referred to problem solving phases.
3.2. Lesson objectives
By the end of this lesson the learner will be able to:
▪ Identify and describe activities of program development life cycle
▪ Group the activities of program development cycle into problem solving phases
3.3. Lesson outline
This lesson is organized as follows:
3.1. Introduction
3.2. Lesson objectives
3.3. Lesson outline
3.4. Program development life cycle
3.5. Problem solving phases
3.6. Structured programming concepts
3.7. Program design
3.8. Revision questions
3.9. Summary
3.10. Suggested reading
3.4. Program development life cycle
A program is a set of instructions that a computer can read and execute. The
programs are written to solve problems or perform a task on a computer. Often
a program is known as a project, application or a solution. The programmers
plan their programs using a sequence of steps called program development life
cycle. This cycle provides a set of phases and steps that are followed by developers
to define, design, develop and maintain a computer program.
[i]. Analysis and specification
[ii]. Design the program
Flowchart
This is a graphical representation of the logical steps needed to carry out a task. Makes
use of symbols to represent the steps.
Pseudo code
▪ It is a representation an algorithm using natural language statements e.g.
structured English. It describes the entire logic of the algorithm.
▪ It is not an executable code.
Guidelines for developing algorithm using pseudo code:
▪ Identify the inputs, processes and outputs
▪ Use natural language e.g. English.
[c].By generality: some languages have variety of features and so can be used for
several purposes while others have limited features and so can only be used for
a given purpose.
Compilation process.
[iii]. Interpreter: An interpreter is a program which translates statements of a
program into a machine code. It processes an internal form of the source program
Interpretive process.
[b]. Editors
These are programs used to enter and modify program in text form. Text editors
format the spacing between words for legibility. They are ideal for structured
languages. Examples Notepad and Wordpad for windows. Unix / Linux require ed, vi
and emacs.
[c].Loaders
These are programs which load machine codes of a program into the system memory.
This program brings the executable file residing in the disk into the memory and
starts it running.
[d]. Linkers
A program which links functions to the libraries for high level languages. It also
combines modules into a program for execution. It searches the program to find
library routines used by the program e.g. printf(). It then determines the memory
locations that code from each module will occupy and relocates its instructions by
adjusting absolute references. It finally resolves references among the files.
[e]. Debuggers
Programs which aid in tracing and debugging a program during its execution.
Program language translation process
5.1. Introduction
In our last lesson we discussed about programming languages and programming tools. In
this lesson, you will be introduced to programming using c. The programming process
begins with the creation of a text file which contains the statements of the program in a
programming language. (This source file is a text file, usually ASCII, which can be
produced with a text editor, such as Windows notepad, or in an Integrated Design
Environment.) This source file is then processed by a special program called a compiler.
Each programming language has its own compiler, and the compiler must be matched to
the hardware and operating system that the new program will run on.
5.2. Objective of the lesson
By the end of this lesson the learner will be able to:
▪ Describe the procedure of writing, compiling and building a program in c.
▪ Describe the general structure of a c program.
▪ Describe program errors.
5.3. Lesson outline
[a]. #include <stdio.h> : This is the first line of the program. It is a preprocessor
command which directs the compiler to include stdio.h file before actual
compilation.
[b]. main(): The main function is where program execution begins.
[c].Comment(s): Any statement enclosed by /*...*/ will be ignored by the compiler
and is put to add additional comments in the program.
[d]. {} : The opening and closing braces are used to enclose a function block in this
case main.
[e]. printf(...) : This library function available in C which prints(outputs) string on
the screen.
[f]. Scanf(…): This library function available in C which takes(input) from the user e.g.
through the keyboard.
[g]. Statement(s): It is a complete instruction that directs the computer to carry
out some
[h]. task. In c statements always end with a semicolon (except for preprocessor
directives such as #define and #include).
[i]. return 0; terminates main()function and returns the value 0 to the operating
system.
Example:
Example: Testing
Even after debugging, the program must be run and different values of a and b entered
to see if it actually calculates the sum as expected.
See the output when testing
Please the enter value of a:
3
Please enter the value of b:
45
The above statement in a c program will produce syntax error as the statement is
terminated with: rather than;
[ii]. Semantic Errors
These are errors reported by the compiler when the statements written in the program
are not meaningful to the compiler. Usually arise from wrongly typed expressions and
formulas.
Example
a+b=c;
In the above statement we are trying to assign value of c in the value obtained by
summation of a and b which has no meaning in a program in c. The correct statement
will be
c=a+b;
[iii]. Linker errors
These are errors reported when an executable file of the program cannot be generated.
This may due to wrong function prototyping, incorrect header files. These errors are
not easy to detect.
#include<math>
}
c) A program errors can be syntax, logic , linker or semantic errors. Classify the
following errors.
i). An indefinite loop
ii). Misspelling keywords
iii). Dividing a number zero
iv). Using undeclared variable.
v). Misspelling function name.
d) Describe the following files as used in a c program.
i). Source code file
ii). Object file
iii). Binary executable file
iv). Header files.
[4 Marks]
e) Describe the process of writing and compiling a c program. [4
Marks]
f) Discuss the following program errors, give an example in each case.
i). Logical error
ii). Run time error
Identify and explain all the information provided in this error message. [4
Mark]
5.11. Summary
In this lesson we have discussed how we can write, compile, debug, run and test a c
program.
We have also describe important files in c. Finally we have identified and discussed
common program errors.
5.12. Suggested reading
[1].An introduction to programming by Wu Thomas, Norman and Theodore: McGrill
(1997).
[2]. C programming language by Brian W. Kernighan, Dennis Ritchie, 2nd ed:
prentice hall(2003).
[3]. C how to program by H.M. Deitel and P.J.Deitel, 3rd ed: Prentice hall(2001).
6.1. Introduction
In our last lesson we discussed how to write, compile, build and debug a program in c.
We also discussed the general structure of a c program. In this lesson you will learn
about the character sets and keywords used in c as well as data types, variables,
constants, qualifiers, statements and expressions in c.
6.2. Lesson objectives
By the end of this lesson, you will be expected to:
▪ Identify various characters used in c program
Initialization of variables
Variables can be initialized (assigned an initial value) in their declaration. The
initializer consists of an equal sign followed by a constant expression as follows:
Syntax
Type variable name= value;
Variables can be initialized in two ways:
[i]. Declare and initialize the variable(s).
int x=8;
char d=’a’;
[ii]. Declare and initialize the variable(s) in separate statements.
int x;
char d;
d=’A’
x=8;
A float-point is used to represent real numbers. That is numbers that can consist of
the integer part and decimal part. The double data type is used to define big floating
point numbers. It reserves twice the storage for the number.
Example
float x, y; //Declaration of float type
y=3.45; //Initialization of float variable
Following table gives detail about standard float-point types with storage sizes and
value ranges and their precision:
Type Storage Value range Precision
size
Float 4 byte 1.2E-38 to 3.4E+38 6 decimal
places
Double 8 byte 2.3E-308 to 15 decimal
1.7E+308 places
long 10 byte 3.4E-4932 to 19 decimal
doub 1.1E+4932 places
le