CBSE Guess > Papers > Important Questions > Class XII > 2010 > Computer Science > Computer Science By Ravi Kiran
CBSE CLASS XII
Q.3. Write a function in C++ which accepts a 2D array of integers and its size as arguments and
displays the elements which lie on diagonals. [Assuming the 2D Array to be a square matrix
with odd dimension i.e., 3x3, 5x5 ,7x7 etc…]
Example : if the array content is
5 4 3
6 7 8
1 2 9
Out put through the function should be :
Diagonal One : 5 7 9
Diagonal Two : 3 7 1
Solution:
void accept(int a[ ][ ],int size)
{ cout<<"Diagonal One:";
for (int i=0;i<size;i++)
for(int j=0;j<size;j++)
if (i= = j)
cout<<a[i][j]<<’\t’;
cout<<"\n Diagonal Two:";
for (i=0;i<size;i++)
for(j=0;j<size;j++)
if((i+j)= =(size-1))
cout<<a[i][j]<<’\t’;
}
OUTSIDE DELHI 2007
Q.1. 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]<<’,’;
}
}
Q.2. 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.
Paper By Mr. Ravi Kiran
Email Id : [email protected] |