Important Questions

CBSE Guess > Papers > Important Questions > Class XII > 2010 > Computer Science > Computer Science By Ravi Kiran

CBSE CLASS XII

3.c) Write a user-defined function in C++ to find and display the sum of diagonal elements from a 2D array MATRIX[6][6] containing integers.
void displaysum( )
{ int i,j,D1=0,D2=0,MATRIX[6][6];
cout<<”\nEnter any 36 values….”;
for(i=0;i<6;i++)
for(j=0;j<6;j++)
{ cin>>MATRIX[i][j];
if(i= = j)
D1=D1+MATRIX[i][j];
else if ((i+j)= =(size-1))
D2=D2+MATRIX[i][j];
}
cout<<”\nThe sum of the elements of the Main Diagonal = “<<D1;
cout<<”\nThe sum of the elements of the Other Diagonal = “<<D2;
}


DELHI 1999

Q.1. 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”;
}

Q.2. 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.

Q.3. 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

Q.1. 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;
}

Q.2. An array T[15][10] is stored in the memory with each element requiring 2 bytes of storage. If the base address of T is 2000, determine the location of T[7][8] when the array VAL is stored (i) Row major (ii) Column major.

Solution: Children, Try this as an assignment.

Paper By Mr. Ravi Kiran
Email Id : [email protected]