1. Balochistan University of I.T &
M.S
1
Lecture 6
Programming, Algorithm Design &
Development
CS106
Programming Methodology
& Abstractions
Spring 20023
Balochistan University
of I.T & M.S
Faculty of System Sciences
Sadique Ahmed Bugti
2. Balochistan University of I.T &
M.S
2
Programming
• Programming is a problem-solving activity.
• Programming is more than just writing code.
• Programming has a life cycle.
3. Balochistan University of I.T &
M.S
3
The Programming Life Cycle
1. Define the program requirements
2. Analyze the problem (specification)
3. Design an algorithm
4. Implement the algorithm (write code)
5. Compile and run the code
6. Test and debugging
4. Balochistan University of I.T &
M.S
4
So… what is the problem?
• Specifying the problem statement forces
you to state the problem clearly and
unambiguously in order to gain a clear
understanding of what is required for its
solution.
5. Balochistan University of I.T &
M.S
5
Analyzing the problem
• Analyzing the problem involves identifying
the problem inputs, outputs, and any
additional requirements or constraints
upon the solution.
• List the problem variables and their
relationships (expressed as formulas)
6. Balochistan University of I.T &
M.S
6
What is an Algorithm?
• Programs consist of algorithms.
• An algorithm is just a well-outlined method
for completing a task.
• This high-level abstraction is not actual
code. However, it does express the ideas
of a program, and is called pseudo-code.
7. Balochistan University of I.T &
M.S
7
Etymology?
• The term algorithm is derived from the name of
Abu Abdullah Allah Muhammed ben
Musa al- Khwarizmi (c.783--847),
a Persian mathematician.
– Founder of algebra (Al-jabr)
– It was translated as "Algorismus" in Latin
– Algorthmi de numero Indorum (Latin)
• Algorithms for computing with Hindu numerals, base-10
with 0.
• Derived the decimal system of numeration, using the so-
called “Arabic numerals”.
8. Balochistan University of I.T &
M.S
8
History of algorithms
Euclid, Greek Mathematician, 300BC
– The prototype of an algorithm
– An algorithm for computing the Greatest
Common Divisor of two numbers
– Archimedes (287-212BC) used the algorithm
in his work “The Measurement of a Circle”
– Earliest known attempt to formalize the
algorithmic process.
9. Balochistan University of I.T &
M.S
9
History of algorithms
• Rhind Papyrus (Egypt, 1650BC),
describes a “rule” for calculating the area
of a circle given its diameter
10. Balochistan University of I.T &
M.S
10
Designing an algorithm
• Don’t attempt to solve every detail of the
problem at the beginning.
• Use top-down design:
– Also known as stepwise-refinement, it
consists of repeatedly decomposing a
problem into smaller problems.
1. List the major steps, or sub-problems
2. Solve the original problem by solving
each of the sub-problems.
11. Balochistan University of I.T &
M.S
11
e.g. Area of a Circle
• An algorithm for calculating the area of a
circle:
area = p r2
could be as follows:
– Obtain the value of the radius of the circle
– Obtain the value of p
– Square the radius and multiply by p
12. Balochistan University of I.T &
M.S
12
Implementing the algorithm
• Write the algorithm as a C program
• Convert each algorithm step into one or
more statements in a programming
language.
13. Balochistan University of I.T &
M.S
13
e.g. Area of a Circle
#include <stdio.h>
#define PI 3.14159
void main(void)
{
double radius; /* radius of the circle, cm */
double area; /* area of the circle, cm^2 */
/* Get the radius of the circle */
printf(“Enter the radius in cm> “);
scanf(“%lf”, &radius);
/* Calculate the area of the circle */
area = PI * radius * radius;
/* Display the area */
printf(“The area is %f cm^2.n”, area);
getch();
}
14. Balochistan University of I.T &
M.S
14
An Example
• Requirements:
– Write a program to convert miles to kilometers.
• Analysis:
– Conversion from one system of measurement to
another.
– Convert distance measurements from miles to
kilometers (km).
– Input distance in miles
– Output distance in kilometres
– Need to know the relationship between miles and km.
• 1 miles equals 1.609km
15. Balochistan University of I.T &
M.S
15
An Example
• – List the three major steps of the algorithm:
Get the distance in miles
Convert the distance to kilometers
Display the distance in kilometers
– Step needs some refinement
• The distance in kilometers is 1.609 times
the distance in miles.
16. Balochistan University of I.T &
M.S
16
Case Study: Implementation
#include <stdio.h>
#define KMS_PER_MILE 1.609
int main(void)
{
double miles; /* distance in miles */
double kms; /* distance in kilometres */
/* Get the distance in miles */
printf(“Enter the distance in miles> “);
scanf(“%lf”, &miles);
/* Convert the distance to kilometres */
kms = KMS_PER_MILE * miles;
/* Display the distance in kilometres */
printf(“That equals %lf kilometres.n”, kms);
}
17. Balochistan University of I.T &
M.S
17
Style
• A properly designed program is easy to
read and understand, and is esthetically
pleasing.
• Good coding style is essential to the art of
programming.
– Good style uses white spaces and comments.
18. Balochistan University of I.T &
M.S
18
Indentation
• Indentation is used to make programs easier
to understand.
– In C indent one level for each new block, or
conditional/loop statement.
– For example:
if (marks < 40)
{
printf(“Fail”);
}
The if statement is the outermost, the
statements inside the if are at the next level.
19. Balochistan University of I.T &
M.S
19
Clarity
• A program should be concise and easy to
read:
– Organize code into paragraphs
– Begin a paragraph with a topic-sentence
comment
20. Balochistan University of I.T &
M.S
20
Simplicity
• Your program should be simple. Some general
rules of thumb are:
– A single function should not be longer
than two or three pages.
– Avoid complex logic like multiple nested
ifs. The more complex your code, the
more indentation levels you will need.
– C statements should not go on forever!
If an equation is going to be longer than
one or two lines, split it into two shorter
equations.
21. Balochistan University of I.T &
M.S
21
An example of what not to do!
#include <stdio.h>
main(t,_,a)
char *a;
{return!0<t?t<3?main(-79,-13,a+main(-87,1-_, main(-86, 0,
a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a
)&&t == 2 ?_<13 ?main ( 2, +1, "%s %d %dn"
):9:16:t<0?t<-
72?main(_,t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%
+,/w#q#n+,/#{l,+,/n{n+ ,/+#n+,/#;#q#n+,/+k#;*+,/'r
:'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d
}rw' i;# ){n l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw'
iK{;[{nl]'/w#q# n'wk nw' iwk{KK{nl]!/w{%'l##w#' i;
:{nl]'/*{q#'ld;r'}{nlwb!/*de}'c ;;{nl'-
{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+; #'rdq#w! nr'/ ')
}+}{rl#'{n' ')# }'+}##(!!/") :t<-50?_==*a
?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a
+1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-
61,*a, "!ek;dc
i@bK'(q)-[w]*%n+r3#l,{}:nuwloca-O;m
22. Balochistan University of I.T &
M.S
22
Optimization
• Optimization is the art of going through a
program and making code more
efficient, so that it runs faster.
– using a more efficient algorithm
• A word on optimization don’t!
• Most programs do not need to be
optimized!
23. Balochistan University of I.T &
M.S
23
Program Aesthetics
• Rule: Use white space to break a function into
paragraphs.
• Code paragraphs
– A C program consists of statements, and
multiple statements on the same subject form a
conceptual block or “paragraph”.
– You can separate “paragraphs” in C using a
blank line.
– Statements are grouped together if they
belong together logically.
24. Balochistan University of I.T &
M.S
24
Program Aesthetics
• Rule: Put each statement on a line by itself
• There are aesthetic issues at the level of the
sentence; or in C, the statement.
– A statement expresses a single thought, idea, or
operation.
– Putting each statement on a line by itself makes
the statement stand out and reserves the use of
indentations for showing program structure.
25. Balochistan University of I.T &
M.S
25
Program Aesthetics
• For example:
x = 12; y = x * 2; z = PI * y
* y;
• is better expressed using the form:
x = 12;
y = x * 2;
z = PI * y * y;
26. Balochistan University of I.T &
M.S
26
Program Aesthetics
• Rule: Avoid very long statements. Use
multiple shorter statements instead.
– A clearly written C statement should not go on
forever. Complex statements can easily be
divided into smaller, more concise statements.
27. Balochistan University of I.T &
M.S
27
Program Aesthetics
• For example
m = 3*(12*(1- (lambda/ (2*(lambda+mu))))–1)*LOG(x)–(18* (1–
(lambda/(2*(lambda+mu))))–2);
is better expressed using the form:
v = (lambda/(2*(lambda+mu)));
alpha = (12*(1–v)–1);
beta = (18*(1–v)–2);
m = 3*alpha*LOG(x)–beta;
28. Balochistan University of I.T &
M.S
28
Program Aesthetics
• Rule: Write all programs in lowercase.
– Lowercase text is easier to understand.
– Uppercase letters can be used in special
circumstances, such as declaring constant
values.
29. Balochistan University of I.T &
M.S
29
Program Aesthetics
• Rule: Keep program files to no longer than
2000-3000 lines.
• Rule: Keep all lines in your program down
to 72 characters or fewer.