Presentation is loading. Please wait.

Presentation is loading. Please wait.

Presented By: Mahmoud Rafeek Alfarra

Similar presentations


Presentation on theme: "Presented By: Mahmoud Rafeek Alfarra"— Presentation transcript:

1 Presented By: Mahmoud Rafeek Alfarra
MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE DATA STRUCTURE Using C# Information Technology , 3’rd Semester Lecture 5: Recursion & Pointers Presented By: Mahmoud Rafeek Alfarra

2 Outline Recursion The factorial Recursion vs. Iteration
Emank X Mezank !!

3 Recursion A recursive method is a method that calls itself either directly or indirectly through another method. Recursion is an important topic discussed at length in upper-level computer science courses. Call Presented & Prepared by: Mahmoud R. Alfarra

4 The factorial The factorial of an integer number greater than or equal to 0 can be calculated iteratively (nonrecursively) using for as follows: HW 5.1 Write a method that performs the factorial using recursion Presented & Prepared by: Mahmoud R. Alfarra

5 The factorial Presented & Prepared by: Mahmoud R. Alfarra

6 The factorial If (number is less than or equal 1) Return 1 else
6 If (number is less than or equal 1) Return 1 else Return number * method(number-1) HW 5.2 Write a method that performs the factorial using recursion technique HW 5.3 Case study : The Fibonacci Series Presented & Prepared by: Mahmoud R. Alfarra

7 Recursion vs. Iteration
Both iteration and recursion are based on a control structure. Iteration uses a repetition structure (such as for, while or do/while) Recursion uses a selection structure (such as if, if/else or switch). Presented & Prepared by: Mahmoud R. Alfarra

8 Recursion vs. Iteration
Both iteration and recursion involve repetition. Iteration explicitly uses a repetition structure. Recursion achieves repetition through repeated method calls. Iteration and recursion each involve a termination test. HW 5.4 Case study : notes more differences between them Presented & Prepared by: Mahmoud R. Alfarra

9 What is Pointers ? 9 A pointer is nothing but a variable that holds the memory address of another type. In C#, pointer can only be declared to hold the memory address of value types and arrays. Presented & Prepared by: Mahmoud R. Alfarra

10 Declaring a Pointer type
10 The general form of declaring a pointer type is as shown below type * variable_name;  Where * is known as the de-reference operator int *x ; This Declares a pointer variable x, which can hold the address of an int type. The reference operator (&) can be used to get the memory address of a variable. int x = 100; The &x gives the memory address of the variable x, which we can assign to a pointer variable int *ptr = & x ; Presented & Prepared by: Mahmoud R. Alfarra

11 Pointers Console.WriteLine((int)ptr) // Displays the memory address
11 Console.WriteLine((int)ptr) // Displays the memory address Console.WriteLine(*ptr) // Displays the value at the memory address. 01011AB10 ptr 78 The value of (*ptr) is 78 But the value of (ptr) is 01011AB10 Presented & Prepared by: Mahmoud R. Alfarra

12 Pointers 12 using System; class MyClass { public void Method() { // code { int x = 10; int y = 20; int *ptr1 = &x; int *ptr2 = &y; Console.WriteLine((int)ptr1); Console.WriteLine((int)ptr2); Console.WriteLine(*ptr1); Console.WriteLine(*ptr2); } } } Presented & Prepared by: Mahmoud R. Alfarra

13 Emank X Mezank !! قال رسول الله صلى الله عليه وسلم : ( كلكم يدخل الجنـة إلا من أبى, قالوا و من يأبى يا رسول الله ؟ قال : من أطاعني دخل الجنة و من عصاني فقد أبى) رواه البخاري

14 Next Lecture Linked List


Download ppt "Presented By: Mahmoud Rafeek Alfarra"

Similar presentations


Ads by Google