You are on page 1of 19

Algorithm Design & Development

What is an Algorithm?

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 socalled Arabic numerals.
3

Designing an algorithm
Dont 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.
Balochistan University of I.T & M.S 4

e.g. Area of a Circle


An algorithm for calculating the area of a circle:
area = T r2

could be as follows:
Obtain the value of the radius of the circle Obtain the value of T Square the radius and multiply by T

Balochistan University of I.T & M.S

Implementing the algorithm


Write the algorithm as a C program Convert each algorithm step into one or more statements in a programming language.

Balochistan University of I.T & M.S

Testing, testing 1,2,3


Test the program to verify that it works Dont be discouraged by initial failures! failure is part of the process

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
Balochistan University of I.T & M.S 8

An Example
List the three major steps of the algorithm: X Get the distance in miles Y Convert the distance to kilometers Z Display the distance in kilometers Step needs some refinement The distance in kilometers is 1.609 times the distance in miles.
Balochistan University of I.T & M.S 9

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); }
Balochistan University of I.T & M.S 10

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.

Balochistan University of I.T & M.S

11

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.
Balochistan University of I.T & M.S 12

Clarity
A program should be concise and easy to read: Organize code into paragraphs Begin a paragraph with a topic-sentence comment

Balochistan University of I.T & M.S

13

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.
Balochistan University of I.T & M.S 14

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 %d\n" ):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 \ Balochistan University of I.T & 15 M.S i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}

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 dont! Most programs do not need to be optimized!
Balochistan University of I.T & M.S 16

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.
Balochistan University of I.T & M.S 17

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;
Balochistan University of I.T & M.S 18

The End

Balochistan University of I.T & M.S

19

You might also like