0% found this document useful (0 votes)
2 views49 pages

3. Algorithm

Am important book for algorithm

Uploaded by

Mojtaba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views49 pages

3. Algorithm

Am important book for algorithm

Uploaded by

Mojtaba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Information Technology Department (CEI) KPU

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‬‬
‫‪ .1‬تعداد دستورالعمل ها مشخص باشد‬
‫‪ .2‬ابتدا وانتها داشته باشد ( از مرحله ای شروع و به مرحله ای ختم شود)‬
‫‪ .3‬هر دستورالعمل عاری از ابهام و پیچیدگی ها باشد‪.‬‬
‫‪ .4‬هر دستورالعمل قابل فهم و قابل اجرا باشد‪.‬‬
‫‪ .5‬از اجرای به هدف خاص و معینی برسیم‪.‬‬
‫‪Information Technology Department‬‬

‫تعریف الگوریتم از دیدگاه یک ماشین‬


‫‪IT‬‬
‫› الگوریتم یک ماشین ساده است که قابلیت‬
‫های زیر را دارد‪:‬‬
‫‪ .1‬دریافت یک یا چند دیتا به عنوان ورودی‬
‫‪ .2‬ارسال یک یا چند دیتا به عنوان خروجی‬
‫‪ .3‬انجام عملیات بین دو دیتا دلخواه‬
‫‪ .4‬ذخیره دیتا و اطالعات در قسمتی از‬
‫ماشین‬
‫‪ .5‬انجام اعمال ریاضی خاص مثل جمع‬
‫تفریق ‪...‬‬
‫‪Information Technology Department‬‬

‫مراحل نوشتن یک الگوریتم‬


‫‪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

‫شرطی نوع دوم‬


‫‪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‬قبل از نوشتن یک برنامه قدم اول پیدا کردن راه حل مسئله است‪.‬‬
‫در این جا باید رابطه اي بین درجه و رادیان پیدا کرد‪.‬‬ ‫–‬

‫‪ .2‬سپس آنچه در برنامه از ما خواسته شده را به صورت مجموعه اي از مراحل مي نویسیم‪.‬‬


‫گرفتن درجه‪d :‬‬ ‫–‬
‫تبدیل درجه به رادیان با استفاده از رابطه باال‪ :‬به دست آمدن ‪R‬‬ ‫–‬
‫چاپ ‪R‬‬ ‫–‬

‫به بیان باال اصطالحا یک الگوریتم گفته مي شود‪ .‬الگوریتم مجموعه اي از مراحل‬ ‫›‬
‫است که دنبال کردن آنها ما را به حل مسئله مي رساند‪.‬‬
‫نوشتن الگوریتم مستقل از یک زبان برنامه نویسي خاص است‪.‬‬ ‫–‬

‫‪ .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

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


29
SIXTH EDITION
Information Technology Department The Problem Analysis–Coding–Execution
Cycle (cont’d.)
IT
› 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,


30
SIXTH EDITION
Information Technology Department The Problem Analysis–Coding–Execution
Cycle (cont’d.)
IT
› 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,


31
SIXTH EDITION
Information Technology Department The Problem Analysis–Coding–Execution
Cycle (cont’d.)
IT
› 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,


32
SIXTH EDITION
Information Technology Department The Problem Analysis–Coding–Execution
Cycle (cont’d.)
IT
› 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,


33
SIXTH EDITION
Information Technology Department The Problem Analysis–Coding–Execution
Cycle (cont’d.)
IT
› 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,


34
SIXTH EDITION
Information Technology Department

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:

perimeter = 2 * (length + width)


area = length * width

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


35
SIXTH EDITION
Information Technology Department

Example 1-1 (cont’d.)


IT
› 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,


36
SIXTH EDITION
Information Technology Department

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

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


37
SIXTH EDITION
Information Technology Department

Example 1-5 (cont’d.)


IT
› 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,


38
SIXTH EDITION
Information Technology Department

Example 1-5 (cont’d.)


IT
› 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,


39
SIXTH EDITION
Information Technology Department

Example 1-5 (cont’d.)


IT
› 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,


40
SIXTH EDITION
Information Technology Department

Programming Methodologies
IT
› Two popular approaches to programming design
– Structured
– Object-oriented

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


41
SIXTH EDITION
Information Technology Department

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

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


42
SIXTH EDITION
Information Technology Department

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

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


43
SIXTH EDITION
Information Technology Department

Object-Oriented Programming (cont’d.)


IT
› 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
› Must learn how to represent data in computer memory,
how to manipulate data, and how to implement
operations

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


44
SIXTH EDITION
Information Technology Department

Object-Oriented Programming (cont’d.)


IT
› Write algorithms and implement them in a programming
language
› Use functions to implement algorithms
› Learn how to combine data and operations on the data
into a single unit called an object
› C++ was designed to implement OOD
› OOD is used with structured design

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


45
SIXTH EDITION
Information Technology Department

ANSI/ISO Standard C++


IT
› C++ evolved from C
› C++ designed by Bjarne Stroustrup at Bell Laboratories
in early 1980s
– Many different C++ compilers were available
› C++ programs were not always portable from one
compiler to another
› In mid-1998, ANSI/ISO C++ language standards were
approved

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


46
SIXTH EDITION
Information Technology Department

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

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


47
SIXTH EDITION
Information Technology Department

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

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


48
SIXTH EDITION
Information Technology Department

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

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


49
SIXTH EDITION

You might also like