Subscribe:Posts Comments | Read Articles on Various Topics - Pro Articles

You Are Here: Home » C and C++, Technical Interview Questions » C & C++ Software Developers Test Questions

C and C++ Technical Questions for placement papers and technical interviews for software engineers recruitment tests / employment tests.

  1. Variably Dimensioned Arrays
    While dealing with Scientific or Engineering problems one is often required to make use of multi-dimensioned array. However, when it comes to passing multidimensional arrays to a function C is found wanting. This is because the C compiler wants to know the size of all but the first dimension of any array passed to a function. For instance, we can define a function compute ( int n, float x[] ), but not compute ( int n, x[][]).
    Thus, C can deal with variably dimensioned 1-D arrays, but when an array has more than one dimension, the C compiler has to know the size of the last dimensions expressed as a constant. This problem has long been recognized, and some of the solutions that are often used are:
    Declare the arrays in the functions to be big enough to tackle all possible situations. This can lead to a wastage of lot of precious memory in most cases. Another solution is to construct multiple-dimension array as an array of pointers. For example, a matrix (2-D array) of floats can be declared as a 1-D array of float pointers, with each element pointing to an array of floats. The problem with this method is that the calling function has to define all arrays in this fashion. This means that any other computations done on the arrays must take this special structure into account.
    Another easy solution, though seldom used, exists. This is based on the following method:
    Pass the array to the function as though it is a pointer to an array of floats (or the appropriate data type), no matter how many dimensions the array actually has, along with the dimensions of the array. Reference individual array elements as offsets from this pointer.Write your algorithm so that array elements are accessed in storage order. The following program for multiplying two matrices illustrates this
    procedure.
    # define M 3
    # define N 2
    # define P 4
    float a[M][N], b[N][P], c[M][P] ;
    void mulmat ( int, int, int, float*, float*, float* ) ;
    main( )
    {
    int i, j ;
    for ( i = 0 ; i < M ; i++ )
    for ( j = 0 ; j < N ; j++ )
    a[i][j] = i + j ;

    for ( i = 0 ; i < N ; i++ )
    for ( j = 0 ; j < P ; j++ )
    b[i][j] = i + j ;
    mulmat ( M, N, P, a, b, c ) ;
    for ( i = 0 ; i < M ; i++ )
    {
    printf ( “\n” ) ;
    for ( j = 0 ; j < P ; j++ )
    printf ( “%f\t”, c[i][j] ) ;
    }
    }
    void mulmat ( int m, int n, int p, float *a, float *b, float *c )
    {
    float *ptrtob, *ptrtoc ;
    int i, j, k, nc ;

    /* set all elements of matrix c to 0 */
    for ( i = 0 ; i < m * p ; i++ )
    *( c + i ) = 0 ;
    for ( i = 0 ; i < m ; i++ )
    {
    ptrtob = b ;
    for ( k = 0 ; k < n ; k++ )
    {
    ptrtoc = c ;
    for ( j = 0 ; j < p ; j++ )
    *ptrtoc++ += *a * *ptrtob++ ;
    a++ ;
    }
    c += p ;
    }
    }
    We know that C stores array elements in a row-major order. Hence to ensure that the elements are accessed in the storage order the above program uses a variation of the normal matrix-multiplication procedure. The pseudo code for this is given below:
    for i = 1 to m
    for j = 1 to p
    c[i][j] = 0
    end
    for k = 1 to n
    for j = 1 to p
    c[i][j] = c[i][j] + a[i][k] * b[k][j]
    end
    end
    end

  2. Why is it not possible to scan strings from keyboard in case of array of pointers to string?
    Ans: When an array is declared, dimension of array should be specified so that compiler can allocate memory for the array. When array of pointers to strings is declared its elements would contain garbage addresses. These addresses would be passed to scanf( ). So strings can be received but they would get stored at unkown locations. This is unsafe.

  3. Bit Arrays
    If in a program a variable is to take only two values 1 and 0, we really need only a single bit to store it. Similarly, if a variable is to take values from 0 to 3, then two bits are sufficient to store these values. And if a variable is to take values from 0 through 7, then three bits will be enough, and so on. Why waste an entire integer when one or two or three bits will do? Because there aren’t any one bit or two bit or three bit data types available in C. However, when there are several variables whose maximum values are small enough to pack into a single memory location, we can use `bit fields’ to store several values in a single integer. Bit fields are discussed in most standard C texts. They are usually used when we want to store assorted information which can be accommodated in 1, 2, 3 bits etc.
    For example, the following data about an employee can be easily stored using bit fields.
    male or female
    single, married, divorced or widowed
    have one of the eight different hobbies
    can choose from any of the fifteen different schemes proposed by the company to pursue his/her hobby.
    This means we need one bit to store gender, two to store marital status, three for hobby, and four for scheme (with one value used for those who are not desirous of availing any of the schemes). We need ten bits altogether, which means we can pack all this information into a single integer, since an integer is 16 bits long.
    At times we may need to store several True or False statuses. In such cases instead of using bit fields using an array of bits would be more sensible. On this array we may be required to perform the following operations:
    Set a bit (make it 1).
    Clear a bit (make it 0).
    Test the status of a bit in the array.
    Reach the appropriate bit slot in the array.
    Generate a bit mask for setting and clearing a bit.
    We can implement these operations using macros given below:
    #define CHARSIZE 8
    #define MASK ( y ) ( 1 << y % CHARSIZE )
    #define BITSLOT ( y ) ( y / CHARSIZE )
    #define SET ( x, y ) ( x[BITSLOT( y )] |= MASK( y ) )
    #define CLEAR ( x, y ) ( x[BITSLOT( y )] &= ~MASK( y ) )
    #define TEST ( x, y ) ( x[BITSLOT( y )] & MASK( y ) )
    #define NUMSLOTS ( n ) ( ( n + CHARSIZE – 1) / CHARSIZE )
    Using these macros we can declare an array of 50 bits be saying,
    char arr[NUMSLOTS(50)] ;
    To set the 20th bit we can say,
    SET(arr, 20 ) ;
    And if we are to test the status of 40th bit we may say,
    if ( TEST ( arr, 40 ) )
    Using bit arrays often results into saving a lot of precious memory. For example, the following program which implements the Sieve of Eratosthenes for generating prime numbers smaller than 100 requires only 13 bytes. Had we implemented the same logic using an array of integers we would have required an array of 100 integers, that is 200 bytes.
    #include
    #include
    #define MAX 100
    main( )
    {
    char arr[NUMSLOTS( MAX )] ;
    int i, j ;
    memset ( arr, 0, NUMSLOTS( MAX ) ) ;
    for ( i = 2 ; i < MAX ; i++ )
    {
    if ( !TEST ( arr, i ) )
    {
    printf ( “\n%d”, i ) ;
    for ( j = i + i ; j < MAX ; j += i )
    SET ( arr, j ) ;
    }
    }
    }

See more C and C++ Computer language interview questions and written test paper questions for developers, engineers, programmers latest job interviews and exam papers.

Related Posts

Leave a Reply

© Copyright 2010 Latestexams. All Rights Reserved | Privacy Policy | Terms & Conditions