SlideShare a Scribd company logo
1 of 30
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
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.
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
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.
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)
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.
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”.
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.
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
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.
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
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.
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();
}
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
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.
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);
}
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.
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.
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
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.
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
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!
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.
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.
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;
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.
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;
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.
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.
Balochistan University of I.T &
M.S
30
The End

More Related Content

Similar to Algorithm

CS8451 DAA Unit-I.pptx
CS8451 DAA Unit-I.pptxCS8451 DAA Unit-I.pptx
CS8451 DAA Unit-I.pptxBolliniNivas
 
1.01.Introduction.pptx
1.01.Introduction.pptx1.01.Introduction.pptx
1.01.Introduction.pptxBinish Raza
 
Numerical inflation: simulation of observational parameters
Numerical inflation: simulation of observational parametersNumerical inflation: simulation of observational parameters
Numerical inflation: simulation of observational parametersMilan Milošević
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.pptArumugam90
 
Control systems R18 course
Control systems R18 course Control systems R18 course
Control systems R18 course satyaSatyant234
 
Project management
Project managementProject management
Project managementsmumbahelp
 
Software testing - EXAMPLE
Software testing  - EXAMPLESoftware testing  - EXAMPLE
Software testing - EXAMPLEpriyasoundar
 
Design and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfDesign and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfHarshNagda5
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLABRavikiran A
 
K-12 Computer Science Framework GaDOE Update
K-12 Computer Science Framework GaDOE UpdateK-12 Computer Science Framework GaDOE Update
K-12 Computer Science Framework GaDOE UpdateTony Vlachakis
 
vtu data structures lab manual bcs304 pdf
vtu data structures lab manual bcs304 pdfvtu data structures lab manual bcs304 pdf
vtu data structures lab manual bcs304 pdfLPSChandana
 
Wireless Communications courses and research at COMNET
Wireless Communications courses and research at COMNET Wireless Communications courses and research at COMNET
Wireless Communications courses and research at COMNET ProjectENhANCE
 
(Final) Feedback and Control System.docx
(Final) Feedback and Control System.docx(Final) Feedback and Control System.docx
(Final) Feedback and Control System.docxRechieJohnRelator
 

Similar to Algorithm (20)

plc learn
plc learnplc learn
plc learn
 
CS8451 DAA Unit-I.pptx
CS8451 DAA Unit-I.pptxCS8451 DAA Unit-I.pptx
CS8451 DAA Unit-I.pptx
 
1.01.Introduction.pptx
1.01.Introduction.pptx1.01.Introduction.pptx
1.01.Introduction.pptx
 
01_intro-cpp.ppt
01_intro-cpp.ppt01_intro-cpp.ppt
01_intro-cpp.ppt
 
01_intro-cpp.ppt
01_intro-cpp.ppt01_intro-cpp.ppt
01_intro-cpp.ppt
 
Numerical inflation: simulation of observational parameters
Numerical inflation: simulation of observational parametersNumerical inflation: simulation of observational parameters
Numerical inflation: simulation of observational parameters
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
 
Control systems R18 course
Control systems R18 course Control systems R18 course
Control systems R18 course
 
Project management
Project managementProject management
Project management
 
CSE0105 Class 0.pdf
CSE0105 Class 0.pdfCSE0105 Class 0.pdf
CSE0105 Class 0.pdf
 
Software testing - EXAMPLE
Software testing  - EXAMPLESoftware testing  - EXAMPLE
Software testing - EXAMPLE
 
Design and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfDesign and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdf
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Problem solving methodology
Problem solving methodologyProblem solving methodology
Problem solving methodology
 
Cyclomatic complexity
Cyclomatic complexityCyclomatic complexity
Cyclomatic complexity
 
K-12 Computer Science Framework GaDOE Update
K-12 Computer Science Framework GaDOE UpdateK-12 Computer Science Framework GaDOE Update
K-12 Computer Science Framework GaDOE Update
 
Cs 331 Data Structures
Cs 331 Data StructuresCs 331 Data Structures
Cs 331 Data Structures
 
vtu data structures lab manual bcs304 pdf
vtu data structures lab manual bcs304 pdfvtu data structures lab manual bcs304 pdf
vtu data structures lab manual bcs304 pdf
 
Wireless Communications courses and research at COMNET
Wireless Communications courses and research at COMNET Wireless Communications courses and research at COMNET
Wireless Communications courses and research at COMNET
 
(Final) Feedback and Control System.docx
(Final) Feedback and Control System.docx(Final) Feedback and Control System.docx
(Final) Feedback and Control System.docx
 

More from yarkhosh

Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If conditionyarkhosh
 
Arithmetic Operator in C
Arithmetic Operator in CArithmetic Operator in C
Arithmetic Operator in Cyarkhosh
 
Math Functions in C Scanf Printf
Math Functions in C Scanf PrintfMath Functions in C Scanf Printf
Math Functions in C Scanf Printfyarkhosh
 
Operators in C
Operators in COperators in C
Operators in Cyarkhosh
 
Data Types in C
Data Types in CData Types in C
Data Types in Cyarkhosh
 
Escape Sequences and Variables
Escape Sequences and VariablesEscape Sequences and Variables
Escape Sequences and Variablesyarkhosh
 
Introduct To C Language Programming
Introduct To C Language ProgrammingIntroduct To C Language Programming
Introduct To C Language Programmingyarkhosh
 

More from yarkhosh (7)

Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If condition
 
Arithmetic Operator in C
Arithmetic Operator in CArithmetic Operator in C
Arithmetic Operator in C
 
Math Functions in C Scanf Printf
Math Functions in C Scanf PrintfMath Functions in C Scanf Printf
Math Functions in C Scanf Printf
 
Operators in C
Operators in COperators in C
Operators in C
 
Data Types in C
Data Types in CData Types in C
Data Types in C
 
Escape Sequences and Variables
Escape Sequences and VariablesEscape Sequences and Variables
Escape Sequences and Variables
 
Introduct To C Language Programming
Introduct To C Language ProgrammingIntroduct To C Language Programming
Introduct To C Language Programming
 

Recently uploaded

LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456KiaraTiradoMicha
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyAnusha Are
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedDelhi Call girls
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxalwaysnagaraju26
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 

Recently uploaded (20)

LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 

Algorithm

  • 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.
  • 30. Balochistan University of I.T & M.S 30 The End