2. INTRODUCTION
īĸ Programming is a required skill today,
just like English and Maths.
īĸ It teaches a variety of skills that are
important in all kinds of professions:
ī critical reading,
ī analytical thinking,
ī creative synthesis, and
ī attention to detail.
īĸ It provides immediate feedback, leading
to exploration, experimentation, and self-
evaluation.
3. What is programming?
īĸ Process of coming up with computer
programs.
īĸ But what is a program?
ī A PROGRAM is a set of instructions that a
computer can follow to do something.
īĸ Programming = process of coming up
with instructions that a computer can
follow to do something.
4. âĻ What is programming?
īĸ As a process, it involves:
īļUnderstanding a problem for which a
solution is required;
īļDesigning a solution;
īļWriting or coding the program in a
programming language;
īļTesting the code;
īļCorrecting any errors (debugging); and
īļMaintaining the program.
5. âĻ and Structured programming?
īĸ Programming that:
ī Enforces a logical structure on the
program to make it efficient and easier to
maintain/modify.
ī Implements modules (procedures /
functions), each fulfilling some function.
ī Uses basic programming constructs -
sequence, selection and iteration (no goto
statements in âspaghetti codeâ).
6. Programming Language
īĸ Programming requires a programming
language:
ī a set of words, codes, and symbols that
allow a programmer to give instructions to
the computer.
ī Each language has rules (or syntax) for
writing the instructions.
7. âĻ Programming Language
īĸ There are many programming
languages â so many! (like human
languages)
īĸ They may be classified (in many
ways, including) into THREE broad
categories:
īļ Machine Language;
īļ Assembly Language; and
īļ High-level Languages.
9. Machine Language
īĸMade up of streams of 1s and 0s.
īĸPrograms written as series of 1s and
0s is said to be in machine language:
10. âĻ Machine Language
īĸ Machine language is the only language
understood by computer hardware.
īĸ Writing programs in machine language
is difficult and cumbersome â
instructions are difficult to remember.
The only language understood by computer
hardware is machine language.
11. âĻ Machine Language
īĸ Each computer has its own machine
language; rules of language differ from
one computer hardware to another.
īĸ Note: Machine language is also
referred to as First Generation
programming language.
First generation programming
Language; also low-level languages
12. Symbolic / Assembly Language
īĸ Developed in 1950s to reduce
programmersâ burden â by Grace Murray
Hopper, US naval officer.
īĸ She developed the concept of a special
computer program that would convert
programs into machine language.
13. âĻ Symbolic / Assembly Language
īĸThe program mirrored the machine
languages using symbols, or mnemonics,
to represent the various machine
instructions.
īĸThe languages developed in this manner
were known as symbolic languages.
īĸAn assembler translates symbolic code
into machine language â thus Assembly
Language.
15. High-Level Languages
īĸ Symbolic language improved programming
efficiency, but still tedious and hardware focused.
īĸ High-level languages were developed to further
improve efficiency and change focus from
computer to the problem being solved.
īĸ High-level languages:
ī produce portable programs;
ī easier to learn;
ī require less time to write;
ī easier to maintain; and
ī provide better documentation.
17. âĻ example
īĸ Programs written in high-level languages have
English-like statements (and familiar math symbols)
and must be converted to machine language (1s and
0s).
īĸ The process of converting them is called compilation.
īĸ Examples of high-level languages are FORTRAN,
COBOL, C, BASIC, Pascal, ALGOL, and many
others.
18. Overview of some high-level languages
a) BASIC
īĸ Beginnersâ All-purpose Symbolic Instruction Code.
īĸ Developed by John Kemeny & Thomas Kurtz in
1964 â for beginners â easy to learn.
īĸ Example: 5 REM Program to compute sum of 10 numbers
10 LET s = 0
20 FOR i=1 TO 10
30 READ n
40 LET s = s + n
50 NEXT i
60 PRINT "The sum of the given numbers ="; s
70 DATA 4, 20, 15, 32, 48
80 DATA 12, 3, 9, 14, 44
100 END
19. b) Pascal
īĸ Introduced in 1971 by Niklaus Wirth in Zurich becoming
the first language to fully embody structured
programming concepts.
īĸ Example:
PROGRAM SUMNUM(INPUT, OUTPUT);
(* Program to Compute the sum of 10
numbers *)
(* Declaration of variables *)
VAR Sum, N :REAL;
VAR i :INTEGER;
BEGIN
Sum := 0;
FOR i := 1 TO 10 DO
BEGIN
READ (N);
Sum := Sum + N;
END;
WRITELN ('THE SUM OF GIVEN
NUMBERS =', SUM);
END
20. Assignment 1:
Read about and write short notes on the following
high-level languages:
ī FORTRAN;
ī COBOL;
ī ADA;
ī ALGOL; and
ī PL/1.
24. Program Development
īĸMultistep process that requires that we:
īUnderstand the problem â output
required; input available; steps to follow
in processing (algorithm).
īPlan the logic of the solution (or
program). Three tools will help in this
task:
ī Flowcharts;
ī Pseudocode; and
ī Structure charts.
25. âĻ Program Development
īCode or write program statements in a
programming language e.g. BASIC, C,
Pascal. This may be done on paper.
īKey the program into the computer
(use text editor).
īCompile, test and debug the program.
īComplete the documentation: charts,
listings, manuals.
26. ... Programming Steps â
1) Understand the Problem
īĸRead the problem or talk to & listen to the
person who posed the problem.
īĸIdentify the problem inputs, outputs, and
any additional requirements or constraints:
ī Inputs â data to work with;
ī Outputs â desired results;
ī Other Requirements/Constraints â
Relationships between variables, format of
results e.g. Table.
27. ... Understand the Problem
Example:
Compute and display the total cost of
oranges given the weight of oranges
purchased and the cost per 1000g of
oranges.
28. ... Understand the Problem
īĸFrom the problem, we identify:
Compute and display the total cost of
oranges given the weight of oranges
purchased and the cost per 1000g of
oranges.
Inputs:
īĸQuantity of oranges purchased (in Kgs);
īĸUnit cost of oranges (in Kshs.).
29. ... Understand the Problem
īĸFrom the problem, we identify:
Compute and display the total cost of
oranges given the weight of oranges
purchased and the cost per 1000g of
oranges.
Outputs:
īĸTotal cost of oranges (in Kshs).
30. ... Understand the Problem
Other requirements/constraints:
īĸTotal cost = Weight (Kgs) X Unit Cost
(Kshs).
īĸThe process we went thru (extracting
essential variables and their relationships)
is called ABSTRACTION.
31. ... Understand the Problem
Exercises:
īĸ Read two numbers from the keyboard,
sum them up, and display the result.
īĸ Eldy surveyors want a program that
converts distances in miles to kilometres
and showing the result on screen.
32. 2) Plan the logic of (Design) the solution
īĸInvolves developing a list of steps
(algorithm) to solve the problem.
īĸUsually the most difficult part of problem-
solving process.
īĸAs noted above, 3 tools are important
here:
īĸStructure charts;
īĸPseudocode; and
īĸFlowcharts.
33. Structure Chart
īĸAlso called hierarchy chart.
īĸIt shows the functional flow through a
program â parts (modules) of a program
and how they are related e.g.
34. âĻ structure chart
īĸIt is drawn before a program is written â
thus acting as an architectural blueprint â
more like a house plan.
īĸIn business world, once a structure chart
is completed, a structured walkthrough is
conducted â where a review panel is
taken through the structure to understand
how programmers plan to solve the
problem and attain the objectives they
set out to achieve.
35. âĻ structure chart
Question 1:
Develop a structure chart for a program
that is meant to obtain two numbers
from a user, multiply the numbers and
show the result on screen.
37. Pseudocode
īĸ Is an English-like statement; part of program logic.
īĸ It describes what the program will do in precise
algorithmic detail e.g.
Pseudocode
English-like statements that follow a loosely defined syntax
and are used to convey the design of an algorithm.
38. Question 2:
Write a pseudocode for a program for calculating the
amount of tax (at 15% of value for property worth Kshs
100,000 and above, and 12% otherwise) of a property.
Assume that the user wants to determine taxes for more
than one property.
Question 3:
Write a pseudocode for a program for calculating
the cost of flooring the living areas (slide 33).
Reading:
Do a literature search in the Library or on the Internet about pseudocode
as a design and documentation tool. Read and attempt the questions
above.
39. Flowchart
īĸProgram design tool that uses standard
graphical symbols to represent logical
flow of data thru a function/program.
īĸIts primary purpose is to show the design
of an algorithm.
40. âĻ Flowchart
īĸIt frees a programmer from the syntax and
details of programming to concentrate on
details of the problem to be solved.
īĸIt gives a pictorial representation of an
algorithm â helps one to think of a
problem pictorially.
43. Flowcharting Symbols:
Two basic categories:
1) Auxiliary symbols
ī enhance readability or functionality of a
flowchart.
ī They do not directly show instructions or
commands.
a) Terminators:
START STOP
b) Flow lines:
Rule: One entry/exit!
Rule: Start/Stop has one
Exiting/entering line!
46. 2) Primary Symbols:
īĸ Show instructions or actions needed a problem presented in
the algorithm.
īĸ They show all three structured programming constructs:
sequence, decision, and repetition.
a) Sequence Symbols:
Sum â x + y
i) Assignment
Statement:
ii) Input/Output
Statement:
Read x
48. âĻ sequence symbols:
iii) Module-call statement:
AVRG(ave, a, b, c)
Startv
Stop
Read (a)
Read (b)
Print
Average
Read (c)
AVRG(ave, a, b, c)
Flowchart for this
is elsewhere (below)
50. b) Selection statement symbols
īĸ Those for specifying conditional statements (that
allow execution of selected statements and
skipping of others).
īĸ There are 2 selection statements (decisions):
īĸ Two-way selection;
īĸ Multiway selection.
Condition Number > 10
TF
T â to the right;
F â to the left;
NB: Not good practice to have any to bottom!
2-way:
51. âĻ selection Startv
Stop
Read (a)
a > 10
TF
Write
(newnum)
newnum â a - 10
NOTE: Only one statement (or
null) in each branch is allowed & it
may be a compound statement
54. Question 4:
Design an algorithm (flowchart) that reads an integer. If
the integer value is greater than 10, it subtracts 10 and
writes the original number and the result. If the number
is less than 10, it displays the message âThat number is
less than 10â.
Question 5:
Design a flowchart for a program that reads a mark
between 0 and 100 and prints a corresponding letter
grade.
55. c) Looping Statements symbols
īĸ Iteration/repetition (for, while, do..while)
a) for
- Counting loop.
- pre-test, so body may
never be executed.
- good when number of
times to loop is known.
56. âĻ for loop actions
Question 6:
Design a flowchart (or algorithm) for a program that
reads 20 numbers and print their sum.
58. b) while
âĸ Not a counting loop;
âĸAlso pre-test loop â body
may not be executed.
âĸGood when number of
times to loop is not known.
Question 7:
Design a flowchart for a program that reads numbers from
the keyboard and prints their total. We donât know how
many numbers the user will input. However, all numbers
are positive numbers.
60. c) do...while
âĸ this is a post-test loop i.e. the
condition is tested at the end of
the loop.
âĸBody of the loop is executed at
least once
Question 8:
Design a flowchart for a program that reads and
processes numbers between 1 and 5. How it processes
the numbers is not important at this stage.
62. So far in Program Development âĻ
īĸ Multistep process that requires that we:
ī Understand the problem â output required; input
available; steps to follow in processing (algorithm).
ī Plan the logic of the solution (or program). Three tools
will help in this task:
ī Flowcharts;
ī Pseudocode; and
ī Structure charts.
ī Code or write program statements in a programming
language e.g. BASIC, C, Pascal. This may be done on
paper.
ī Key the program into the computer (use text editor).
ī Test and debug the program.
ī Complete the documentation: charts, listings, manuals.
63. Exercises
Write a pseudocode and develop a flowchart for each of
the following problems:
1) Calculate the circumference and area of a circle (PI =
3.142).
2) Compute and display the letter grade of a studentâs
mark in a certain course.
3) Compute and print the net pay of an employee given
the number of hours worked and the hourly rate of
pay.
4) Convert the temperature given in degrees fahrenheit
to degrees celcius given that:
Degrees Celcius = (Degrees Fahrenheit â 32) * 5/9
64. Coding
īĸ Writing instructions in a programming language â
high-level language.
īĸ So we need to learn the language, its structure and
syntax.
īĸ Choice: C