DS Solutions (Arrays) - 1
DS Solutions (Arrays) - 1
DS Solutions (Arrays) - 1
(Solutions)
Assuming that each integer takes one memory location the array is stored in row-major order and
first element of the array is stored at location 200, what is the address of the element A [i] [j]?
Explanation:
= 200 + (𝑖 − 1)10 + (𝑗 − 1)
= 200 + 10𝑖 − 10 + 𝑗 − 1
= 189 + 10𝑖 + 𝑗
var p, q : integer;
begin
p:= p+q;
q:= p−q;
p:= p−q;
end;
(a) exchanges (p) and (q) (b) doubles (p) and stored in (q)
(c) doubles (q) and stores in (p) (d) leaves (p) and (q) unchanged
1
Explanation:
var M: integer;
procedure FIND (M: real);
begin
M:=sqrt(M);
end;
begin
M:= 25;
FIND (M)
Writeln (M)
end.
(a) 5 (b) 25
Explanation:
2
Explanation:
*In Indirect addressing mode the instruction does not have the address of the data to be operated
on, but the instruction points where the address is stored.
*In Auto decrement addressing mode, before determining the effective address, the value in the
base register is decremented by the size of the data item which is to be accessed.
* In immediate addressing mode the data is to be used is immediately given in instruction itself,
so it deals with constant data.
5. Consider the following declaration of a two dimensional array in C. Assume RMO and index
starting from ‘0’.
Assuming that the main memory is byte addressable and that the array is stored starting from
memory address 100, the address of L [50] [60] is
Explanation:
Address of L[50][60]
1. M [2][4]
2. N [2]
3
3. M [3]
4. N [2][4]
Which will not give compile time errors if used as left hand sides of assignment statements in a
C program?
Explanation:
Here int *M[10] is an array of 10 pointers which can be M[2][4] as its left hand side.
It can also be M[3] as its left hand side and also N[2][4] can obviously be used as left hand side
as it is given in declaration of N[10][10] only, but N[2] cannot be used because it is declared as
2-dimensional array.
#include <stdio.h>
#define print (a) printf (“%d”, a)
int a;
void A (int p)
{
p+ = a;
print (p);
}
void B(int *q)
{
int p = *q + 3;
A (a);
*q = a – 2;
print (a);
}
Main (void)
{
a = 6;
4
B(&a);
print(a);
}
Explanation:
a = 6;
B(&a);
p = *q + 3 = 6 + 3 = 9;
A(a), a = 6;
print (a) = 6
a 6
100
p=6+3=9
A(6)
P = 15
*q = 7
8.main ( )
{
int a = 2, b, c;
a* = b = c = 4;
a = b = c;
printf (“%d”, a);
5
a = = (b = c);
printf (“%d”, a);
}
(a) 1, 4 (b) 4, 4
(c) 1, 1 (d) 8, 1
Explanation:
The statement (a* = b = c = 4;) assign the value of a with 8, but in the next statement a = b = c.
Here “ = = “ is for the test of quality operator. It will get priority over ‘=’, so equality operator
yield a result of TRUE (b = c) and integer 1 will be assigned to a.
So equality operator yield FALSE and value of expression is ‘0’. But as equality operator does
not change the operand value, so value of a will be remain 1.
A = 100
for i = 1 to m do
for j = 1 to m do
{
Temp = B [i] [j] + A
B [i] [j] = B [j] [i]
B [j] [i] = Temp – A
}
for i = 1 to m do
for j = 1 to m do
Output (B[i][j]);
6
(c) Adding 100 to the upper diagonal elements and subtracting 100 from diagonal elements of B
Explanation:
If we take a look at the inner statements of first loop, we can notice that the statements Swap
B[i][j] and B[j][i] for all i and j. Since the loop runs for all elements, every element B[l][m]
would be swapped twice, once for i = l and j = m and then for i = m and j = l,
(a) p – 1 (b) p2 – 3p + 2
Explanation:
1 1 … 1 1 2 … 𝑝
2 2 … 2 1 2 … 𝑝
Total sum = ∑𝑝𝑖=1 ∑𝑝𝑗=1 ( ⋮ ⋮ 𝑝 𝑝
⋱ ⋮ ) − ∑𝑖=1 ∑𝑗=1 ( ⋮ ⋮ ⋱ ⋮
)=0
𝑝 𝑝 … 𝑝 1 2 … 𝑝