An online library for CBSE Computer Science and Informatics Practices students

CLASS XII COMPUTER SCIENCE ARRAY BASED IMPORTANT QUESTIONS


Page No. > 1  2  3  4 
16 
Write a function in C++ to print sum of all values which either are divisible by 2 or divisible by 3 present in a 2D array passed as the argument of the function. 
Ans. 
#include<conio.h>
#include<iostream.h>
void Sum(int A[3][3],int R,int C)
{
int i,j,S=0;
for(i=0;i<R;i++)
for(j=0;j<C;j++)
if(A[i][j]%2==0A[i][j]%3==0)
S=S+A[i][j];
cout<<"\nThe Sum of all the values which are divisible by 2 or 3 in the array = "<<S;
}
void main()
{
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
clrscr();
Sum(a,3,3);
getch();
} 
17 
Write a function in C++ to find the sum of diagonal elements from a 2D array of type float. Use the array and its size as parameters with float as its return type. 
Ans. 
#include<conio.h>
#include<iostream.h>
float diasum(float arr[3][3],int r,int c)
{
int i,j;
float Dsum=0.0;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
if((i==j))
{
Dsum=Dsum+arr[i][j];
cout<<arr[i][j]<<endl;
}
cout<<Dsum;
return Dsum;
}
void main()
{
float arr[3][3]={{1,2,3},{4,5,6},{7,8,9}};
clrscr();
diasum(arr,3,3);
getch();
}

18 
Assume a array E containing elements of structure Employee is required to be arranged in descending order of Salary. Write a C++ function to arrange same with the help of bubble sort, the array and its size is required to be passed as parameters to the function. Definition of structure Employee is as follows: 
Ans. 
void sortEmp(Employee E[ ],int n)
{
int i,j;
Employee Etemp;
for(i=0;i<n;++i)
for(j=0;j<(n1)i ;j++)
if(E[j].salary<E[j+1].salary)
{
Etemp=E[j];
E[j]=E[j+1];
E[j+1]=temp;
}
cout<<"The details of the employee in ascending order of salary ";
for(i=0;i<n;i++)
cout<<E[i].Eno<<'\t'<<E[i].name<<’\t<<E[i].Salary<<endl;
}

19 
Given two arrays of integers X and Y of sizes m and n respectively. Write a function named MERGE() which will third array named Z, such that the following sequence is followed.
All odd numbers of X from left to right are copied into Z from left to right.
All even numbers of X from left to right are copied into Z from right to left.
All odd numbers of Y from left to right are copied into Z from left to right.
All even numbers of Y from left to right are copied into Z from right to left.
X, Y and Z are passed as arguments to MERGE().
Eg. X is {3, 2, 1, 7, 6, 3} and {9, 3, 5, 6, 2, 8, 10}
the resultant array Z is {3, 1, 7, 3, 9, 3, 5, 10, 8, 2, 6, 6, 2} 
Ans. 
void MERGEARRAY(int X[ ], int m,int Y[ ],int n,int Z[ ])
{
int mn,i,,left=0,right=mn1; mn=m+n;
for(i=0;i<m;i++)
if(X[i]%2= = 1)
Z[left++]=X[i]; //For copying odd numbers of X into Z from left to right
else
Z[right ]=X[i]; //For copying even number of X into Z from right to left
for(i=0;i<n;i++)
if (X[i]%2= = 1)
Z[left++]=Y[i]; //For copying odd numbers of Y into Z from left to right
else
Z[right ]=Y[i]; //For copying even number of X into Z from right to left
} 
20 
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. 
Ans. 
#include<conio.h>
#include<iostream.h>
void Search(int AR[], int Sno, int EN)
{
int l=0,u=EN1,m,flag=0;
while(l<=u)
{
m=(l+u)/2;
if(Sno==AR[m])
{
flag=1;
break;
}
else if(Sno<AR[m])
u=m1;
else
l=m+1;
}
if( flag==0)
cout<<"\nThe Search Element "<<Sno<<"is not available";
else
cout<<"\nThe Search Element "<<Sno<<" is available";
}
void main() { int AR[5]={1,2,3,4,5}; clrscr(); Search(AR,4,5); getch();
} 
21 
An array VAL[1…15][1…10] is stored in the memory with each element requiring 4 bytes of storage. If the base address of the array VAL is 1500, determine the location of VAL[12][9] when the array VAL is stored (i) Row wise (ii) Column wise. 
Ans. 
Given Data:
VAL[1…15][1…10]
Word Length (W) = 4 Bytes
Base Address of VAL(B) = 1500
VAL[12][9] = ?
C = Total No of Columns R = Total No of Rows
Lr = Least Row=1 Lc = Least Column=1
( i ) Row Major:
Address of an element (I,J) in row major = B + W ( C (ILr) + (J – Lc))
VAL [12][9] = 1500 + 4 (10 * (121) + (91))
= 1500 + 4 (10 * 11+8)
= 1500 + 4 (118)
= 1500 + 472
= 1972.
( i ) Column Major:
Address of an element (I,J) in column major = B + W ( (ILr) + R(J – Lc))
VAL [12][9] = 1500 + 4 ((121) +15 * (91))
= 1500 + 4 (11 + 15 * 8)
= 1500 + 4 ( 11+ 120)
= 1500 + 4 * 131
= 1500 + 524
= 2024. 


