OUTSIDE DELHI 2007:
3.a) Write a function in C++ which accepts an integer array and its size as arguments and replaces elements having even values with its half and elements having odd values with twice its value .
Example : If an array of five elements initially contains the elements as 3, 4, 5, 16, 9.
then the function should rearrange content of
the array as 6, 2, 10, 8, 18
Solution:
void accept(int a[ ],int size)
{ for (int i=0;i<size;i++)
{ if (a[i]%2= =0)
a[i]=a[i]/2;else
a[i]=a[i]*2;
cout<<a[i]<<’,’;
}
}
3.b) An array Arr[15][20] is stored in the memory along the row with each element
occupying 4 bytes. Find out the Base address of the location Arr[3][2], if the location Arr[5][2] is stored at the address 1500.
Solution: Given Data: Arr[15][20] W=4 B=? R=15 C=20 Lr = 0 Lc = 0 Address of Arr[3][2] = ?
Address of Arr[5][2] = 1500.
Address of an element (I,J) in row major = B+W(C(I-Lr)+(J-Lc ))
Therefore, 1500 = B+4(20(5-0)+(2- 0))
1500 = B+4(20*5+2)
1500 = B+4*102
1500 = B+408
B =1500-408
B=1092
Address of Arr[3][2] =1092+4(20*3+2)
=1092+4(62)
=1092+248
=1340.
3.d) Write a function in C++ which accepts a 2D array of integers and its size as arguments and displays the elements of middle row and the elements of middle column. [Assuming the 2D Array to be a square matrix with odd dimension i.e., 3x3, 5x5, 7x7 etc…]
Example : If the array content is
3 5 4
7 6 9
2 1 8
Output through the function should be :
Middle Row : 7 6 9
Middle Column : 5 6 1
Solution:
void accept(int a[ ][ ],int size)
{ cout<<"Middle Row:";
for (int i=0;i<size;i++)
for(int j=0;j<size;j++)
if (i= = size/2)
cout<<a[i][j]<<’\t’;
cout<<"\n Middle Column:";
for (i=0;i<size;i++)
for(j=0;j<size;j++)
if(j= =size/2)
cout<<a[i][j]<<’\t’;
CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )