Page 129 - C++
P. 129
b) An array T[50][20] 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 T[30][15], if an element T[25][10] is stored at the memory location
9800. 3
Ans: Number of row=m=50
Number of columns=n=20
Width of element=w=4
Loc( T[i][j])= b+(i+ j * m)*w
Loc( T[25][10]) =9800 given
I=25, j=10
B= base address
So 9800=B+(25+10 *50)*4
B= 9800 – 2300=7500
Now loc( T[30][15]) i=30, j=15
Loc= 7500 +(30 +15 * 50)*4
=7500 +3120=10620
c) Write a function displayalternate() to display alternate element starting
from [0][0] position of 2D array of MXN size. Function is having array, number
of row and column as parameter 2
Ans: void displayalternate( int a[][5], int m, int n)
{ // converting 2d to 1d
int k=0;
for( int i=0; i<m; i++)
{
for(int j=0; j<n; j++, k++)
b[k]=a[i][j];
}
// displaying alternate element
for( i=0 i<k; i+=2)
cout<<b[i];
}
d) Evaluate the following postfix expression using stack. Show status of stack
after each operation. False, True, NOT, OR, True, False, AND, OR 2
Ans:
Scanned operation Stack status Result
element
False push False
True push False True
XII / Comp. Sc. Page 8 of 17