Contents
Chapterwise Question Bank CBSE Class 12 Computer Science (C++) – Array
Topic – 1
Data Structure and One Dimensional Array
Exam Practice
Short Answer Type Questions [2 Mark]
Question 1:
Write code for a function oddEvenfint s[], int N) in C++, to add 5 in all the odd values and 7 0 in
all the even values of the array s. Delhi 2014
e.g. if the original content of the array s is:
Аnswer:
void oddEven(int s[], int N) { for(int i=0;i<N;i++) { if(s[i]%2 == 0) s[i] += 10; else s[i] += 5; } }
Question 2:
Write code for a function void Convertint T[], int Num) in C++, which repositions all the elements of the array by shifting each of them one to one position before and by shifting the first element to the last position. Delhi 2013
Аnswer:
#include<iostream.h> void Convert(int T[], int Num) { int temp = T[0]; for(int i=0;i<(Num-1);i++) { T[i]=T[i+1]; } T[i] = temp; }
Question 3:
Write a function SWAP2CHANGE(int p[ ], int N) in C++ to modify the content of the array in such a way that the elements, which are multiples of 10 swap with the value present in the very next position in the array. Delhi 2012
e.g. if the content of array p is:
91, 50, 54, 22, 30, 54
The content of array p should become:
91, 54, 50, 22, 54, 30
Аnswer:
void SWAP2CHANGE(int p[],int N) { int C; for(int i=0;i<=N-1;i++) { if(p[i]%10 = 0) { C = p[i]; p[i] = p[i+1]; p[i+1] = C; i++; } } cout<<"Array after change becomes"; for(i=0;i<—N-1;i++) { cout<<p[i]<<"\t"; } }
Question 4:
Write a Get1From2( ) function in C++ to transfer the content from two arrays FIRST[ ] and SECOND[ ]
to array ALL[ ]. The even places (0,2,4,…) of array ALL[ ] should get the content from the array Firstf[ ] and odd places (1,3,5,…) of the array ALL[ ] should get the content from the array SECONDf ]. Delhi 2011
e.g. if the FIRST[ ]array contains
30, 60, 90
and the SECOND[ ] array contains
10, 50, 80
The ALL [ ] array should contain
30, 10, 60, 50, 90, 80
Аnswer:
void GetlFrom2(int FIRST[],int SECONDE[],int N) { int ALL[6]; for(int i=0,j=0;i<N;i=i+2,j++) { ALL[i] = FIRST[j]; } for(j=1,i=0;j<N;j=j+2,i++) { ALL[j] = SECONDE[i]; } }
Question 5:
Write a Get2Froml( ) function in C++ to transfer the content from one array ALL[ ] to two different arrays Odd[ ]
and Even[ ].
The Odd[ ] array should contain the values from odd position (1,3,5,…) of ALL[ ] and Even[ ] array
should contain the values from places (0,2,4,…) of ALL[ ]. All India 2011
e.g. The ALL[ ] array should contain
12,34, 56, 67, 89, 90
if the Odd[ ] array contains
34, 67, 90
and the Even[ ] array contains
12, 56, 89
Аnswer:
void Get2Froml(int ALL[],int N) { int j=0,k=0; int Odd[10],Even[10]; for(int i=1;i<=N;i++) { if((i% 2) == 0) { Even[j++] = ALL[i]; } else { Odd[k++] = ALL[i]; } } }
Question 6:
Write a function CHANGE( ) in C++, which accepts an array of integer and its size as parameters and divide all those array elements by 7, which are divisible by 7 and multiply other array elements by 3. Delhi 2010
Аnswer:
void CHANGE(int A[],int N) { for(int i=0;i<N;i++) { 1f(A[i]%7 == 0) A[i] = A[i]/7; else A[i] = A[i]*3; } }
Question 7:
Write a function SORTPOINTSf( ) in C++ to sort an array of structure Game in descending order of points using bubble sort.
Assume the following definition of structure Game. Delhi 2009
struct Game { long PNo; //Player Number char PName[20]; long Points; };
Аnswer:
void SORTPOINTS(struct Game G[],int n) { int i,j; struct Game t; for(i=1;i=n;i++) { for(j=0;j<=n-1;j++) { if(G[j+1].Points>G[j].Points) { t = G[j]; G[j] = G[j+1]; G[j+1] = t; } } } }
Question 8:
Write a function in C++, with accepts an integer array and its size as parameters and swap the elements of every even location with its following odd location. All India 2008
e.g. if an array contains the elements as:
2, 4, 1, 6, 5, 7, 9, 23, 10
Then, the function should rearrange the array as:
4, 2, 6, 1, 7, 5, 23, 9, 10
Аnswer:
void ElementSwap(int A[],int size) { int limt, temp; if(size%2!=0) limt = size-1; else limt=size; for(int i=0;i<limt—1;i+=2) { temp = A[i]; A[i] = A[1+1]; A[i+1] = temp; } }
Question 9:
Write a function in C++, which accepts an integer array and its size as parameters and rearrange the array in reverse order. Delhi 2008
e.g. if an array contains the elements as:
4, 2, 5, 1, 6, 7, 8, 12, 10
Then, the function should rearrange the array as:
10, 12, 8, 7, 6, 1, 5, 2, 4
Аnswer:
void RevArray(int A[],int size) { int i=0,j=size-1,temp; for(;i<=j;i++,j--) { temp = A[i]; A[i] = A[j]; A[j] = temp; } }
Question 10:
Write a function in C++, which accepts an integer array and its size as parameters and replaces
the elements having odd values with the thrice its value and the elements having even values
with twice its values.
e.g. If an array contains the elements as 3, 4, 5, 16, 9. Then, the function should rearrange
the array as 9, 8, 15, 32, 27
Аnswer:
void Rearrange(int A[],int size) { for(int i=0;i<size;i++) { if(A[i]%2 == 0) A[i] *= 2; else A[i] *= 3; } }
Question 11:
Write a function in C++, which accepts an integer array and its size as arguments and exchanges
the values of first half side elements with the second half side elements of the array.
e.g. if an array of eight elements has initial content as:
2, 4, 1, 6, 7, 9, 23, 10
The function should rearrange the array:
7, 9, 23, 10, 2, 4, 1, 6
Аnswer:
void Swap(int A[],int size) { int i,j,temp,mid = size/2; if(size%2 == 0) j=mid; else j=mid+1; for(i =0;i<mid;i++,j++) { temp = A[i]; A[i] = A[j]; A[j] = temp; } }
Question 12:
Given two arrays of integers A and B of sizes m and n, respectively. Wrjte a function named MIX(), which will produce a third array named C, such that the following sequence is followed.
- All even numbers of A from left to right are copied into C from left to right.
- All odd numbers of A from left to right are copied into C from right to left
- All even numbers of B from left to right are copied into C from left to right.
- All odd numbers of B from left to right are copied into C from right to left.
A, B and C are passed as arguments to MIX( ). e.g. A is {3, 2, 1, 7, 6, 3} and B is {9, 3, 5, 6, 2, 8, 10},
the resultant array C is {2, 6, 6, 2, 8, 10, 5, 3, 9, 3, 7, 1, 3}.
Аnswer:
void mix(int A[],int B[],int n.int m) { int C[100],i=0,j=0,k=0,L; L = m+n-1; whi1e(i<n) { if(A[i]%2 == 0) C[k++] = A[i++]; else C[L--] = A[i++]; } whi1e(j<m) { if(B[j]%2 == 0) C[k++] = B[j ++]; else C[L--] = B[j++]; } cout<<"\nThe elements of the array C is :"; for(i=0;i<m+n;i++) cout<<C[i]<<"\t"; } void main() { int A[] = {3,2,1,7,6,3}; int B[] = {9,3,5,6,2,8,10}; mix(A,B,6,7); }
Question 13:
Suppose X, Y, 1 are arrays of integers of sizes M, N and M+N respectively. The numbers in array X and Y appear in descending order. Write a user defined function in C++ to produce third array Z by merging arrays X and Y in descending order.
Аnswer:
const M = 10; const N = 10; void merge() { int X[M]; int Y[N]; int Z[M+N]; int i,j,k; { cout<<"Enter the first array in descending order\n"; for(i=0;i<M;i++) { cin>>X[i]; } cout<<"Enter the second array in descending order\n"; for(i=0;i<N;i++) { cin>>Y[i]; } i=0,j=0,k=0; // Merging the first and second array whi1e((i<M)&&(j<N)) { if(X[i]<Y[j]) Z[k++] = Y[j++]; else Z[k++] = X[i++]; } whi1e(i<M) Z[k++] = X[i++]; whi1e(j<N) Z[k++] = Y[j++]; cout<<"The merged arrays are"; for(k=0;k<(M+N);k++) cout<<"\t"<<Z[k]; }
Question 14:
Write a function checkf( ) to check if the passed array of 10 integers is sorted or not.
The function should return 1 if arranged in ascending order, -1 if arranged in descending order,
0 if it is not sorted.
Аnswer:
int check(int a[10]) { int flag = 0; for(int j=0;j<9;j++) { if(a[j]<a[j+1]) flag = 1; else { flag = 0; break; } } if(flag == 1) return flag; for(j=0;j<9;j++) { if(a[j]>a[j+1]) flag =-1; else { flag = 0; break; } } return flag; }
Question 15:
Write the definition for a function void Transfer (intA[6], int B[6]) in C++, which takes two integer arrays, each containing 6 elements as parameters.
The function should exchange all odd places (is, 1st, 3rd and 5th) of the two arrays, Delhi 2013C
Аnswer:
void Transfer(int A[6],int B[6]) { int i, temp; for(i=1;i<=5;i=i+2) { temp = A[i]; A[i] = B[i]; B[i]= temp; } cout<<"Array A becomes\n"; for(i=0;i<=5;i++) { cout<<A[i]<<"\t"; } cout<<"\n"; cout<<"Array B becomes\n"; for(i=0;i<=5;i++) { cout<<B[i]<<"\t”; } }
Question 16:
Write a function TRANSFER (int A[ ], int B[ ] ,int Size) in C++, to copy the elements of array A into array B
in such a way that all the negative elements of A appear in the beginning of B,
followed by all the positive elements, followed by all the zeroes maintaining their respective orders in array A.
e.g. If the contents of array A are :
7, -23, 3, 0, – 8, – 3, 4, 0
The contents of array B should be:
-23, -8,-3, 7, 3, 4, 0, 0
Аnswer:
void TRANSFER(int A[],int B[],int Size) { int k=0; for(int i=0;i<Size;i++) if(A[i]<0) { B[k]=A[i]; k++; } for(int i=0;i<Size;i++) if(A[i]>0) { B[k]=A[i]; k++; } for(int i=0;i<Size;i++) if(A[i]==0) { B[k]=A[i]; k++; } }
Question 17:
Suppose one dimensional array AR is stored in the memory. Write a function in C++ to display all even numbers present in the array. Also, find the sum of these even numbers.
Аnswer:
void sum(int AR[],int n) { int i,sum=0; cout<<"All even numbers present in the array are:"; for(i=0;i<n;i++) { if(AR[i]%2 == 0) { sum = sum + AR[i]; cout<<"\n"<<AR[i]; } } cout<<"\nThe sum of even numbers is :"<<sum; }
Question 18:
Binary search is to be used on the following sorted array to search 30.
Give the index of the element that would be compared with 30 at every step.
Аnswer:
Search for 30
Step 1
Lower = 0, Upper = 9
Mid = int((Lower + Upper)/2)
= int ((0+9)/2) = int(4.5) = 4
Step 2
A[Mid] = A [4] is 40
40>30 therefore Upper
= Mid – 1=4 – 1 = 3
Step 3
Lower = 0, Upper = 3
Mid = int((Lower + Upper)/2)
= int ((0+3)/2)=int (1.5) = 1
Step 4
A[Mid] = A[ 1 ] is 22
22<30 therefore Lower
= Mid+1 = 1 + 1 = 2
Step 5
Lower = 2, Upper = 3
Mid = int ((Lower +Upper)/2)
= int ((2+3)/2)=int(2.5)= 2
Step 6
A [Mid] = A[2] is 30
30 = 30 therefore search is successful 1.
Question 19:
Write a function in C++ to combine the content of two equi-sized arrays A and B by computing their
corresponding elements with the formula 2*A[i]+3*B[i]; Where i varies from Oto N – 1 and transfer
the resultant content in the third same sized array.
Аnswer:
void addNsave(int A[],int B[],int C[],int N) { for(int i=0;i<=N-1;i++) { C[i]=2*A[i]+3*B[i]; } }
Question 20:
Write a function in C++, that accepts two equal length character arrays and compare each letter of array,
display the greater letter one time more than as many times as the subscript of that element,
e.g. two arrays input are Shyam and Mohan so the program produces the following output:
S
o o
y y y
a a a a
n n n n n
Аnswer:
void print(char a[],char b[]) { for(int i=0;i<strlen(a);i++) { if(a[i]>b[i]) { for(int j=1;j<=i+1;j++) cout<<a[i]<<" "; cout<<endl; } else { for(j=0;j<i+1;j++) cout<<b[i]<<" "; cout<<endl; } } }
Topic – 2
Two Dimensional (2D) Array
Exam Practice
Short Answer Type Questions [2/3 Mark]
Question 1:
Write a user defined function int sumsingle (int A[4][4]) in C++, which finds and returns the sum of all numbers present in the first row of the array, e.g. if the array contains:
then, the function should return 33.
Аnswer:
int sumsingle(int A[4][4]) { int Sum=0; for(int j=0;j<=3;j++) { Sum=Sum + A[0][j]; } return Sum; }
Question 2:
Write a user defined function AddEnd2(int A[ ][4], int N, int M) in C++ to find and display the sum of all the values, which are ending with 2 (i.e. units place is 2). All India 2014
e.g. if the content of array is:
22 16 12
19 5 2
The output should be 36
Аnswer:
void AddEnd2(int A[][4], int N, int M) { int sum = 0; for(int i=0;i<N;i++) { for(int j=0;j<M;j++) { if(A[i][j]%10 == 2) sum += A[i][j]; } } cout<<sum; }
Question 3:
Write a user defined function DispNTen(int L[ ][4],int R,int C) in C++ to find and display all the numbers,
which are not divisible by 10. Delhi 2013
e.g. if the content of array is:
The output should be
17 12 19
Аnswer:
void DispNTen(int L[][4],int R.int C) { int i,j; for(i=0;i<R;i++) for(j=0;j<C;j++) if(L[i][j]%10!= 0) cout<<L[i][j]<<" "; }
Question 4:
Write a function ALTERNATE (intA[][3], int N, int M) in C++ to display all alternate elements from two dimensional array A(starting from A[0][0]). e.g. All India 2012
if the array is containing:
23 54 76
37 19 28
62 13 19
The output will be
23 76 19 62 19
Аnswer:
void ALTERNATE(int A[][3],int N,1nt M) { int i,j; for(i=0;i<=N-1;i++) { if(i%2 == 0) { j = 0; } else { j=i; } whi1e(j<= M-l) { cout<<A[i][j]<<"\t"; j += 2; } } }
Question 5:
Write a DSUM() function in C++ to find sum of diagonal elements of a N*N matrix. All India 2011
Аnswer:
const int N=5; void DSUM(int A[N][N]) { int i,j,sum=0; cout<<"Sum of Diagonal One"; for(i=0;i<N;i++) { sum += A[i][i]; } cout<<sum<<" "<<endl; sum=0; cout<<"Sum of Diagonal Two"; for(i=0;i<N;i++) { sum += A[i][n-(i+1)]; } cout<<sum<<" "; }
Question 6:
Define a function SWAPARR ( ) in C++ to swap (interchange) the first row elements with the last row elements,
for a two dimensional integer array passed as the argument of the function. All India 2009
Аnswer:
void SWAPARR(int a[][], int r, int c) { for (int i=0;i<c;i++) { int t=a[0][i]; a[0][i]=a[r-1][i]; a[r-1][i]=t; } }
Question 7:
Write a COLSUM function in C++ to find sum of each column of a N*M matrix. Delhi 2011
Аnswer:
void COLSUM(int A[4][3],int r,int c) { int Sum[10],i,j; for(j=0;j<c;j++) { Sum[j] = 0; for(i=0;i<r;i++) Sum[j] += A[i][j]; cout<<"Sum of Columns"<<j+1<<" = "; cout<<Sum[j]<<endl; } }
Question 8:
Write a function int ALTERSUM(int B[][5], int N,int M) in C++ to find and return the
sum of elements from all alternate elements of a two dimensional array starting from B[0][0], All India 2010
Аnswer:
int ALTERSUM(int B[][5],int N.int M) { int s=0,C=1; for(int i=0;i<N;i++) for(int j=0;j<M;j++) { if(C%2 != 0) s = s+B[i][j]; C++; } return s; }
Question 9:
Define a function SWAPCOL( ) in C++ to swap (interchange) the first column elements with the last column elements, for a two dimensional integer array passed as the argument of the function. Delhi 2009
e.g. if the two dimensional array contains:
Аnswer:
void SWAPCOK(int A[][4],int M,int N) { int i,temp; for(i=0;i<M;i++) { temp = A[i][0]; A[i][0]=A[i][N-1]; A[i][N-1] = temp; } }
Question 10:
Define a function in C++ to print the product of each column of a two dimensional integer array passed as the argument of the function. Delhi 2008
e.g. if the two dimensional array contains:
Then, the output should appear as
Product of column 1=24
Product of column 2=30
Product of column 3=240
Аnswer:
void ColProd(int A[][10],int r,int c) { int Prod,i,j; for(j=0;j<c;j++) { Prod = 1; for(i=0;i<r;i++) Prod *= A[i][j]; cout<<”product of column"<<j+1<<"="<<Prod<<endl; } }
Question 11:
Write a function in C++, which accepts an integer array and its size as parameters and assign
the elements into two dimensional array of integer in the folio wing format e.g. if an array contains the elements as:
1 2 3 4 5 6
Output would be
Аnswer:
void fun(int arr[],int size) { int a2[10][10],i,j; for(i=0;i<size;i++) { for(j=0;j<size;j++) { if((i+j)>=size) a2[i][j] = 0; else a2[i][j] = arr[j]; cout<<a2[i][j]<<" "; } cout<<endl; } }
Question 12:
Define a function in C++, which accepts a two dimensional array of integer and its size as
arguments and displays the elements, which lie on diagonal.
e.g. if the two dimensional array contains:
5 4 3
6 7 8
1 2 9
Output
Diagonal One : 5 7 9
Diagonal Two : 3 7 1
Аnswer:
void Diagonal s(int A[][3],int n) { int i; cout<<"Diagonal One:"; for(i=0;i<n;i++) cout<<A[i][i]<<" "; cout<<"\nDiagonal Two:"; for(i=0;i<n;i++) cout<<A[i][n-(i+1)]<<" "; }
Question 13:
Define a function in C++, which accepts a two dimensional array of integer and its size as arguments
and displays the elements of middle row and the elements of middle column.
e.g.
if the two dimensional array contains:
5 4 3
6 7 8
1 2 9
Output
Middle row : 6 7 8
Middle column : 4 7 2
Аnswer:
void DispMidRowCol(int Arr[][5], int s) { int mid = s/2, i; cout<<"Middle Row"; for(i=0;i<s;i++) cout<<Arr[mid][i]<<" "; cout<<"Middle Column"; for(i=0;i<s;i++) cout<<Arr[i][mid]<<" "; cout<<endl; }
Question 14:
Write a function in C++ to print the sum of all the values, which are either divisible by 2 or are
divisible by 3 present in a two dimensional array passed as the argument to the function.
Аnswer:
void SumArrElements(int A[][20],int r,int c) { int sum=0; for(int i=0;i<r;i++) for(int j=0;j<c;j++) { if(A[i][j]% 2 == 0||A[i][j]%3==0) sum += A[i][j]; } cout<<"The sum is:"<<sum<<endl; }
Question 15:
Write a function in C++ to find and display the sum of each row and each column of a
two dimensional array of type float. Use the array and its size as parameters with float as its return type.
Аnswer:
void RowColSum(float A[10][10],int r,int c) { int i,j; float RS[10],CS[10]; //Find row sum for(i=0;i<r;i++) { RS[i] = 0; for(j=0;j<C;j++) RS[i] += A[i][j]; } //Find column sum for(j=0;j<c;j++) { CS[j] = 0; for(i=0;i<r;i++) CS[j] += A[i][j]; } //Display 2D array with rowsum and //col sum for(i=0;i<r;i++) { for(j=0;j<c;j++) cout<<A[i][j]<<"\t"; cout<<"sum="<<RS[i]<<endl; } for(j=0;j<c;j++) cout<<"sum="<<CS[j]<<"\t"; cout<<endl; }
Question 16:
Write a user defined function Upper_half(), which takes a two dimensional array A,
with size N rows and N columns as arguments and prints the upper half of the array.
Аnswer:
void Upper_half(int A[][10],int N) { int i,j; for(i=0;i<N;i++) { for(j=0;j<N;j++) { if(i<=j) cout<<A[i][j]<<" "; else cout<<" "; } cout<<"\n"; } }
Question 17:
Write a function MAX in C++, which will return the largest number stored in a two dimensional array of integers.
Аnswer:
int MAX(int a[10][10],int m,int n) { int max = a[0][0]; for(int i=0;i<m;i++) for(int j=0;j<n;j++) { if(a[i][j]>max) max = a[i][j]; } return max; }
Question 18:
An array A[20][30] is stored along the row in the memory with each element requiring
4 Bytes of storage. If the base address of array A is 32000, find out the location ofA[15][10], Also,
find the total number of elements present in this array. All India 2014
Аnswer:
Address of A[i][j] = BaseAddress + [(ixNo.of columns)+j] x size of each element Here, BaseAddress = 32000 Number of rows = 20, Number of columns = 30 Size of each element = 4 Bytes i = 15, j = 10 Address of A[15][10] = 32000+[(15X30) +10] x4 = 32000+[460x4] = 32000+1840 = 33840 Total number of elements = 20 x 30 = 600
Question 19:
An array P[15][10] is stored along the column in the
memory with each element requiring 4 bytes of storage. If the base address of array P is 1400,
find out the location of P[8][5]. Delhi 2013
Аnswer:
P[i][j] = B + W*[i + j x Number of row] P[8][5] = 1400 + 4[8+5(15)] = 1400+4[75+8] = 1400+4[83] = 1400+332 = 1732
Question 20:
An array T[15][10] is stored along the row in the
memory with each element requiring 8 bytes of storage. If the base address of array T is 14000,
find out the location of T[10][7]. All India 2013
Аnswer:
T[i][j] = B + W[n(I-Lr)+(J-Lc)] T[10][7] = 14000+8[10(10)+7] = 14000+8[100 + 7] = 14000+8[107] = 14000+856 = 14856
Question 21:
An array S[ 10][30] is stored in the memory along the column with each of the element occupying 2 bytes.
Find out the memory location ofS[5][10], if the element S[2][15] is stored at the location 8200. Delhi 2012
Аnswer:
S[I][J] = B + W[(I-I1) + n(J-J1)] S[2][15] = B + 2[(2-0)+10*(15-0)] 8200 = B + 2[152] 8200 = B + 304 B = 8200 - 304 B = 7896 S[I][J] = B + W[(I-I1)+n(J-J1)] S[5][10] = 7896 + 2[5 + 10 * 10] = 7896 + 2 [105] = 7896 + 210 S[5][10] = 8106
Question 22:
An array P[20] [50] is stored in the memory along the column with each of the element occupying 4 bytes,
find out the memory location for the element P[15][10], if P[0][0] is stored at the location 5200. Delhi 2011
Аnswer:
Given array is column wise stored LOC(P[I][J]) = Base(P)+W(l+J*M) LOC(P[0][0]) = Base address = 5200 W = 4 I = 15 J = 10 M = 20 LOC(P[ 15][10])= Base(P) + 4(15 + 10*20) LOC(P[15][10])= 5200 + 4(15+200) = 5200 + 4(215) = 5200 + 860 = 6060
Question 23:
An array G[50][20] is stored in the memory along the
row with each of the element occupying 8 bytes, find out the memory location for the element G[10][15],
if G[0][0] is stored at 4200. All India 2011
Аnswer:
G[0][0]=Base address = B=4200 R=50 C=20 Element size = 8 bytes lr=0 J=15 1=10 Jc=0 Given array row wise stored G[I][J] =B+W[C(I-Ir)+(J-Jc)] G[10][15] = 4200 + 8[20(10-0)+(15-0)] = 4200+8(20(10)+15) = 4200+8(215)= 4200+1720 = 5920
Question 24:
An array P[50][‘60] is stored in the memory along the column with each of the element occupying 2 bytes,
find out the memory location for the element P[10][20], if the base address of the array is 6800. Delhi 2010
Аnswer:
Given array is column wise stored.
LOC(P[I] [J] ) = Base(P)+W(I+J*M)
Base address=6800
1=10
J=20
M=50
W=2
LOC(P[10][20]) = 6800 +2(10+20*50)
= 6800 +2(10+1000)
= 6800 +2020 = 8820
Question 25:
An array T[90][100] is stored in the memory along the column with each of the element occupying 4 bytes, find out the memory location for the element T[W][40], if the base address of the array is 7200. All India 2010
Аnswer:
LOC(T[I][J] ) = Base(T)+W(I+J*M) LOC(T[10][40]) = 7200+4(10+40*90) = 7200+4(10+3600) = 7200+14440 = 21640
Question 26:
An array S[40] [30] is stored in the memory along the column with each of the elements occupying 4 bytes,
find out the base address and address of element S[20][15], if an element S[15][10] is
stored at the memory location 7200. Delhi 2009
Аnswer:
For column wise allocation Address of S[I][J] = Base + W*[(I-Ir)+(J-Jc)*M] So, address of S[15][10] = Base + 4*((15-0)+(10-0)*40) 7200 = Base + 4*(400 + 15) 7200 = Base + 4*415 7200 = Base + 1660 Base = 7200- 1660= 5540 Now, address of S[20] [15] = Base + [(J-0)*M+(I-0)]*4 = 5540 + [(15-0)*40+(20-0)]*4 = 5540 + (15*40+20)*4 = 5540 + (600+20)*4 = 5540 + 620*4 = 5540 + 2480 = 8020
Question 27:
An array Arr[50][10] is stored in the memory along the row with each of the elements occupying 2 bytes.
Find out the base address and address of element Arr[20][50], if an element Arr[ 10][25] is stored at the memory location 10000. All India 2008
Аnswer:
Number of rows = 50 Number of columns = 10 Element size=2 Address of Arr[10][25[ = 10000 Base address B=? Lowest row lr=0 Lowest column lc=0 For row-wise allocation Arr[P][Q] = B+W[C(P-Ir)+(Q-Ic)] Arr[10][25] = B+2[10(10-0)+(25-0)] = B + 2(125) 10000 = B+250 B = 10000-250 = 9750 Arr[i][j] = B+W[C(i-Ir)+(j-Ic)] Arr[20][50] = 9750+2[10(20-0)+(50-0)] = 9750+2*250 = 9750 + 500 = 10250
Question 28:
An array MAT[30][10] is stored in the memory along the column with each of the elements occupying 8 bytes.
Find out the base address and address of element MAT[20][5], if an element MAT[5][7] is stored at the memory location 1000.
Аnswer:
Base Address B= ? Number of rows M = 30 Element size W = 8 Ir, Ic = 0 Array in column major order Address of MAT[I][J] = B+W(M(J-Ic)+(1-Ir)) MAT[5][7] = 1000 1000 = B + 8 [30(7-0)+(5-0)] 1000 = B + 8 [30(7)+(5)] 1000 = B + 8 (210+5) 1000 = B + 8 (215) 1000 = B + 1720 B = 1000-1720 B = -720 Adderss of MAT[20][5] = -720 + 8 [30 (5-0) + (20-0)] = -720 + 8 [30(5) + 20] = -720 + 8 (150 + 20) = -720 + 8 (170) = -720 + 1360 = 640
Question 29:
An array A[15][20] is stored in the memory along the row with each of the elements occupying 4 bytes.
Find out the base address and address of element A[3][2], if an element A[5][2] is stored at the memory location 1500.
Аnswer:
A[R][C] = A[15][20] R = 15 C = 20 Element size=4 bytes Ir = 0 Jc = 0 A[5][2] = 1500 To find base address B A[I][J] = B+W[C(I-Ir)+(J-Jc)] A[5][2] = B+4[20(5-0)+(2-0)] 1500 = B+408 B = 1500-408 B = 1092 To find address of A[3][2], A[3][2] = 1092+4[20(3-0)+(2-0)] = 1092+248 = 1340
Question 30:
An array A[35][l 5] is stored in the memory along the row with each of the elements occupying 4 bytes.
Find out the base address and address of element A[20][5], if an element A[2][2] is stored at the memory location 3000.
Аnswer:
A[R][C] = A[35][ 15] R = 35 C = 15 Element size = 4bytes Ir = 0 Jc = 0 A[2][2] = 3000 To find base address B, A[I][J] = B+W[C(I-Ir)+(J-Jc)] A[2][2] = B+4[ 15(2-0)+(2-0)] 3000 = B+128 B = 3000-128 B = 2872 To find address of A[20][5], A[20][5] = 2872+4(15(20-0)+(5-0)] = 2872+1220 = 4092 Arr[2][5] = 4092
Question 31:
An array V[15][30] is stored in the memory with each of the elements occupying 8 bytes.
If the base address of V is 5300, find out memory locations of V[8][12] and V[12][2], if the array is stored along row.
Аnswer:
V[R][C] = V[15][30] R = 15 C = 30 Element size = 8 bytes Ir = 0 Jc = 0 B = 5300 Row wise Address of V[I][J] = B+W[C(I-Ir)+(J-Jc)] V[8][12] = 5300+8 [30(8-0)+( 12-0)] = 5300+2016 = 7316 Address of V[I][J] = B+W [C(I-Ir)+(J-Jc)] V[12][2] = 5300+8[30(12-0)+(2-0)] = 5300+2896 = 8196
Question 32:
An array Arr[5][5] is stored in the memory with each of the elements occupying 4 bytes.
Assuming the base address of Arr to be 1000, compute the address of Arr[2][4], when the array is stored
(i) Row wise
(ii) Column wise
Аnswer:
Base address, B= 1000 R = 5 C = 5 Element size = 4 bytes Ir = 0 Jc = 0 I = 2 J = 4 (i) Row wise Arr[I][J] = B+W[C(I—Ir)+(J—Jc)] Arr[2][4] = 1000+4[5(2-0)+(4-0)] = 1000+56 = 1056 (ii) Column wise Arr [I][J] = B+W[(I-Ir)+ R(J-Jc)] Arr[2][4] = 1000+4[(2-0)+5(4-0)] = 1000+88= 1088
Question 33:
An array VAL [1. .10] [1..10] requires 8 bytes of storage. If the base address of array VAL is 1500,
determine the location of VAL [4][5], when the array VAL is stored
(i) Row wise
(ii) Column wise.
Аnswer:
Given Base = 1500 W = 8 bytes N = 10 M = 10 I = 4 J = 5 (i) To find Row Major Order VAL[I][J] = B+[(I-1)*N+(J-1)]*W VAL[4][5] = 1500+[(4-1)*10+(5-1)]*8 = 1500+(34)*8 = 1500+272 = 1772 (ii) To find Column Major Order VAL[I][J] = B+[(J-1)*M +(I-1)]*W VAL[4][5] = 1500+[(5-1)*10 +(4-1)]*8 = 1500+(43)*8 = 1500+344 = 1844
Computer ScienceChapterwise Question Bank for Computer ScienceNCERT Solutions