Download presentation
Presentation is loading. Please wait.
Published byAshley Hunt Modified over 8 years ago
2
Recursion Pointers and Arrays
3
#include void print3(int n){ if (n==0)return; printf ("%d\n",n); print3(n-1); printf ("%d\n",n); } void main(){ print3(5); }
4
#include void print4(int n){ if (n==0)return; print4(n-1); printf ("%d\n",n); print4(n-1); } void main(){ print4(4); }
5
int is_even (unsigned int n) { if (n==0) return 1; else return (is_odd(n-1)); } int is_odd (unsigned int n) { return (!is_even(n)); }
6
int recFib(int n){ if(n<=1) return n; return recFib(n-1)+ recFib(n-2); }
7
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); }
8
void main(){ int a[10]={1,3,6,7,8,12,15}; int index; index =binSearch(6,0,7,a); if (index=-1) printf (“Element not found%d\n",index); else printf (“Element found at: %d place\n",index); }
9
{1, 3, 6, 7, 8, 12,15} f=0, t=7, m=3 {1, 3, 6} f=0, t=2, m=1 f=0, t=0, m=0 f=2, t=2, m=2 f=6, t=7, m=6 f=4, t=4, m=4 {7, 8, 12, 15} f=4, t=7, m=5
10
{1, 3, 6, 7, 8, 12,15} f=0, t=7, m=3 {1, 3, 6} f=0, t=2, m=1 f=0, t=0, m=0 f=2, t=2, m=2 f=6, t=7, m=6 f=4, t=4, m=4 f=4, t=7, m=5 f=0, t=2, m=1 {1, 3, 6, 7, 8, 12,15} f=0, t=7, m=3 f=2, t=2, m=2
11
בתחילת המשחק מונחות הדיסקיות על עמוד אחד מסודרות לפי גודל המטרה היא להעביר את כל הדיסקיות לעמוד האחרון בכל צעד מותר להעביר דיסקית אחת, ואסור לשים דיסקית גדולה על קטנה
12
#include 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); moveDisk(numDisks, first, last); hanoi(numDisks - 1, help, last, first); } void main(){ hanoi(3,"A","C","B"); }
13
Number of DisksNumber of Disks’ Moves 11 24 416 8256 1665,536 324,294,967,296 6418,446,744,073,709,600,000
14
Pointers and Arrays
15
void main() { int a,b,*pa,*pb; a = 15; b = 20; pa = &a; b = *pa; *pa = 0; pb = &b; pa=pb; *pa=5; } MemoryAddresses 0 7663 5556 a: 5556 359 524 b: 524 2 8797 2015pa: 3028 3028 6080 15 5556 0 5 524 pb: 6080
16
#include void swap(int x, int y){ int temp=x; x=y; y=temp; } void main(){ int a=5,b=7; printf("a: %d, b: %d\n",a,b); swap(a,b); printf("a: %d, b: %d\n",a,b); }
17
#include void swap(int *x, int *y){ int temp=*x; *x=*y; *y=temp; } void main(){ int a=5,b=7; printf("a: %d, b: %d\n",a,b); swap(&a,&b); printf("a: %d, b: %d\n",a,b); }
18
#include void minmax(int x, int y, int *min, int *max){ if (x<y){ *min=x; *max=y; } else{ *min=y; *max=x; } void main(){ int a,b,min,max; printf("Enter a and b\n"); scanf("%d%d",&a,&b); minmax(a,b,&min,&max); printf("min: %d, max: %d\n",min,max); }
19
01234567 A int A[8]; i-th array element - A[i -1] Zero based A[3]
20
Zeroing of the whole array: ◦ for(i=0; i<10; i++) A[i]=0; Copying of array to another array ◦ for(i=0; i<10; i++) A[i]=B[i]; Loop required for similar array operations – comparison, print, etc.
21
1000 2200 3330 int A[3][4] = {{1},{2,2},{3,3,3}};
22
1000 22 -2-2 0 3330 A[1][2] = -2;
23
100022-203330 A[0][1]A[1][2]A[2][0]
24
Functions can accept arrays as arguments Usually the array’s size also needs to be passed (why?) Changes in the function affect the original array!! ◦ int main(int argc, char** argv);
25
int CalcSum(int arr[], int size) { int i; int sum = 0; for(i=0; i<size; i++) sum += arr[i]; return sum; } #include #define SIZE 10 int main() { int input[SIZE], i; printf("Please enter a list of %d integers:\n“, SIZE); for(i=0; i<SIZE; i++) scanf("%d", &input[i]); printf("The sum is %d\n", CalcSum(input, SIZE)); return 0; }
26
S is actually a pointer to S[0] int S[10]; int *P; P=S; /* From now P is equivalent to S */ Both P and S are now pointing to S[0]
27
When an array is defined, a fixed amount of memory (the size of the array) is allocated. ◦ The array variable is set to point to the beginning of that memory segment When a pointer is declared, it is uninitialized (like a regular variable) Unlike pointers, the value of an array variable cannot be changed Pointers are not necessarily arrays
28
Pointers can be incremented and decremented If p is a pointer to a particular type, p+1 yields the correct address of the next variable of the same type p++, p+i, and p += i also make sense
29
If p and q point to elements in an array, q-p yields the number of elements between p and q.
30
int nums[] = {1, 2, 3}; char str[] = “moshe”; int *q = nums; char *p = str; 123 qp moshe\0
31
int nums[] = {1, 2, 3}; char str[] = “moshe”; int *q = nums; char *p = str; 123 (q+1)(p+3) moshe\0
32
void main() { int a[6],*p,*q,i; p = &a[0]; *p = 50; q = &a[1]; *(p+2) = 7; *q=9; *(q+3)=*(p+1); i=q-p; } MemoryAddresses 0 35 5572 a: 5572 17 528 p: 528 2 10 5572 7 q: 3028 3028 6080 50 5576 9 9 i: 6080 59 7663 3 13 1
33
void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; }
34
int i,array[10]; int *ptr; ………………….. for (i=0; i<10; i++) printf(“%d”, array[i]); for (i=0; i<10; i++) printf(“%d”, *(array+i)); for (ptr=array; ptr <= &array[9]; ptr++) printf(“%d”, *ptr); /* Using array and index */ /* Using a pointer */ /* Using array as pointer and index */
35
Normally a function accepting an array will be declared, for example – int func(int arr[]); We might want to declare a function accepting a 2D array with – int func2(int arr2D[][]); This is a mistake!
36
int func2(int arr[][4]); A 2D array is actually an array of arrays ◦ int arr[3][4] – is an array of 3 integer arrays, each of size 4
37
When passing a 2D array to a function, we are actually passing a usual array, where each element is an array ◦ We must pass the size of each element Note – it’s also legal to declare int func2(int arr[3][4]); ◦ The ‘3’ is meaningless but harmless
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.