Download presentation
Presentation is loading. Please wait.
1
C Programming
2
Last Lecture Reminder Recursion
3
Recurrent Problem Definition - Factorial
n! = 1*2*3*… *(n-1)*n – General algorithm 0! = 1 – Basic case n! = n*(n-1)! – Recurrent step – solving complex case for n>0 – stop condition
4
Factorial int factorial(int n) { int fact = 1; while (n>1)
fact *= n; n--; } return fact; #include <stdio.h> int main() { int n, fact = 0; printf("Enter a number\n"); scanf("%d", &n); fact = factorial(n); printf("the factorial is %d\n", fact); printf(“n = %d\n", n); return 0; }
5
Recurrent Factorial int recFactorial(int n){ if(n<=1) return 1;
return n*recFactorial(n-1); } #include <stdio.h> int main() { int n, fact = 0; printf("Enter a number\n"); scanf("%d", &n); fact = recFactorial(n); printf("the factorial is %d\n", fact); printf(“n = %d\n", n); return 0; }
6
Linear Recursion Call step(n-1) Solve step n Return Result
7
Solve step (n-1) Call step(n-2)
Tail Recursion Solve step n Call step(n-1) Solve step (n-1) Call step(n-2) Solve step 1 Return Result
8
Binary Recursion Solve step n Call step(n-1)
return Solve step 1 return
9
Mutual Recursion A: Solve step n Call B(n-1)
B: Solve step (n-1) Call B(n-2) A: Solve step n-2 Call B(n-3) B: Solve step (n-3) Call B(n-4)
10
print1 #include <stdio.h> void print1(int n){ if (n==0)return;
printf ("%d\n",n); print1(n-1); } void main(){ print1(5);
11
print2 #include <stdio.h> void print2(int n){ if (n==0)return;
print2(n-1); printf ("%d\n",n); } void main(){ print2(5);
12
print3 #include <stdio.h> void print3(int n){ if (n==0)return;
printf ("%d\n",n); print3(n-1); } void main(){ print3(5);
13
print4 #include <stdio.h> void print4(int n){ if (n==0)return;
print4(n-1); printf ("%d\n",n); } void main(){ print4(4);
14
Fibonacci Numbers fib1 = 0; fib2 = 1; printf("%d ", fib1);
while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; }
15
Fibonacci Numbers int recFib(int n){ if(n<=1) return n;
return recFib(n-1)+ recFib(n-2); }
16
Binary Search Algorithm
int array[SIZE] = {1, 3, 6, 7, 8, 12,15}; Find a place of element 6 Find a place of element 9
17
Binary Search Algorithm
{1, 3, 6, 7, 8, 12,15} {1, 3, 6} {7, 8, 12,15} {1} {3,6} {7,8} {12,15} {3} {6} {7} {8} {12} {15}
18
Binary Search int binSearch (int value, int from, int to, int Arr[]){
int middle = from+(to-from)/2; if (Arr[middle]==value) return middle; else if (from == to) return -1; else if (Arr[middle] < value) return binSearch (value, middle+1, to, Arr); else return binSearch (value, from, middle-1, Arr); }
19
Binary Search void main(){ int a[10]={5,7,15,19,28,35,56,88,155,156};
int index; index =binSearch(19,0,9,a); if (index=-1) printf (“Element not found%d\n",index); else printf (“Element found at: %d place\n",index); }
20
Binary Search 6 {1, 3, 6, 7, 8, 12,15} {1, 3, 6} {7, 8, 12,15} {1}
{3,6} {7,8} {12,15} {3} {6} {7} {8} {12} {15}
21
Binary Search 9 {1, 3, 6, 7, 8, 12,15} Mid = 3 {1, 3, 6} Mid = 1
{7, 8, 12,15} {1} {3,6} {7,8} {12,15} {3} {6} {7} {8} {12} {15}
22
מגדלי הנוי בתחילת המשחק מונחות הדיסקיות על עמוד אחד מסודרות לפי גודל
בתחילת המשחק מונחות הדיסקיות על עמוד אחד מסודרות לפי גודל המטרה היא להעביר את כל הדיסקיות לעמוד האחרון בכל צעד מותר להעביר דיסקית אחת, ואסור לשים דיסקית גדולה על קטנה
23
פתרון עבור 4 דיסקיות
24
#include <stdio.h>
void moveDisk(int num, char from[], char to[]){ printf ("move disk %d from %s to %s\n", num,from,to); } void hanoi (int numDisks, char first[], char last[], char help[]){ if (numDisks == 1){ moveDisk(numDisks, first, last); else{ hanoi(numDisks - 1, first, help, last); hanoi(numDisks - 1, help, last, first); void main(){ hanoi(3,"A","C","B");
25
output move disk 1 from A to C move disk 2 from A to B
move disk 1 from C to B move disk 3 from A to C move disk 1 from B to A move disk 2 from B to C
26
POINTERS AND ARRAYS
27
Pointer Variables int k = 5; int *pk = NULL; pk = &k; *pk = 4;
scanf(“%d”, &k); printf(“%d”, *pk);
28
Declaring a pointer variable
type *variable_name; A pointer is declared by adding a * before the variable name.
29
Referencing The unary operator & gives the address of a variable
The statement: P = &C; assigns the address of C to the pointer variable P, and now P points to C To print a pointer, use %p format.
30
Dereferencing The unary operator * is the dereferencing operator
Applied on pointers Access the object the pointer points to The statement: *P = 5; puts in C (the variable pointed to by P) the value 5
31
void main() { int a,b,*pa,*pb; a = 15; b = 20; pa = &a; b = *pa;
pb = &b; pa=pb; *pa=5; } Addresses Memory a: 5556 b: 524 pa: 3028 524 15 20 359 5 pb: 6080 3028 524 5556 2 5556 7663 15 6080 8797 524
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.