Monday, July 16, 2018

C Questions And Answers – 2018A16

There are many commonly asked questions regarding C programming language. Below are some collected such question-answer examples. The questions are usually related with Turbo C IDE in windows or GCC under Linux environment [not always].

For more such examples, click C_Q&A label.

 

-------------------------------------------------------------------------------------------------

 

Towers Of Hanoi

Suppose there are three pegs labeled A, B and C. Four disks are placed on peg A. The

bottom-most disk is largest, and disks go on decreasing in size with the topmost disk being smallest. The objective of the game is to move the disks from peg A to peg C, using peg B as an auxiliary peg. The rules of the game are as follows:

 

Only one disk may be moved at a time, and it must be the top disk on one of the pegs. A

larger disk should never be placed on the top of a smaller disk. Suppose we are to write a

program to print out the sequence in which the disks should be moved such that all disks on peg A are finally transferred to peg C. Here it is...

 

main( )

{

int n = 4 ;

move ( n, 'A', 'B', 'C' ) ;

}

 

move ( n, sp, ap, ep )

int n ;

char sp, ap, ep ;

{

if ( n == 1 )

printf ( "\n Move from %c to %c ", sp, ep ) ;

else

{

move ( n - 1, sp, ep, ap ) ;

move ( 1, sp, ' ', ep ) ;

move ( n - 1, ap, sp, ep ) ;

}

}

 

And here is the output...

Move from A to B

Move from A to C

Move from B to C

Move from A to B

Move from C to A

Move from C to B

Move from A to B

Move from A to C

Move from B to C

Move from B to A

Move from C to A

Move from B to C

Move from A to B

Move from A to C

Move from B to C

 

This problem is the famous Towers of Hanoi problem, wherein three pegs are to be employed for transferring the disks with the given criteria. Here's how we go about it. We have three pegs: the starting peg, sp, the auxiliary peg ap, and the ending peg, ep, where the disks must finally be. First, using the ending peg as an auxiliary or supporting peg, we transfer all but the last disk to ap. Next the last disk is moved from sp to ep. Now, using sp as the supporting peg, all the disks are moved from ap to ep. ‘A’, B and C denote the three pegs. The recursive function move( ) is called with different combinations of these pegs as starting, auxiliary and ending pegs.

 

-------------------------------------------------------------------------------------------------

 

What would be the output of following program?

 

struct syntax

{

int i ;

float g ;

char c ;

}

main( )

{

printf ( "I won't give you any error" ) ;

}

 

Ans:

The above program would get compiled successfully and on execution it would print the message given in printf(). What strikes in the above code snippet is the structure syntax which is declared but not terminated with the statement terminator, the semicolon. The compiler would not give any error message for it, as it assumes that main( ) function have a return type of struct syntax and hence would successfully compile and execute the program.

 

-------------------------------------------------------------------------------------------------

 

Allocating memory for a 3-D array

 

#include "alloc.h"

#define MAXX 3

#define MAXY 4

#define MAXZ 5

main( )

{

int ***p, i, j, k ;

p = ( int *** ) malloc ( MAXX * sizeof ( int ** ) ) ;

for ( i = 0 ; i < MAXX ; i++ )

{

p[i] = ( int ** ) malloc ( MAXY * sizeof ( int * ) ) ;

for ( j = 0 ; j < MAXY ; j++ )

p[i][j] = ( int * ) malloc ( MAXZ * sizeof ( int ) ) ;

}

for (k=0;k<MAXZ ;k++ )

{

for (i=0;i<MAXX ;i++ )

{

for (j=0;j<MAXY ;j++ )

{

p[i][j][k] = i + j + k ;

printf ( "%d ", p[i][j][k] ) ;

}

printf ( "\n" ) ;

}

printf ( "\n\n" ) ;

}

}

 

-------------------------------------------------------------------------------------------------

 

Data Structures

 

How to distinguish between a binary tree and a tree?

 

Ans:

A node in a tree can have any number of branches. While a binary tree is a tree structure in which any node can have at most two branches. For binary trees we distinguish between the subtree on the left and subtree on the right, whereas for trees the order of the

subtrees is irrelevant.

 

Consider the following figure...

 

This above figure shows two binary trees, but these binary trees are different. The first has an empty right subtree while the second has an empty left subtree. If the above are regarded as trees (not the binary trees), then they are same despite the fact that they are drawn differently. Also, an empty binary tree can exist, but there is no tree having zero nodes.

 

-------------------------------------------------------------------------------------------------

 

…till next post, bye-bye & take care.

No comments:

Post a Comment