BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI SECOND SEMESTER 2015 - 2016 COURSE : COMPUTER PROGRAMMING (CS F111) COMPONENT : Tutorial#9 (SOLUTION) DATE : 14-MAR-2016 Answer 1(a). #include<stdio.h> main() { int a[15], posn[5], key, size, k; scanf(“%d”,&size); scanf(“%d”,&key); for(i=0;i<=n-1;i++) scanf(“%d”,&a[i]); j=0; for(i=0;i<=n-1;i++) { if(key==a[i]) { posn[j]=i+1; j++; } } if(j==0) printf(“\nKEY NOT FOUND!\n\n”); else { printf(“Occurences of key are as follows:\n”); for(k=0;k<j;k++) printf(“%d\n”,posn[k]); } } Answer 1(b). In the previous program posn[--j] will be the last occurrence of the key. Answer 1(c). Index of all occurrences of key in a 2-D array main() { int a[10][10], key, nrows, ncols, i, j; printf(“Enter the rows and columns: “); scanf(“%d %d”, &nrows, ncols); for(i=0; i<nrows;i++) CS F111 COMPUTER PROGRAMMING Page 1 of 3 for(j=0;j<ncols;j++) scanf(“%d”,&a[i][j]); scanf(“%d”,&key); for(i=0;i<nrows;i++) for(j=0;j<ncols;j++) if(key==a[i][j]) printf(“row no: %d\tcol no.:%d\n”,i+1,j+1); } Answer 1(d). The above program can be modified to get the last occurrence of the key in the 2-D array as follows main() { int a[10][10], key, nrows, ncols, i, j,k,l; printf(“Enter the rows and columns: “); scanf(“%d %d”, &nrows, ncols); for(i=0; i<nrows;i++) for(j=0;j<ncols;j++) scanf(“%d”,&a[i][j]); scanf(“%d”,&key); for(i=0;i<nrows;i++) for(j=0;j<ncols;j++) if(key==a[i][j]) { k=i+1; l=j+1; } printf(“The last occurrence is at row no: %d\tcol no.:%d\n”,k,l); } Answer 2. main() { int a[10][10], b[10]; printf(“Enter the rows and columns: “); scanf(“%d %d”, &nrows, ncols); for(i=0; i<nrows;i++) for(j=0;j<ncols;j++) scanf(“%d”,&a[i][j]); for(j=0; j<=ncols-1; j++) { k=0; for(i=0; i<nrows-1; i++) b[k++]=a[i][j]; /*Perform insertion sort on b array*/ k=0; CS F111 COMPUTER PROGRAMMING Page 2 of 3 for(i=0; i<=nrows-1; i++) a[i][j]=b[k++]; } Answer 3. Hint: a. Implement Linear Search for each row, b. with maximum number of elements in each row as the limit, Once the Row is finalized, the c. Use Binary Search for that row and d. Use Reverse string program a. Program for Binary Search b. Program for Linear Search #include <stdio.h> int main() { int c, first, last, middle, n, search, array[100]; #include<stdio.h> int main(){ int a[10],i,n,m,c=0; printf("Enter number of elements\n"); scanf("%d",&n); printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements of the array: "); for(i=0;i<=n-1;i++){ scanf("%d",&a[i]); } printf("Enter %d integers\n", n); for (c = 0; c < n; c++) scanf("%d",&array[c]); printf("Enter value to find\n"); scanf("%d", &search); first = 0; last = n - 1; middle = (first+last)/2; while (first <= last) { if (array[middle] < search) first = middle + 1; else if (array[middle] == search) { printf("%d found at location %d.\n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if (first > last) printf("Not found! %d is not present in the list.\n", search) return 0; } printf("Enter the number to be search: "); scanf("%d",&m); for(i=0;i<=n-1;i++){ if(a[i]==m){ c=1; break; } } if(c==0) printf("The number is not in the list"); else printf("The number is found"); return 0; ***END*** CS F111 COMPUTER PROGRAMMING Page 3 of 3