COMPUTER SCIENCE ARRAYS

DELHI 1999:

3.a) Suppose a 1D array AR containing integers is arranged in ascending order. Write a user defined function in C++ to search for one integer from AR with the help of binary search method, to show presence of the number in the array. The function should have three parameters: (1) an array AR (2) the
number to be searched and (3) the number of elements N in the array.

void BinSearch(int AR[ ], int Sno, int N)
{ int l=0,u=N-1,m,flag=0;
while(l<=u)
{ m=(l+u)/2;
if (Sno= = AR[m])
{ flag=1;
break;
}
else if(Sno<AR[m])
u=m-1;
else
l=m+1;
}
if( flag = = 0)
cout<<”\nThe Search
Element “<<Sno<<” is not available”;
else
cout<<”\nThe Search Element “<<Sno<<” is available”;
}

3.b) An array A[10][20] is stored in the memory with each element requiring 4 bytes of storage. If the base address of the array in the memory is 400, determine the location of A[8][13] when the array VAL is stored (i) Row major (ii) Column major.

Solution: Children, Try this answer.

3.c) Write a user-defined function in C++ to find and display the multiplication of row elements of two dimensional array A[4][6] containing integers.

void rowmul( )
{ int A[4][6],i,j,rowmul;
cout<<”\nEnter any 24 values…”;
for(i=0;i<4;i++)
for(j=0;j<6;j++)
cin>>A[i][j];for(i=0;i<4;i++)
{ rowmul=1;
for(j=0;j<6;j++)
rowmul=rowmul*A[i][j];
cout<<”\nThe multiplication of “<<i+1<<” row = “<<rowmul;
}
}

DELHI 1998:

3.a) Suppose an array P containing float is arranged in ascending order. Write a user defined function in C++ to search for one float from p with the help of binary search method. The function should return an integer 0 to show absence of the number in the array. The function should have the parameters as (1) an array P (2) the number DATA to be searched (3) number of elements N.

int BinSearch(float P[ ], float DATA, int N)
{ int l=0,u=N-1,m;
while(l<=u)
{ m=(l+u)/2;
if (DATA= = P[m])
return 1;
else if(DATA<P[m])
u=m-1;
else
l=m+1;
}
return 0;
}

 

CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )