August 1999
CA208 : 'C' PROGRAMMING

QUESTION 1 (Compulsory)

Total Marks: 20 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 1

(a) Using single statements,

(i) Define a type Boolean, which can have the enumerated types either true or false;

(ii) Declare a two dimensional character array called counter of four elements by three elements, and initialize it with the values o,n,e,t,w,o,t,r,e;

(iii) Declare a pointer to an array of doubles, with the size of the array unknown, called mrw, which may have values implicitly changes and therefore should not be optimized.

 

[1]


[2]

 

[2]

(i) enum Boolean {true, false};

(ii) char counter[4][3] = {'o', 'n', 'e', 't', 'w', 'o', 't', 'r', 'e' }

(iii) volatile double mrw[ ];

 

(b) What would be the result of evaluating the following expressions :

(i) 8 & 7

(ii) 8 && 7

(iii) 8 | 7

(iv) 8 || 7

(v) 3 >> 2

(vi) 3 << 2

 

[1]

[1]

[1]

[1]

[1]

[1]

(i) 0

(ii) 1

(iii) 15

(iv) 1

(v) 0

(vi) 12

 

(c) When a 2-dimensional matrix is transposed, the rows of the matrix are interchanged with the columns of a matrix. This means that an element in position [ i, j ] of the original matrix would be in position [ j, i ] in the transposed matrix.

Therefore if we had a 4 * 2 matrix M,

pic1.gif (1078 bytes)

the transposition would be a 2 * 4 matrix M',

pic2.gif (1642 bytes)

Write a function
void transpose(float matrix[] [ncols], float result[] [nrows])
which takes two reference parameters matrix and result, and will perform a transposition operation on matrix and place the result in result.

 

 

 

 

 

 

 


[3]

A sample definition of transpose is given below:

void transpose (float matrix[ ] [ncols], float result [ ] [nrows] )
{
     int row, col;
     for(col=0; col<ncols; col++)
          for (row=0; row<nrows; row++)
              result[col][row] = matrix[row][col] ;
}

 

(d) In statistics, the mode is defined as being the most commonly occurring element in a list. So, for example, in the list (1,2,3,3,3,2,1) the mode would be the number 3, as it occurs the most times. Write the function, the signature of which given below, which will take 2 parameters. The first parameter is a pointer to an integer array, and the second parameter is the length of that array. All of the elements stored in the array are within the range 0 -- 10. The function should examine the array, and return the mode.
int mode (int input[], int size);
[6]
A sample definition of mode follows:

int mode(int input[ ], int size)
{
   int count;
   int m=0;
   int appearances[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

   for(count=0l count<10; count++)
          if(appearances[count] > m)
               m = count;

   return m;
}