Programming and data structure miscellaneous
- Consider the following C function in which size is the number of elements in the array E :
int MyX(int*E, unsigned int size)
{
int Y = 0;
int Z;
int i, j, k;
for (i = 0; i < size; i++)
Y = Y + E[i];
for (i = 0; i < size; i++)
for (j = i; j < size; j++)
{
Z = 0;
for (k = i; k <= j; k++)
Z = Z + E[k];
if (Z > Y)
Y = Z;
}
return Y;
}
The value returned by the function MyX is the
-
View Hint View Answer Discuss in Forum
Clearly the code segment : for
(i = 0; i < size; i + +) Y = Y + E [i]
Computes the sum of all element in the array E.
Note : As the array E may contain negative elements, the sum of elements of subarray of E may be greater than the sum of all elements in the array E. Now, comes the nested i, j, k loops :
‘i’ changes the starting of the subway.
‘j’ changes the end position of the subarray. ‘k’ loop is used to compute sum of elements of the particular subarray. The sum of subarray is stored in z and is compared to the earlier max sum Y. Finally the larger sum gets stored in Y.
This can be easily seen how sum of subarray is computed with different values of i and j.Correct Option: A
Clearly the code segment : for
(i = 0; i < size; i + +) Y = Y + E [i]
Computes the sum of all element in the array E.
Note : As the array E may contain negative elements, the sum of elements of subarray of E may be greater than the sum of all elements in the array E. Now, comes the nested i, j, k loops :
‘i’ changes the starting of the subway.
‘j’ changes the end position of the subarray. ‘k’ loop is used to compute sum of elements of the particular subarray. The sum of subarray is stored in z and is compared to the earlier max sum Y. Finally the larger sum gets stored in Y.
This can be easily seen how sum of subarray is computed with different values of i and j.
- Consider the following two C code segments. Y and X are one and two dimensional arrays of size n and n × n respectively, where 2 ≤ n ≤ 10. Assume that in both code segments, elements of Y are initialized to 0 and each element X[i] [j] of array X is initialized to i + j. Further assume that when stored in main memory all elements of X are in same main memory page frame.
Code segment 1 :
//initialize elements of Y to 0
//initialize elements X[i] [j] of X to i+j
for (i = 0; i < n; i++)
Y[i] += X[0] [i];
Code segment 2 :
//initialize elements of Y to 0
//initialize elements X[i] [j] of X to i+j
for (i = 0; i < n; i++)
Y[i] += X[i] [0];
Which of the following statements is/are correct?
S1 : Final contents of array Y will be same in both code segments
S2 : Elements of array X accessed inside the for loop shown in code segment 1 are contiguous in main memory
S3 : Elements of array X accessed inside the for loop shown in code segment 2 are contiguous in main memory
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
NA
- Consider the following C program. #include
int main() { static int a[] = {10, 20, 30, 40, 50}; static int *p[] = {a, a+3, a+4, a+1, a+2}; int **ptr = p; ptr++; printf(“%d%d”, ptr-p, **ptr); } The output of the program is ______.
-
View Hint View Answer Discuss in Forum
Correct Option: B
- Suppose C = ❮c[0] ,......,c[k - ]❯ is an array of length k, where all the entries are from the set {0, 1}. For any positive integers a and n, consider the following pseudocode.
DOSOMETHING (c, a, n)
z ← 1
for i ← 0 to k – 1
do z ← z2 mod n
if c[i] = 1
then z ← (z × a) mod n
rutrun z
If k = 4, c = ❮1, 0,1, 1❯ , a = 2 and n = 8, then the output of DOSOMETHING (c, a, n) is _________.
-
View Hint View Answer Discuss in Forum
C
i 0 1 1
20 something
Correct Option: C
C
i 0 1 1
20 something
- A Young tableau is a 2D array of integers increasing from left to right and from top to bottom. Any unfilled entries are marked with ∞, and hence there cannot be any entry to the right of, or below a ∞. The following Young tableau consists of unique entries.
When an element is removed from a Young tableau, other elements should be moved into its place so that the resulting table is still a Young tableau (unfilled entries may be filled in with a ∞). The minimum number of entries (other than 1) to be shifted, to remove 1 from the given Young tableau is ________.
-
View Hint View Answer Discuss in Forum
Correct Option: A