```BITS PILANI, DUBAI CAMPUS
SECOND SEMESTER 2015 - 2016
COURSE
: COMPUTER PROGRAMMING (CS F111)
COMPONENT
: Tutorial#9 (SOLUTION)
DATE
: 14-MAR-2016
#include&lt;stdio.h&gt;
main()
{
int a[15], posn[5], key, size, k;
scanf(“%d”,&amp;size);
scanf(“%d”,&amp;key);
for(i=0;i&lt;=n-1;i++)
scanf(“%d”,&amp;a[i]);
j=0;
for(i=0;i&lt;=n-1;i++)
{
if(key==a[i])
{
posn[j]=i+1;
j++;
}
}
if(j==0)
else
{
printf(“Occurences of key are as follows:\n”);
for(k=0;k&lt;j;k++)
printf(“%d\n”,posn[k]);
}
}
In the previous program posn[--j] will be the last occurrence of the key.
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”, &amp;nrows, ncols);
for(i=0; i&lt;nrows;i++)
CS F111
COMPUTER PROGRAMMING
Page 1 of 3
for(j=0;j&lt;ncols;j++)
scanf(“%d”,&amp;a[i][j]);
scanf(“%d”,&amp;key);
for(i=0;i&lt;nrows;i++)
for(j=0;j&lt;ncols;j++)
if(key==a[i][j])
printf(“row no: %d\tcol no.:%d\n”,i+1,j+1);
}
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”, &amp;nrows, ncols);
for(i=0; i&lt;nrows;i++)
for(j=0;j&lt;ncols;j++)
scanf(“%d”,&amp;a[i][j]);
scanf(“%d”,&amp;key);
for(i=0;i&lt;nrows;i++)
for(j=0;j&lt;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);
}
main()
{
int a[10][10], b[10];
printf(“Enter the rows and columns: “);
scanf(“%d %d”, &amp;nrows, ncols);
for(i=0; i&lt;nrows;i++)
for(j=0;j&lt;ncols;j++)
scanf(“%d”,&amp;a[i][j]);
for(j=0; j&lt;=ncols-1; j++)
{
k=0;
for(i=0; i&lt;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&lt;=nrows-1; i++)
a[i][j]=b[k++];
}
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 &lt;stdio.h&gt;
int main()
{
int c, first, last, middle, n, search,
array[100];
#include&lt;stdio.h&gt;
int main(){
int a[10],i,n,m,c=0;
printf(&quot;Enter number of elements\n&quot;);
scanf(&quot;%d&quot;,&amp;n);
printf(&quot;Enter the size of an array: &quot;);
scanf(&quot;%d&quot;,&amp;n);
printf(&quot;Enter the elements of the
array: &quot;);
for(i=0;i&lt;=n-1;i++){
scanf(&quot;%d&quot;,&amp;a[i]);
}
printf(&quot;Enter %d integers\n&quot;, n);
for (c = 0; c &lt; n; c++)
scanf(&quot;%d&quot;,&amp;array[c]);
printf(&quot;Enter value to find\n&quot;);
scanf(&quot;%d&quot;, &amp;search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first &lt;= last) {
if (array[middle] &lt; search)
first = middle + 1;
else if (array[middle] == search) {
printf(&quot;%d found at location
%d.\n&quot;, search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first &gt; last)
in the list.\n&quot;, search)
return 0;
}
printf(&quot;Enter the number to be
search: &quot;);
scanf(&quot;%d&quot;,&amp;m);
for(i=0;i&lt;=n-1;i++){
if(a[i]==m){
c=1;
break;
}
}
if(c==0)
printf(&quot;The number is not in the
list&quot;);
else
printf(&quot;The number is found&quot;);
return 0;
***END***
CS F111
COMPUTER PROGRAMMING
Page 3 of 3
```