Presentation is loading. Please wait.

Presentation is loading. Please wait.

Complementary tutorial - arrays, loops, riddles Prepared by: Valentin Kravtsov.

Similar presentations


Presentation on theme: "Complementary tutorial - arrays, loops, riddles Prepared by: Valentin Kravtsov."— Presentation transcript:

1 Complementary tutorial - arrays, loops, riddles Prepared by: Valentin Kravtsov

2 Arrays – warm up questions Given array “arr”, print its content #define N 7 int i; int arr[N] = {1,2,3,4,5,6,7}; for( i=0 ; i<N ; i++ ){ printf("%d ",arr[i]); } Output: 1 2 3 4 5 6 7

3 Given array “arr”, print its content backwards #define N 7 int i; int arr[N] = {1,2,3,4,5,6,7}; for( i=N-1 ; i>=0 ; i-- ){ printf("%d ",arr[i]); } Output: 7 6 5 4 3 2 1

4 Given array “arr”, print its content backwards discarding the leading zeroes int i, leading_zeros = 1; //true int arr[N] = {1,2,3,4,5,0,0}; for( i=N-1; i>=0 ; i-- ){ if(leading_zeros && arr[i]==0) continue; printf("%d ",arr[i]); leading_zeros = 0; //false } Output: 5 4 3 2 1 Solution #1 – straightforward

5 Given array “arr”, print its content backwards discarding the leading zeroes int i; int arr[N] = {1,2,3,4,5,0,0}; for( i=N-1 ; i>=0 && arr[i]==0 ; i-- ); for( ; i>=0 ; i--){ printf("%d ",arr[i]); } Output: 5 4 3 2 1 Solution #2 – a bit more efficient

6 2D Arrays To print a multiplication table:

7 2D Arrays To print a multiplication table: int i, j; for( i=1 ; i<=N ; i++){ for( j=1 ; j<=N ; j++){ printf("%2d ",i*j); } printf("\n"); }

8 2D Arrays To print the “flag of Britain”: o o o ooooooooo ooo

9 2D Arrays To print the “flag of Britain”: int i,j; char c; for(i=0 ; i<N; i++){ for(j=0 ; j<N ; j++){ c = (i==j || i+j==N-1 || i==N/2 || j==N/2)?'o':' '; printf("%c",c); } printf("\n"); }

10 Given a 2D array arr[N][N], we have to print TRUE if there is at least one sorted column, FALSE otherwise. int i,j; for( j=0 ;j<N ; ++j ) { for( i=1; i<N ;++i){ if( arr[i][j] < arr [i-1][j] ) break; } if( i==N ) {printf(“TRUE”); return 1; } } printf(“FALSE”);

11 Your optional challenging home- assignment To print a “number spiral” 99 98 97 96 95 94 93 92 91 90 64 63 62 61 60 59 58 57 56 89 65 36 35 34 33 32 31 30 55 88 66 37 16 15 14 13 12 29 54 87 67 38 17 4 3 2 11 28 53 86 68 39 18 5 0 1 10 27 52 85 69 40 19 6 7 8 9 26 51 84 70 41 20 21 22 23 24 25 50 83 71 42 43 44 45 46 47 48 49 82 72 73 74 75 76 77 78 79 80 81

12 Given two arrays, arr1 & arr2, with sorted non- repeating numbers, print the intersection of these arrays. int arr1[N] = {1,5,7,8,15,20,22,23,40,43}; int arr2[N] = {2,3,4,5, 6,11,15,19,20,43}; int i,j; for( i=j=0 ; i<N && j<N ; ){ if(arr1[i] == arr2[j]){ printf("%d ", arr1[i]); i++; j++; } else if(arr1[i] > arr2[j]) { j++; } else { i++; } } Output: 5 15 20 43

13 Given a sorted array of size N, efficiently find if there are 2 numbers summing up to NUM. int i,j,arr[N] = {1,4,7,8,12,19,22}; //N=7, NUM = 16 for(i=0,j=N-1; i<j; ){ if(arr[i] + arr[j] == NUM){ printf("%d + %d = %d\n",arr[i],arr[j], NUM); return 0; }else if(arr[i] + arr[j] > NUM){ j--; }else{ i++; } } printf("There are no such numbers..."); Output: 4 + 12 = 16

14 Medium to Hard questions Given an array of size N (0..N-1), filled with values between 0..N, we have one number that is missing in the array. Find the missing number. int i, arr[N] = {1,6,2,5,0,3,7}; //N=7, values=0..7, missing "4" int count[N+1] = {0}; for(i=0;i<N;i++){ count[ arr[i] ] = 1; } for( i=0 ; i<N+1 ; i++){ if(count[i]==0) printf("The missing number is %d\n",i); } Output: The missing number is 4 Version 1 Complicated…

15 int i,arr[N-1] = {1,6,2,5,0,3}; //N=7, missing "4" int count = 0; for( i=0 ; i<N-1 ; i++ ){ count += arr[i]; } printf("The missing number is: %d\n",(N-1)*N/2-count); Output: The missing number is 4 Version 2 The tricky one Given an array of size N (0..N-1), filled with values between 0..N, we have one number that is missing in the array. Find the missing number.

16 Given an array of size N with random values which might be negative, find the sum of a maximal sequence. int i,arr[N] = {7, -3, -8, 5, -1, 10, -19, 6, 6, -4}; //N=10 int currSum=0, maxSum = arr[0]; for(i=0; i<N; i++ ){ currSum +=arr[i]; if(currSum > maxSum){ maxSum = currSum; } if(currSum < 0){ currSum = 0; } } printf(“Max sum = %d\n",maxSum); Output: Max sum = 14

17 Given an array of size N with values between 0..99, find the number that appears most frequently. int arr[N] = {0, 6, 81, 6, 92, 1, 34, 0, 37, 6}; //N=10 int count[100] = {0}; int i, maxIndex, maxCount=0; for (i=0; i<N; ++i) { count[ arr[i] ]++; } for (i=0; i<100; ++i) { if (count[i] > maxCount) { maxCount = count[i]; maxIndex = i; } printf("Max=%d appeared %d times\n", maxIndex, maxCount); Output: Max=6 appeared 3 times

18 Given 2 arrays of small letters of size N, we must decide if one is a permutation of another. char arr1[N] = {'a','b','c','d'}, arr2[N] = {'b','d','c','a'}; int count[26] = {0}, i; for(i=0 ; i < N ; i++) { count[ arr1[i] - 'a']++; count[ arr2[i] - 'a']--; } for (i=0 ; i<26 ; ++i){ if (count[i] != 0){ printf("Illegal permutation"); return 0; } printf("Legal permutation"); return 1; Output: Legal permutation

19 Given an array of size N (N is even), we need to find the minimum and the maximum values by using at most 3N/2 cells comparisons. int i,min,max,tmpMin,tmpMax, arr[N] = {7,3,2,5,8,3,4,9}; //N=8 min=max=arr[0]; for(i = 0; i < N-1 ; i+=2){ if(arr[i] > arr[i+1]){ tmpMin = arr[i+1]; tmpMax = arr[i]; }else{ tmpMin = arr[i]; tmpMax = arr[i+1]; } if(tmpMin < min) min = tmpMin; if(tmpMax > max) max = tmpMax; } printf("min = %d, max = %d",min,max); Output: min = 2, max = 9


Download ppt "Complementary tutorial - arrays, loops, riddles Prepared by: Valentin Kravtsov."

Similar presentations


Ads by Google