Introduction to Programming Lecture 2: Algorithm Design.

Slides:



Advertisements
Similar presentations
Introduction to Computer Science Theory
Advertisements

Exercise (1).
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Appendix B Solving Recurrence Equations : With Applications to Analysis of Recursive Algorithms.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Parallel Sorting – Odd-Even Sort David Monismith CS 599 Notes based upon multiple sources provided in the footers of each slide.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf 10.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
Lecture 12 Oct 13, 2008 Some simple recursive programs recursive problem solving, connection to induction Some examples involving recursion Announcements.
CS150 Introduction to Computer Science 1
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
Lecture 8 Analysis of Recursive Algorithms King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
شنت گذاري  .
نمايش اعداد.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Analysis of Recursive Algorithms What is a recurrence relation? Forming Recurrence Relations Solving Recurrence Relations Analysis Of Recursive Factorial.
نمايش اعداد.
Telephone English Series 2 Tip 2 Material Providers: Pooneh Tajali, Mehdi Khodaparast Text & PowerPoint Specialist: Zhaleh Kazemi Project Supervisor:
 Experiment 07 Function Dr. rer.nat. Jing LU
Algorithmics - Lecture 21 LECTURE 2: Algorithms description - examples -
PYTHON PROGRAMMING Week 10 – Wednesday. TERMS – CHAPTER 1 Write down definitions for these terms:  Computation  Computability  Computing  Artificial.
1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.
Lesson four Grade three
نمايش اعداد علی عادلی.  مبنا ( base ): –مبناي r: ارقام محدود به [0, r-1]  دسيمال:(379) 10  باينري:( ) 2  اکتال:(372) 8  هگزادسيمال:(23D9F)
طراحی مدارهای منطقی نیمسال دوم دانشگاه آزاد اسلامی واحد پرند.
به نام خدا برنامه سازی سمت سرور (php)
1. Example 1 – Averages 2. Example 2 – Rolling Dice 3. Example 3 – Number Analysis 4. Example 4 - Divisibility ( while ) Additional Examples on for Loops.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Count and add list of numbers From user input and from file.
Introduction to Programming Lecture 7: Repeating Statements.
Introduction to Programming Lecture 6: Making Decisions.
به نام خدا دانشگاه علمي كاربردي واحد 11 تهران محيط‌هاي چند رسانه‌اي ) اسلايد سوم ) E.Javanmard Website:
اسامي شناسه ها (Identifier names) اسامي متغيرها ، توابع ، برچسب ها (labels) وبقيه اشياء تعريف شده توسط كاربر در C ، شناسه ( identifier ) ناميده مي شود.
شرط و تصميم اصول كامپيوتر 1. 2 الگوريتم اقليدس E1: [find remainder] Divide m by n and let r be the remainder. Clearly, 0
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
1/46 Logical Agents عاملهاي منطقي Chapter 7 (part 2) Modified by Vali Derhami.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Order.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Introduction to Programming Lecture 5: Interaction.
Introduction to Programming Lecture 8: Functions.
الف) تابع y = f(x) = X >= 0x -2 < x < 0 x3x3 X
آشنايي با سيستم اعداد.
Unit 6 Analysis of Recursive Algorithms
for Repetition Structures
تهیه و تنظیم: فاطمه قاسمی دانشگاه صنعتی شریف – پاییز 86
مرتب سازي مقايسه اي مرتب سازي خطي
آرايه ها.
ANOVA: Analysis Of Variance
ANOVA: Analysis Of Variance
Odd-even merge sort مرتب سازی.
Quick Sort مرتب سازي سريع.
لغت نامه و جدول درهم سازي Dictionaries and Hash Tables
آشنايي با برنامه نويسي به زبان C++
Introduction to Programming
تکنیک دیماتل DEMATEL: decision making trial and evaluation laboratory.
The for-loop and Nested loops
مباني كامپيوتر و برنامه سازي Basics of Computer and Programming
نمايش اعداد در کامپيوتر چهار عمل اصلي
(Structured Query Language)
فيلتر كالمن معرفي : فيلتر كالمن تخمين بهينه حالت‌ها است كه براي سيستم‌هاي ديناميكي با اختلال تصادفي در سال 1960 بزاي سيستم‌هاي گسسته و در سال 1961 براي.
MPC Review کنترل پيش بين-دکتر توحيدخواه.
Pass by Reference.
مثال : فلوچارتي رسم كنيد كه دو عدد از ورودي دريافت كرده بزرگترين عدد
عملیات با رشته‌ها موسوی ندوشنی ویراست 1389 دانشگاه صنعت آب و برق.
ساختمان داده ها مرتب سازی درجی
مباني كامپيوتر و برنامه سازي Basics of Computer and Programming
مباني كامپيوتر و برنامه سازي Basics of Computer and Programming
Suppose I want to add all the even integers from 1 to 100 (inclusive)
Parallel sorting.
Presentation transcript:

Introduction to Programming Lecture 2: Algorithm Design

2 What We Will Learn  Sample algorithms to practice problem solving steps  Input & Output analysis  Algorithm design  Pseudo-code

الگوريتم تشخيص زوج يا فرد بودن عدد Algorithm: Odd-Even (1) 1- print “Please enter an integer” 2- read n 3- y  n mod 2 4- if (y == 0) print “Number is even” else print “Number is odd” 3

الگوريتم تشخيص زوج يا فرد بودن عدد Algorithm: Odd-Even (2) 1- print “Please enter an integer” 2- read n 3- if(n < 0) n  -1 * n 4- while(n >= 2) n  n – 2 5- if(n = 0) print “even” else print “odd” 4

الگوريتم تشخيص زوج يا فرد بودن عدد Algorithm: Odd-Even (3) 1- print “Please enter an integer” 2- read n 3- while (n >= 2) or (n < 0) n  n - sign(n) * 2 4- if (n = 1) print “odd” else print “even” 5

الگوريتمي كه يك رشته عدد را كه با 0 تمام مي‌شود را مي‌گيرد و تعداد اعداد زوج و فرد را چاپ مي‌كند Algorithm: Count Odd-Even odd_cnt  0 even_cnt  0 print “Please enter an integer” read n while (n != 0) y  n mod 2 if (y == 0) even_cnt  even_cnt + 1 else odd_cnt  odd_cnt + 1 print “Please enter an integer” read n print “Odd = “ odd_cnt “Even = “ even_cnt 6

الگوريتمي كه يك عدد صحيح مثبت را بگيرد و مجموع ارقام آن را چاپ كند Algorithm: Digit-Sum print “Please enter a positive integer” read n sum  0 m  n while (n != 0) y  n mod 10 sum  sum + y n  n - y n  n / 10 print “sum of digits of” m “ = “ sum 7

الگوريتمي كه يك عدد صحيح مثبت را بگيرد و آنرا در مبناي 8 چاپ كند Algorithm: Base-8 print “Please enter a positive integer” read n i  0 while (n != 0) x[i]  n mod 8 n  floor (n / 8) i  i + 1 i  i - 1 while (i >= 0) print x[i] i  i - 1 8

الگوريتمي كه يك عدد صحيح مثبت را بگيرد و فاكتوريل آنرا چاپ كند Algorithm: Factorial-1 print “Please enter a positive integer” read n i  1 result  1 while (i <= n) result  i * result i  i + 1 return result 9

الگوريتمي كه يك عدد صحيح مثبت را بگيرد و فاكتوريل آنرا توليدكند Algorithm: Factorial-2 print “Please enter a positive integer” read n result  1 while (n > 0) result  result * n n  n - 1 return result 10

الگوريتمي كه يك عدد صحيح مثبت را بگيرد و فاكتوريل آنرا توليد كند Algorithm: Factorial-Recursive (n) if (n == 1) return 1 else return n * Factorial-Recursive (n - 1) 11

الگوريتمي كه يك رشته عدد را كه محل عضو اول آن با start و محل عضو آخر آن با end مشخص شده است را به صورت صعودي مرتب كند. Algorithm: sort (x, start, end) while (start != end) j  find index of minim element from start to end swap x[j] and x[start] start  start + 1 ================== Algorithm find_min(x, start, end) i  start y  i while (i <= end) if(x[i] < x[y]) y  I i  i + 1 return y 12

الگوريتمي كه يك رشته عدد را كه محل عضو اول آن با start و محل عضو آخر آن با end مشخص شده است را به صورت صعودي مرتب كند. algorithm swap(x, j, i) temp  x[j] x[j]  x[i] x[i]  temp 13