3. Algorithm
3. Algorithm
C++
Algorithm
Information Technology Department
Objective
IT
– Learn what an algorithm is and explore problem-solving
techniques
– Become aware of structured design and object-oriented design
programming methodologies
Information Technology Department
3.1 Introduction
IT
› 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
Information Technology Department
الگوریتم Algorithm
IT
› به مجموعه ازیک یا چند دستورالعمل که اجرای آنها با ترتیب تعین شده منجر
به انجام یک کار به ویژه حل یک مسله گردد الگوریتم گفته میشود.
› مجموعه از دستورالعمل های که با یک ترتیب معین و خاص و همچنین مرحله
به مرحله انجام شده و موجب حل مسله ای میگردد.
Information Technology Department
مثال
IT
› مفهوم هریک از عبارات زیر را برای ماشین الگوریتم بنوسید.
A 2 .1
B 2*3-5 .2
C B+1 .3
J J+1 .4
Information Technology Department
مثال
IT
› الگوریتم بنوسید که سه عدد 2,3,5را در سه خانه A,B,Cذخیره کرده و
سپس میانگین آنرا محاسبه و چاپ نماید.
› حل مسله:
.1شناخت مسله :دیتا های مسله اعداد 2,3,5هستند و نتیجه حاصل از انها
میانگین سه عدد است.
.2طرح نشقه مسله :فرمول محاسبه میانگین سه عدد.
میانگین سه عدد= مجموع سه عدد تقسیم 3
Information Technology Department
IT
› الگوریتم مسله ذکر شده به صورت ذیل است
.1شروع
.2عدد 2را در خانه Aقرار بده
.3عدد 3را در خانه Bقرار بده
.4عدد 5را در خانه Cقرار بده
.5خانه های A,B,Cرا جمع کن در خانه Sقرار بده
.6مقدار خانه Sرا بر 3تقسیم کن و در AVEقرار بده
.7مقدار خانه AVEرا چاپ کن
.8پایان
Information Technology Department
IT
› عموما برای ساده تر شدن الگوریتم و قابل فهم بودن ان از فرم نوشتاری در
ریاضیات برای دستورالعملها ,بجای فرم دری استفاده میشود.
.1شروع
A 2 .2
B 3 .3
C 5 .4
S A+B+C .5
AVE S/3 .6
AVE .7را چاپ کن
.8پایان
Information Technology Department
IT
› حقوق یک کارگر Aافغانی است و
هر ماه %10از حقوق وی بخاطر
بیمه و %5هم مالیه کسر میگردد.
الگوریتمی بنوسید که حقوق یک
کارگر را به عنوان ورودی گرفته و
حقوق خالص آنرا حساب نموده و چاپ
نماید.
› Bمقدار بیمه
› Mمقدار مالیه
› Hمقدار حقوق خالص
Information Technology Department
IT
› الگوریتم بنوسید که زمان Tرا برحسب ثانیه به عنوان ورودی دریافت نموده و
معین کند که چند ساعت چند دقیقه و چند ثانیه میشود .
Information Technology Department
جمالت شرطی
IT
› این جمالت به دو نوع تقسیم میشوند:
.1شرطی نوع ساده
در این گونه جمالت شرطی اگر شرط بعد از IFدرست باشد دستورات مقابل
THENاجرا و به حط بعدی منتقل میشود اما اگر شرط نادرست باشد دستورات
مقابل THENاجرا نگردیده و مستقیما به خط بعد میرود.
اگر شرط درست باشد میتوان اجرای برنامه را به چند خط باالتر ویا پایانتر انتقال
داد.
Information Technology Department
مثال
IT
› الگوریتم بنوسید که اعداد جفت دو رقمی را یکی یکی محاسبه و چاپ نماید.
Information Technology Department
مثال
IT
› الگوریتم بنوسید که عدد nرا به عنوان ورودی دریافت و اعداد طاق کوچکتر
یا مساوی به عدد nرا یکی یکی محاسبه و چاپ نماید.
IT
Information Technology Department
IT
› الگوریتم بنوسید که اعداد جفت بین 1000و 2000را یکی یکی تولید و چاپ
کند در ضمن مجموع آنها را حساب کند.
Information Technology Department
IT
› الگوریتم بنوسید که عدد nرا دریافت و مجموع زیر را محاسبه و چاپ نماید
Information Technology Department
IT
› الگوریتمی بنوسید که سه عدد a,b,cکه طول اظالع مثلث هستند را دریافت و
معین کند که مثلث قایم الزاویه است یا خیر؟
Information Technology Department
IT
› الگوریتمی بنوسید که دو عدد A,Bرا به عنوان ورودی دریافت و بزرگترین و
کوچکترین عدد را محاسبه و چاپ نماید.
.1شروع
A .2و Bرا بگیر
.3اگر A>Bباشد Aرا به عنوان بزرگترین و Bرا به عنوان کوچکترین
عدد بنویس و بروبه 6
B .4را به عنوان بزرگترین عدد بنویس
A .5را به عنوان کوچکترین عدد بنویس
.6پایان
Information Technology Department
IT
› الگوریتمی بنوسید که سه عدد A,B,Cرا به عنوان ورودی دریافت و
بزرگترین عدد را چاپ نماید.
Information Technology Department
کار خانگی
IT
› الگوریتمی بنوسید که یک عدد مثبت را به عنوان ورودی دریافت و معین کند
که عدد جفت است یا طاق.
Information Technology Department
IT
› یک شرکت میخواهد که حقوق کارمندان خویش را به صورت زیر افزایش بدهد
› کارمندان که معاش آن کمتر یا مساوی به 25000باشد , %5اگر بین
25000و 35000باشد %10از معاش شان به معاش شان اضافه گردد.
الگوریتم بنوسید که معاش اولیه ,مقدار اضافی و معاش جدید شانرا حساب
نماید.
IT
Information Technology Department
Information Technology Department d R
3.2 Algorithms R d
180 180
IT
مثال :1برنامه اي بنویسید که زاویه بر حسب درجه را دریافت کرده و معادل رادیان آن را روي مانیتور
نشان دهد.
.1قبل از نوشتن یک برنامه قدم اول پیدا کردن راه حل مسئله است.
در این جا باید رابطه اي بین درجه و رادیان پیدا کرد. –
به بیان باال اصطالحا یک الگوریتم گفته مي شود .الگوریتم مجموعه اي از مراحل ›
است که دنبال کردن آنها ما را به حل مسئله مي رساند.
نوشتن الگوریتم مستقل از یک زبان برنامه نویسي خاص است. –
.3در مرحله آخر با نوشتن دستورات مناسب الگوریتم را به برنامه واقعي تبدیل مي کنیم.
Information Technology Department
3.2 Algorithms
IT
› Computing problems
– All can be solved by executing a series of actions in a specific
order
› Algorithm: procedure in terms of
– Actions to be executed
– The order in which these actions are to be executed
› Program control
– Specify order in which statements are to be executed
27
Information Technology Department
3.3 Pseudocode
IT
› Pseudocode
– Artificial, informal language that helps us develop algorithms
– Similar to everyday English
– Not actually executed on computers
– Helps us “think out” a program before writing it
› Easy to convert into a corresponding C++ program
› Consists only of executable statements
28
Information Technology Department Programming with the Problem Analysis–
Coding–Execution Cycle
IT
› Algorithm:
– Step-by-step problem-solving
process
– Solution achieved in finite
amount of time
› Programming is a process
of problem solving
Example 1-1
IT
› 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:
Example 1-5
IT
› 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
Programming Methodologies
IT
› Two popular approaches to programming design
– Structured
– Object-oriented
Structured Programming
IT
› Structured design:
– Dividing a problem into smaller subproblems
› Structured programming:
– Implementing a structured design
› The structured design approach is also called:
– Top-down (or bottom-up) design
– Stepwise refinement
– Modular programming
Object-Oriented Programming
IT
› 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
Summary
IT
› 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
Summary (cont’d.)
IT
› 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
Summary (cont’d.)
IT
› 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