COMPUTER SCIENCE ARRAYS

3.b) An array X[10][20] is stored in the memory with each element requiring 4 bytes of storage. If the Base address of the array is 1000, calculate location of X[5][15] when the array X is stored using column major order.

NOTE: X[10][20] means valid row indices are 0 and 9 and valid column indices are 0 and 19

Solution: Children, Try this answer as an assignment.

3.c) Write a user-defined function named Lower_half() which takes 2D array A, with size N rows and N columns as argument and prints the lower half of the array.

Eg:

Input:

2    3    1    5    0
7    1    5    3    1
2    5    7    8    1
0    1    5    0    1
3    4    9    1    5

Output:

2
7    1
2    5    7
0    1    5    0
3    4    9    1    5

Solution:

void Lower_half( int A[ ][ ],int N)
{ int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{ if(i<j)
cout<<A[i][j]<<’\t’;
cout<<endl;
}
}

DELHI 2000:

3.a) Suppose A, B, C are arrays of integers of size M, N and M+N respectively. The numbers in array A appear in ascending order while numbers in array in descending order. Write user defined function in C++ to produce third array C by merging array A by B in ascending order. Use A, B and C as arguments in the function.

void Merge(int A[ ],int M,int B[ ],int
N,int C[ ])
{ int a,b,c;
for(a=0,b=N-1,c=0;a<M&&b>=0;)
{ if(A[a]<=B[b])
C[c++]=A[a++];
else
C[c++]=B[b--];
}
if(a<M)
{ while(a<M)
C[c++]=A[a++];
}
else
{ while(b>=0)
C[c++]=B[b--];
}
}

 

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