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

1. Introduction and Program design

these are notes of c programming

Uploaded by

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

1. Introduction and Program design

these are notes of c programming

Uploaded by

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

DEFINITIONS

Computer program – It refers to a series of coded instructions written and fed to the computer to help in solving
a particular problem.

Programming language – this refers to a set of rules, symbols and special words used to construct a computer
program. Programming language rules consist of the following:
 Rules of Syntax, which specify how valid instructions, are written in the language.
 Rules of Semantics, which determine the meaning of the instructions (what the computer will do).
Generally it is a notation for communicating to a computer what we want it to do.

Source code – This is a program written in a human readable version.


Object code - The machine language version of a source program.
Linking – Process where the intermediate code (object code), the start up code (code that acts as an interface
between the program and the operating system) and the code for library routines used in the program are
combined in a single executable file. It is achieved through the use of a program known as a linker.

LEVELS OF LANGUAGES
i). High Level Languages – Contain statements that are written in English words and symbols. Such languages
are not designed specifically for any brand of computer. High-level languages permit faster development of
large programs. The final program as executed by the computer is not as efficient, but the savings in
programmer time
Features
 Provides notation to describe problem-solving strategies rather than organize data and instructions at
machine-level.
 It requires a compiler or interpreter.

ii). Low Level Languages (Assembly Language) – Contain statements that are written using mnemonics
(codes suggesting their meaning and therefore easier to remember) to present operations and addresses that
relate to their main memory and storage registers of a computer.
Features
 Uses Symbolic Names
 Low-level programming wastes effort in coding a solution rather than solving a problem.
 It is difficult to build and maintain large programs.
 It requires assembler.

iii). Machine Languages – Is a machine oriented language in which instructions correspond or resemble a
computers own native machine code consisting of strings of 1’s and 0’s, stored as binary numbers. The
instructions are interpreted and executed directly by the computer. The main problems with using machine
code directly are that it is very easy to make a mistake, and very hard to find it once you realize the mistake
has been made.

PROGRAM PROCESSING
A program written in a high level language (source code) can only be run in its machine code equivalent
format. This is achieved by use of a computer program translator.

Computer program translator


A translator is a program that converts statements written in one language to statements in another language.
There are three types of translators namely
 Assemblers
 Interpreters
 Compilers

Programming Notes ~ Wainaina Page 1 of 12


i) Assembler
This is a program that translates assembly language program into machine code. The final result is a machine-
language program that can run on its own at any time; the assembler and the assembly-language program no
longer needed. If an assembly-language program needs to be changed or corrected, it is necessary to make the
changes to the source code and then re-assemble it to create a new object program.

ii) Interpreter
This is a program that translates the statements in high-level language as the program is running. This means
they are translated into machine language and then executed as the program is running
Characteristics of Interpreters:
 Reads the source code one line at a time performing the specific instructions contained in that line
 Slower, but often easier to develop
 Allows runtime Flexibility
 More appropriate for use on the web
 Good error messaging

iii) Compiler
This is a program that translates a high level language into machine oriented language. Once the program has
been compiled, the resulting machine code is saved separately, and can be run on its own at any time. As with
assembly-language programs, updating or correcting a compiled program requires that the original (source)
program be modified appropriately and then recompiled to form a new machine-language (object) program.
Sometimes the object code cannot be directly executed. Various library files must be “linked in” using another
special program called a linker, which produces executable code.

Roles of a Compiler
i). It converts the source code into object code (instructions in machine language).
ii). It checks the source code for syntax errors i.e. object code can never be produced if the source code
contains syntax errors
Characteristics of Compilers:
 Reads the entire program and converts it into object code/ binary code/ machine code.
 Runs faster
 Typically has more functionality and is easier to optimize
 Best choice for complex programs that need to be fast
 Poor error messaging

Libraries
Libraries (in computer programming terms) contain chunks of precompiled (object) code for various functions
and procedures that come with a programming language that requires compilation, e.g. functions and
procedures to facilitate I/O.

PROGRAM BUGS
It refers to program errors. They are caused by three main types of errors, namely
 Syntax errors
 Logic errors
 Semantic errors

i). Syntax /compiler errors


They are errors in a language caused by not following the set of rules that govern the way in which words,
symbols, expressions are formed and combined.

Programming Notes ~ Wainaina Page 2 of 12


ii). Logic errors
They are errors due to incorrect use of control structures, incorrect calculations or omission of a procedure
e.g. indefinite loop in a program or generation of negative values instead of positive values etc.

iii). Semantic errors


They are errors due to illegal expressions that the computer cannot make meaning of i.e. data overflow due
to attempt to assign a value to a memory location smaller than the value, division by zero etc.

Debugging Tools
To assist in error detection many debugging tools exist. Some of these allow the user to analyze the core dump
that occurs in the event of a fatal error. (A core dump describes the “state” of a program when a fatal error
occurs). Others allow programmers to step through and execute a program line by line to support analysis of its
execution.

STRUCTURED PROGRAMMING
This refers to a collection of programming techniques for implementing program and control abstractions in a
hierarchical manner. It emphasizes in programming using sequences, conditions and repetition (no jumps and
“goto” statements). Structured programming methodology enhances readability and hence maintainability.

STRUCTURED PROGRAMMING TECHNIQUES


Structured programming methodology is accomplished by use of three techniques:
i). Modular Design. Modularization of a program involves the identification, definition, and construction of
relatively independent blocks of program code, called modules. Each module performs a clearly defined
task and is characterized by a single entry point and a single exit point.
Modular Advantages.
a) It enables the use of modules that contain standard procedures through the program, thus saving
development time.
b) Testing of individual modules in isolation makes tracing of mistakes easy.
c) Amendments of single modules don’t affect the rest of the program.

ii). Top down Design. The top-down program-design technique assumes a modular program and organizes
these modules into a hierarchical structure. Top-down design is characterized by stepwise refinement and
delayed decision. An overview of a modular, hierarchical program is depicted in the figure below.

At any level in the hierarchy, the program can be regarded as complete and tested for correctness.

iii). Structured Theorem.


It states that: - it is possible to write any program by using three control structures of sequence, selection
and repetition structures
This helps by both eliminating the use of goto statements and establishing a structured framework for
representing the solution

Programming Notes ~ Wainaina Page 3 of 12


C LANGUAGE
C was designed by Denis Ritchie at Bell laboratory in the early 1970’s. The earlier version of C called B was
written by Ken Thompson who adapted it from Martin Richard’s BCPLC (Basic combined Program Language).
Denis Ritchie improved BCPL and named it C, which is the second letter of BCPL.

Reasons for learning C


 C is easy to master since it is extremely small and contains only 32 words
 One of the most portable languages, ranging from PCs to large scale mainframes
 Programs written in C execute faster than the programs written in most other languages
 Efficient and compact code as direct manipulation of bits, bytes, words and pointers are allowed.
 One of the most flexible languages that allow many types of programming purposes
 C is a language with high level features that support structured methodology
 Communication with the hardware is easily possible using C. i.e C stands in between Low Level Languages
(LLL) and High Level Languages (HLL) and it was designed to have both: a relatively good programming
efficiency (as compared to Machine oriented languages) and a relatively good machine efficiency (as
compared to Problem oriented languages).

C Programming Environment
The C programming typically go through the following four phases:
Phase 1
Involves creation of a C program in text mode (source code)

Program keyed in from program stored in disk in


Editor
a computer text mode

Amendments can be made to the program during this phase

Phase 2.
Involves translating of the program using a compiler

Program stored in disk program stored in disk in


compiler
in text mode in machine oriented language.

If the compiler flags an error in the program, then the programmer must return to phase one and amend the
lines of text that have errors.

Phase 3
Involves the linking/loading of the compiled program

Program stored in disk Link/load machine code program


in machine oriented language stored in memory.

Phase 4
Involves program execution
Machine code program
Data running in memory results.

The program might contain run time errors, in which case the operating system will terminate the program
prematurely. To modify the program, it will be necessary to repeat the 4 phases again.

Programming Notes ~ Wainaina Page 4 of 12


PROGRAM DESIGN
Developing a computer program involves devising a solution to a problem. Good program only come from
good program design and good program design only comes from giving sufficient thought and efforts to the
early stages of programming i.e. a programmer cannot solve a programming problem satisfactorily if the
problem has not been properly stated. The longer an error goes undetected the more costly it is to correct hence
early stages of program design are very important. Program design aims at the following:
Reliability: - The program can be depended upon always to do what it is supposed to do
Maintainability: - The program will be easy to change and modify when need arises
Readability: - The program will be easy for the program to read and understand
Portability: - The program will be transferable to a different computer with minimum of modification.
Performance: - The program causes tasks to be done quickly and efficiently
Storage saving: - The program is not allowed to be necessarily long

PROGRAM DEVELOPMENT CYCLE (STAGES IN PROGRAMMING)


The following are stages necessary for producing a program.
i). Identifying The Problem (Requirement specification)
ii). Outlining the Solution
iii). Developing the Outline into An Algorithm
iv). Testing for correctness of the Algorithm
v). Translate the design into a program
vi). Testing And Debugging
vii). Documentation And Maintenance Of The Program

i) Identifying The Problem (Requirement specification)


This step involves the careful reading of the problem until the programmer understands properly what is
to be done. A good specification will usually specify what processing is needed by giving the exact
relationship between the outputs and inputs from which there are derived rather than prescribing how the
program should be written

ii) Outline the Solution: -This is where the programmer may decide to break the problem into smaller tasks
or steps. It usually takes the steps of hierarchy or structure chart.

iii) Develop The Outline Into An Algorithm (Algorithm Design)


This is where the outline developed in step two is expanded into a set of precise steps which describes
exactly the tasks to be performed under the order in which they are to be carried out.

iv) Test For Correctness Of The Algorithm


This is the step where data is “walked” through the algorithm to check that the instructions described in
the algorithm will do what there are supposed to do. If logical errors are detected the are easily corrected

v) Translate the design into a program: -This step involves translating the algorithm into a program using
an appropriate programming language

vi) Testing And Debugging


Debugging is the detection and correction of errors that may exists in the program. Program testing
involves creating test data designed to produce predictable output and comparing the actual output with
the predicted output. Careful design in the early stages of the programming will help to minimize these errors

vii) Documentation And Maintenance Of The Program


Documentation involves external documentation such as hierarchy charts, the algorithm and test data
reports. It also involves internal documentation coded in the program.
Program Maintenance involves changes that may be made in a program.

Programming Notes ~ Wainaina Page 5 of 12


ALGORITHM DESIGN
An algorithm is a step-by step procedure to be followed in order to obtain a solution to a problem. To be useful
as a basis for writing programs, the algorithm must have the following characteristics:
 Must arrive at the correct solution within a definite time.
 Must be clear, precise and unambiguous.
 Must be in a format that leads itself to an elegant implementation in programming language.
The following are algorithms design tools.
i). Flowcharts
ii). Pseudocode

i). Flowchart
This refers to the graphical representation of an algorithm or a portion of it. It used to convey in diagrammatic
form, the logic, processing operations and flow of control required of a computer program
Flowcharts are used by programmers in two ways:
a) To plan the structure of the program before its written
b) To describe the structure of the program after its been written.
Flowcharts are made up of boxes of standard shapes linked to show the order of processing. Each box contains a
brief note stating what an application is when applicable and further explanation can be given.
The following are the standard set of symbols used to draw flowcharts

i). Start/End
Start
Used to indicate the start or end
End of a process

ii). Process

Process This represents operations on data.


Details are written in the rectangular
box.

iii). Input/output

Input and output operations are


Input/output
shown in the parallelogram.

iv). Decision.

Decisions The diamond shape is used to show where a decision or a test is to be


made between two alternatives.

Programming Notes ~ Wainaina Page 6 of 12


v). Connectors

A A small circle containing a number or letter is used to split a large flowchart into
small parts.
A

Using Flowcharts to represent Control structures.


Control structures refer to statements that are used to control the flow of program's execution. The following
represents the three control structures using flowchart

a) Sequence:

Statement(s) -A
B Statement(s) -B
Statement(s) -C.

b) Iteration (Repetition)

While Condition is True


False Task Statement(s)
Condition False

True
True
Task(s)

Programming Notes ~ Wainaina Page 7 of 12


c) Selection (Using If Constructs)
i) Double selection.

True False
If condition is true
Condition Task statement(s) A
Else
Task statement(s)
Task(s) B
Task(s) A

ii) Single selection.

False
Condition

If condition is true
True Task statements(s)
End if
Task(s)

iii) Multiple selection

False
True
1 False
False
True
2 FalseFals
e
Task (A)
True False
Task (B) 3 False
Task (C)

Programming Notes ~ Wainaina Page 8 of 12


If condition1 is true Or a better alternative If condition1 is true
task 1 statement(s) A Task statement(s) A
Else Else If condition2 is true
If condition2 is true Task statement(s) B
Task statement(s) B Else If condition3 is true
Else Task statement(s) C
If condition3 is true ……………………
Task statement(s) C ……………………
Else End if
……………………
……………………
End If
End If
End if

Selection (Using Case Structure.)


The multiple selection construct using the if constructs, leads to a rather complicated structure as shown as
above. A preferable solution is the use of the case structure as shown below

Condition

1=true 2=true 3=true

Task(s) A Task(s) B Task(s) C

Case of condition
Case 1:
Task statement(s) A
Case 2:
Task statement(s) B
Case 3:
Task statement(s) C
………
………
End case

The advantage of flowchart is that the patterns of logic are easy to form.
The main disadvantage is that it allows the designer of an algorithm to develop structures that don’t conform to
the 3 control constructs which could lead to inelegant and dangerous implementation in whatever programming
language used i.e. they are incapable of being expressed in structured syntax in any programming language and
would lead to excessive use of goto’s and their associated labels.

Programming Notes ~ Wainaina Page 9 of 12


Exercise
Draw a flowchart to illustrate the solution of the following program.
“A value is input from the keyboard. If it’s negative, the word NEGATIVE is displayed, if it’s positive, the
word POSITIVE is displayed, otherwise the word ZERO is displayed”

Start

Input
Number

True
False

Number < 0

True False
Print
Negative Number > 0

Print Zero
Print Positive

End

Programming Notes ~ Wainaina Page 10 of 12


ii). PSEUDO CODE.
It’s a non-executable program code used as an aid to develop and document structured programs. It aims at
providing programmers with a clear and unambiguous process specifications. Process in pseudo code is
expressed as a combination of sequence of events, decisions that need to be implemented and actions that may
be repeated.
The following are advantages of pseudo code over flowchart
i) Statements are written in English text.
ii) It only allows the three control structures of sequence, selection and iteration, i.e. the technique does not
permit the use of goto statements and their associated labels
iii) Many languages such as PASCAL have syntax that is almost identical to pseudo code and makes the
transition from design to coding extremely easy

Rules for writing pseudocode.


1. Simple English should be used to write the statements.
2. Every English statement or instruction should be on its one line i.e. separate line.
3. Indention of the statements should be done appropriately. Keywords on indention are used to signify a
particular control constructs.
4. Each set of instruction is written from top to down with only one entry and one exit.
5. Groups of statement may be formed into modules and that group given the name.

Example
Develop a pseudocode algorithm for a program that accepts each of the average marks of 10 students in a class
and computes the sum and the average mark of the class. The program should display the average mark of the
class.

Begin
Set total to zero
Set counter to one
While counter is less than or equal to ten
Input student average mark
Add average mark to total
Add one to the counter
Set the class average to total divided by ten
Display the class average
End

Counter 1 2 3 4 5 6
Total 0 70 125 200 260 300
Average mark 70 55 75 60 40
Class average 60

Exercise
Develop a pseudocode algorithm for a program that accepts each of the average marks of students in a class and
computes the sum and the average mark of the class. The program should display the average mark of the class.

Desk Checking.
It involves tracing through the logic of the algorithm with some test data. It follows the following six simple
steps.
i) Choose s simple input test cases that are valid. Two or three test cases are usually efficient.
ii) Establish what the expected result should be for each the case data.
iii) Make a table of the relevant variable names within the algorithm on the paper.

Programming Notes ~ Wainaina Page 11 of 12


iv) Take the first test data case through the algorithm keeping a step by step record of the contents of each
variable in the table as the data passes through the algorithm.
v) Repeat step four using other test data cases until the algorithm has reached the end.
vi) Check that the expected result established in step two matches the actual result in step 5.

Programming Notes ~ Wainaina Page 12 of 12

You might also like