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

Algorithm

Uploaded by

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

Algorithm

Uploaded by

Zabih keraam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Algorithm

Objective
– Learn what an algorithm is and explore problem-solving
techniques
– Become aware of structured design and object-oriented design
programming methodologies
3.1 Introduction
› Before writing a program:
– Have a thorough understanding of the problem
– Carefully plan an approach for solving it
› While writing a program:
– Know what “building blocks” are available
– Use good programming principles

3
‫الگوریتم ‪Algorithm‬‬
‫› به مجموعه ازیک یا چند دستورالعمل که اجرای آنها با ترتیب تعین شده منجر به‬
‫انجام یک کار به و‪-‬یژه حل یک مسله گردد الگوریتم گفته میشود‪.‬‬
‫› مجموعه از دستورالعمل های که با یک‪ -‬ترتیب معین و خاص و همچنین مرحله به‬
‫مرحله انجام شده و‪ -‬موجب حل مسله ای میگردد‪.‬‬
‫ویژگی های یک الگوریتم‬
‫‪ .1‬تعداد دستورالعمل ها مشخص باشد‬
‫‪ .2‬ابتدا وانتها داشته باشد ( از مرحله ای شروع و به مرحله ای ختم شود)‬
‫‪ .3‬هر دستورالعمل عاری از ابهام و پیچیدگی ها باشد‪.‬‬
‫‪ .4‬هر دستورالعمل قابل فهم و قابل اجرا باشد‪.‬‬
‫‪ .5‬از اجرای به هدف خاص و معینی برسیم‪.‬‬
‫تعریف الگوریتم از دیدگاه یک ماشین‬
‫› الگوریتم یک ماشین ساده است که قابلیت‬ ‫›‬
‫های زیر را دارد‪:‬‬
‫‪ .1‬دریافت یک یا چند دیتا به عنوان ورودی‬
‫‪ .2‬ارسال یک یا چند دیتا به عنوان خروجی‬
‫‪ .3‬انجام عملیات بین دو دیتا دلخواه‬
‫‪ .4‬ذخیره دیتا و اطالعات در قسمتی از‬
‫ماشین‬
‫‪ .5‬انجام اعمال ریاضی خاص مثل جمع‬
‫تفریق ‪...‬‬
‫مراحل نوشتن یک الگوریتم‬
‫› درابتدا هر الگوریتم کلمه شروع و در انتها کلمه پایان قرار دهید‪.‬‬
‫› برای هر دستورالعمل شماره ای در نظر بگیرید‪.‬‬
‫› برای محاسبه یک‪ -‬عبارت ریاضی یا انجام عملیات روی چند دیتا حتما مکانی‬
‫برای ذخیره دیتا و نتایج ان در نظر بگیرید‪.‬‬
‫› برای قرار دادن مقداری در یک خانه الگوریتم یا یک مکان از حافظه ماشین‬
‫الگوریتم از عالمت فرضی ‪ ‬استفاده نمایید‪.‬‬
‫مثال‬
‫› مفهوم هریک از عبارات زیر را برای ماشین الگو‪-‬ریتم بنوسید‪.‬‬
‫‪A 2 .1‬‬
‫‪B 2*3-5 .2‬‬
‫‪C B+1 .3‬‬
‫‪J J+1 .4‬‬
‫مثال‬
‫› الگوریتم بنوسید که سه عدد ‪ 2,3,5‬را در سه خانه ‪ A,B,C‬ذخیره کرده و سپس‬
‫اوسط آنرا محاسبه و چاپ نماید‪.‬‬
‫› حل مسله‪:‬‬
‫‪ .1‬شناخت مسله‪ :‬دیتا های مسله اعداد ‪ 2,3,5‬هستند و نتیجه حاصل از انها اوسط‬
‫سه عدد است‪.‬‬
‫‪ .2‬طرح نقشه مسله‪ :‬فرمول محاسبه اوسط سه عدد‪.‬‬
‫اوسط سه عدد= مجموع سه عدد تقسیم ‪3‬‬
‫› الگوریتم مسله ذکر شده به صورت ذیل است‬
‫‪ .1‬شروع‬
‫‪ .2‬عدد ‪ 2‬را در خانه ‪ A‬قرار بده‬
‫‪ .3‬عدد ‪ 3‬را در خانه ‪ B‬قرار بده‬
‫‪ .4‬عدد ‪ 5‬را در خانه ‪ C‬قرار بده‬
‫‪ .5‬خانه های ‪ A,B,C‬را جمع کن در خانه ‪ S‬قرار بده‬
‫‪ .6‬مقدار خانه ‪ S‬را بر ‪ 3‬تقسیم کن و در ‪ AVE‬قرار بده‬
‫‪ .7‬مقدار خانه ‪ AVE‬را چاپ کن‬
‫‪ .8‬پایان‬
‫› عموما برای ساده تر شدن الگوریتم و قابل فهم بودن ان از فرم نوشتاری در ریاضیات برای‬
‫دستورالعملها‪ ,‬بجای فرم دری استفاده میشود‪.‬‬
‫‪ .1‬شروع‬
‫‪A 2 .2‬‬
‫‪B 3 .3‬‬
‫‪C 5 .4‬‬
‫‪S A+B+C .5‬‬
‫‪AVE S/3 .6‬‬
‫‪ AVE .7‬را چ‪-‬اپک‪---‬ن‬
‫‪ .8‬پایان‬
‫› حقوق یک کارگر ‪ A‬افغانی است و هر‬
‫ماه ‪ %10‬از حقوق وی بخاطر بیمه و‬
‫‪ %5‬هم مالیه کسر میگردد‪ .‬الگوریتمی‬
‫بنوسید که حقوق یک کارگر را به‬
‫عنوان ورودی گرفته و حقوق خالص‬
‫آنرا حساب نموده و چاپ نماید‪.‬‬
‫› ‪ B‬مقدار بیمه‬
‫› ‪ M‬مقدار ما‪--‬لیه‪-‬‬
‫› ‪ H‬مقدار حقوقخ‪-‬ا‪--‬لص‬
‫جمالت شرطی‬
‫› این جمالت به دو نوع تقسیم میشوند‪:‬‬
‫‪ .1‬شرطی نو‪-‬ع ساده‬
‫در این گونه جمالت شرطی اگر شرط بعد از ‪ IF‬درست باشد دستورات مقابل‬
‫‪ THEN‬اجرا و به خط بعدی منتقل میشود اما اگر شرط نادرست باشد دستورات‬
‫مقابل ‪ THEN‬اجرا نگردیده و‪ -‬مستقیما به خط بعد میرود‪.‬‬
‫اگر شرط درست باشد میتو‪-‬ان اجرای برنامه را به چند خط باالتر ویا پایانتر انتقال‬
‫داد‪.‬‬
‫مثال‬
‫› الگوریتم بنوسید که اعداد جفت دو رقمی را یکی یکی محاسبه و‪ -‬چاپ نماید‪.‬‬
Programming with the Problem Analysis–
Coding–Execution Cycle
› Algorithm:
– Step-by-step problem-solving
process
– Solution achieved in finite
amount of time
› Programming is a process
of problem solving

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


15
SIXTH EDITION
The Problem Analysis–Coding–Execution Cycle
(cont’d.)
› Step 1: Analyze the problem
– Outline the problem and its requirements
– Design steps (algorithm) to solve the problem
› Step 2: Implement the algorithm
– Implement the algorithm in code
– Verify that the algorithm works
› Step 3: Maintenance
– Use and modify the program if the problem domain changes

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


16
SIXTH EDITION
The Problem Analysis–Coding–Execution Cycle
(cont’d.)
› Thoroughly understand the problem and all requirements
– Does program require user interaction?
– Does program manipulate data?
– What is the output?

› If the problem is complex, divide it into subproblems


– Analyze and design algorithms for each subproblem

› Check the correctness of algorithm


– Can test using sample data
– Some mathematical analysis might be required

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


17
SIXTH EDITION
The Problem Analysis–Coding–Execution Cycle
(cont’d.)
› Once the algorithm is designed and correctness
verified
– Write the equivalent code in high-level language
› Enter the program using text editor

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


18
SIXTH EDITION
The Problem Analysis–Coding–Execution Cycle
(cont’d.)
› Run code through compiler
› If compiler generates errors
– Look at code and remove errors
– Run code again through compiler
› If there are no syntax errors
– Compiler generates equivalent machine code
› Linker links machine code with system resources

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


19
SIXTH EDITION
The Problem Analysis–Coding–Execution Cycle
(cont’d.)
› Once compiled and linked, loader can place program
into main memory for execution
› The final step is to execute the program
› Compiler guarantees that the program follows the rules
of the language
– Does not guarantee that the program will run correctly

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


20
SIXTH EDITION
Example 1-1
› Design an algorithm to find the perimeter and area of a
rectangle
› The perimeter and area of the rectangle are given by
the following formulas:

perimeter = 2 * (length + width)


area = length * width

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


21
SIXTH EDITION
Example 1-1 (cont’d.)
› Algorithm:
– Get length of the rectangle
– Get width of the rectangle
– Find the perimeter using the following equation:
perimeter = 2 * (length + width)
– Find the area using the following equation:
area = length * width

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


22
SIXTH EDITION
Example 1-5
› Calculate each student’s grade
– 10 students in a class; each student has taken five tests; each
test is worth 100 points
› Design algorithms to:
– Calculate the grade for each student and class average
– Find the average test score
– Determine the grade
› Data: students’ names; test scores

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


23
SIXTH EDITION
Example 1-5 (cont’d.)
› Algorithm to determine the average test score:
– Get the five test scores
– Add the five test scores
› Suppose sum stands for the sum of the test scores
– Suppose average stands for the average test score:
› average = sum / 5;

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


24
SIXTH EDITION
Example 1-5 (cont’d.)
› Algorithm to determine the grade:
if average is greater than or equal to 90
grade = A
otherwise
if average is greater than or equal to 80 and less than 90
grade = B
otherwise
if average is greater than or equal to 70 and less than 80
grade = C
otherwise
if average is greater than or equal to 60 and less than 70
grade = D
otherwise
grade = F

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


25
SIXTH EDITION
Example 1-5 (cont’d.)
› Main algorithm is as follows:
– totalAverage = 0;
– Repeat the following for each student:
› Get student’s name
› Use the algorithm to find the average test score
› Use the algorithm to find the grade
› Update totalAverage by adding current student’s average test score
– Determine the class average as follows:
› classAverage = totalAverage / 10

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


26
SIXTH EDITION
Programming Methodologies
› Two popular approaches to programming design
– Structured
– Object-oriented

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


27
SIXTH EDITION
Structured Programming
› Structured design:
– Dividing a problem into smaller sub-problems
› Structured programming:
– Implementing a structured design
› The structured design approach is also called:
– Top-down (or bottom-up) design
– Stepwise refinement
– Modular programming

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


28
SIXTH EDITION
Object-Oriented Programming
› Object-oriented design (OOD)
– Identify components called objects
– Determine how objects interact with each other
› Specify relevant data and possible operations to be
performed on that data
› Each object consists of data and operations on that
data

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


29
SIXTH EDITION
Object-Oriented Programming (cont’d.)
› An object combines data and operations on the data
into a single unit
› A programming language that implements OOD is
called an object-oriented programming (OOP)
language

› C++ was designed to implement OOD

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


30
SIXTH EDITION
Summary
› Computer: electronic device that can perform
arithmetic and logical operations
› Computer system has hardware/software
– Central processing unit (CPU): brain
– Primary storage (MM) is volatile; secondary storage (e.g., disk) is
permanent
– Operating system monitors overall activity of the computer and
provides services
– Various kinds of languages

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


31
SIXTH EDITION
Summary (cont’d.)
› Compiler: translates high-level language into machine
code
› Algorithm: step-by-step problem-solving process;
solution in finite amount of time
› Problem-solving process has three steps:
– Analyze problem and design an algorithm
– Implement the algorithm in code
– Maintain the program

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


32
SIXTH EDITION
Summary (cont’d.)
› Structured design:
– Problem is divided into smaller subproblems
– Each subproblem is solved
– Combine solutions to all subproblems
› Object-oriented design (OOD): a program is a
collection of interacting objects
– Object: data and operations on those data

C++ PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,


33
SIXTH EDITION
‫کار خانه گی‬
‫› الگورتمی را بنوسید که اعداد دو رقمی که خود شان با معکوس شان برابر است‬
‫را روی صفحه چاپ نماید‪.‬‬
Questions?

Thank You

You might also like