Sunday, April 23, 2006

FAQ's of C.. Check out your C Knowledge... Part VI

61.
How come the output for both the programs is different when the logic is same?main( ){int i, j ;for ( i = 1, j = 1 ; i <= 5, j <= 100 ; i++, j++ ){gotoxy ( 1, 1, ) ;printf ( "%d %d", i, j ) ;}}main( ){int i, j ;for ( i =1, j = 1; j <= 100, i <= 5; i++, j++ ){gotoxy ( 1, 1 ) ;printf ( "%d %d", i, j ) ;}}Output -> 5 5Even if logic of both the programs is same the output of the first program comes out to be 100, 100, but of the second program it is 5, 5. The comma operator plays a vital role inside the for loop. It always considers the value of the latest variable. So, at the time of testing the condition in for loop, the value of j will be considered in the first program and value of i in the second.-------------------------------------------------------------------------------------------------62. Can we get the x and y coordinate of the current cursor position ?Ans : The function wherex( ) and wherey( ) returns the x-coordinate and y-coordinate of the current cursor position respectively. Both the functions return an integer value. The value returned by wherex( ) is the horizontal position of cursor and the value returned by wherey( ) is the vertical position of the cursor. Following program shows how to use the wherex( ) and wherey( ) functions.#include main( ){printf ( "Just\n To\n Test\n Where\n the cursor\n goes" ) ;printf ( "Current location is X: %d Y: %d\n", wherex( ), wherey( ) ) ;}
63. How do I programmatically delete lines in the text window?Ans: While writing programs that perform screen-based I/O, you may want to-delete the current line's contents, moving one line up, all of the output that follows. In such cases a function called delline( ) can be used. Following code snippet illustrates the use of function delline( ).#include main( ){int i ;clrscr( ) ;for ( i = 0; i <= 23; i++ )printf ( "Line %d\r\n", i ) ;printf ( "Press a key to continue : " ) ;getch( ) ;gotoxy ( 2, 6 ) ;for ( i = 6; i <= 12; i++ )delline( ) ;getch( ) ;}-------------------------------------------------------------------------------------------------64. How do I get the time elapsed between two function calls ?Ans: The function difftime( ) finds the difference between two times. It calculates the elapsed time in seconds and returns the difference between two times as a double value.#include #include #include main( ){int a[] = { 2, -34, 56, 78, 112, 33, -7, 11, 45, 29, 6 } ;int s ;time_t t1, t2 ; // time_t defines the value used for time functions = sizeof ( a ) / 2 ;t1 = time ( NULL ) ;sel_sort ( a, s ) ; // sort array by selection sortbub_sort ( a, s ) ; // sort array by bubble sort methodt2 = time ( NULL ) ;printf ( "\nThe difference between two function calls is %f", difftime ( t2, t1 ) ) ;}In the above program we have called difftime( ) function that returns the time elapsed from t1 to t2.-------------------------------------------------------------------------------------------------65.
How do I use swab( ) in my program ?Ans: The function swab( ) swaps the adjacent bytes of memory. It copies the bytes from source string to the target string, provided that the number of characters in the source string is even. While copying, it swaps the bytes which are then assigned to the target string.
#include #include #include main ( ) {char *str1 = "hS eesll snsiasl not eh es as oher " ;char *str2 ;clrscr( ) ;swab ( str1, str2, strlen ( str1 ) ) ;printf ( "The target string is : %s\n", str2 ) ; // output -- She sells snails on the sea shoregetch( ) ;}-------------------------------------------------------------------------------------------------66.
Turbo C provides various command line compiler options which we can use through TCC. The compiler options include : displaying specific warning messages, generating 8087 hardware instructions, using a filename for generating assembly code, etc. Instead of compiler options being executed at command line we can use these compiler options in our program. This can be achieved using #pragma options. We can use various flags with #pragma options to use the compiler options. All these flags are available in turbo C's online help.-------------------------------------------------------------------------------------------------67.
I have an array declared in file 'F1.C' as,int a[ ] = { 1, 2, 3, 4, 5, 6 } ;and used in the file 'F2.C' as,extern int a[ ] ;
In the file F2.C, why sizeof doesn't work on the array a[ ]?
Ans: An extern array of unspecified size is an incomplete type. You cannot apply sizeof to it, because sizeof operates during compile time and it is unable to learn the size of an array that is defined in another file. You have three ways to resolve this problem: 1. In file 'F1.C' define as, int a[ ] = { 1, 2, 3, 4, 5, 6 } ;int size_a = sizeof ( a ) ;and in file F2.C declare as,extern int a[ ] ;extern int size_a ;2. In file 'F1.H' define, #define ARR_SIZ 6In file F1.C declare as,#include "F1.H"int a[ ARR_SIZ ] ;and in file F2.C declare as,#include "F1.H"extern int a[ ARR_SIZ ] ;3. In file 'F1.C' define as, int a[ ] = { 1, 2, 3, 4, 5, 6, -1 } ;and in file 'F2.C' declare as,extern int a[ ] ;Here the element -1 is used as a sentinel value, so the code can understand the end without any explicit size.
68. How to delete a line from text displayed on the screen?Ans: Sometimes, specially when we are creating a text editor like program we may wish to allow user to delete a line. We can do so by using two functions namely clreol( ) and delline( ). The clreol( ) function deletes the line from the current cursor position to the end of line. The delline() function deletes the entire line at the current cursor position and moves up the following line. Following program shows how to use these functions.#include main( ){int i ;for ( i = 1 ; i <= 20 ; i++ )printf ( "This is Line %d\n", i ) ;getch( ) ;gotoxy ( 1, 7 ) ;clreol( ) ;getch( ) ;gotoxy ( 1, 12 ) ;delline( ) ;getch( ) ;}-------------------------------------------------------------------------------------------------69. How do I programmatically insert lines in the text window?Ans: We can insert a blank line in the text window using the insline( ) function. This function inserts line at current cursor position. While doing so, it shifts down the lines that are below the newly inserted line.#include void main( ){printf ( "The little snail was slowly moving up. She wanted\r\n" ) ;printf ( "to reach the top of the tree. It was chilly\r\n" ) ;printf ( "winter season. Most of the animals were resting in\r\n" ) ;printf ( "their nests as there was a heavy snow fall.\r\n" ) ;printf ( "\r\nPress any key to continue:" ) ;gotoxy ( 10, 2 ) ;getch( ) ;insline( ) ;getch( ) ;} -------------------------------------------------------------------------------------------------
70.
What will be the output of the following program?main( ){unsigned int num ;int i ;printf ( "\nEnter any number" ) ;scanf ( "%u", &num ) ;for ( i = 0 ; i < 16 ; i++ )printf ( "%d", ( num << i & 1 << 15 ) ? 1 : 0 ) ;}Ans: The output of this program is the binary equivalent of the given number. We have used bitwise operators to get the binary number.-------------------------------------------------------------------------------------------------71. Graphics
Building Mouse Cursors...
In text mode the mouse cursor appears as a block, whereas in graphics mode it appears as an arrow. If we wish we can change the graphics cursor to any other shape the way Windows does. The mouse cursor in graphics mode occupies a 16 by 16 pixel box. By highlighting or dehighlighting some of the pixels in this box we can get the desired shape. For example, the following bit-pattern can be used to generate the cursor which looks like an hour-glass. 1111111111111111 0000000000000000 1000000000000001 0000000000000000 1111111111111111 0000000000000000 1000000000000001 0000000000000000 0100000000000010 1000000000000001 0010000000000100 1100000000000011 0000100000010000 1111000000001111 0000001001000000 1111110000111111 0000001001000000 1111110000111111 0000100000010000 1111000000001111 0010000000000100 1100000000000011 0100000000000010 1000000000000001 1000000000000001 0000000000000000 1111111111111111 0000000000000000 1000000000000001 0000000000000000 1111111111111111 0000000000000000 Mouse pointer bitmap Screen Mask the one's in the mouse pointer bitmap indicate that the pixel would be drawn whereas the zeros indicate that the pixel would stand erased. It is important to note that the mouse pointer bit pattern is 32 bytes long. However, while actually writing a program to change the pointer shape we need a 64 byte bit-map. This provision is made to ensure that when the cursor reaches a position on the screen where something is already written or drawn only that portion should get overwritten which is to be occupied by the mouse cursor. Of the 64 bytes the first 32 bytes contain a bit mask which is first ANDed with the screen image, and then the second 32 bytes bit mask is XORed with the screen image.
The following program changes the mouse cursor in graphics mode to resemble an hour glass.
# include "graphics.h"# include "dos.h"
union REGS i, o ;struct SREGS s ;
int cursor[32] = { /* Hour-glass screen mask */0x0000, 0x0000, 0x0000, 0x0000,0x8001, 0xc003, 0xf00f, 0xfc3f,0xfc3f, 0xf00f, 0xc003, 0x8001,0x0000, 0x0000, 0x0000, 0x0000,/* The mouse pointer bitmap */0xffff, 0x8001, 0xffff, 0x8001,0x4002, 0x2004, 0x1008, 0x0240,0x0240, 0x0810, 0x2004, 0x4002,0x8001, 0xffff, 0x8001, 0xffff, } ;main( ){ int gd = DETECT, gm ;initgraph ( &gd, &gm, "c:\\tc\\bgi" ) ;if ( initmouse( ) == -1 ){ closegraph( ) ;printf ( "\n Mouse not installed!" ) ; exit( ) ; } gotoxy ( 10, 1 ) ; printf ( "Press any key to exit..." ) ;changecursor ( cursor ) ; showmouseptr( ) ;getch( ) ; }
initmouse( ){ i.x.ax = 0 ; int86 ( 0x33, &i, &o ) ;return ( o.x.ax == 0 ? -1 : 0 ) ; }showmouseptr( ){ i.x.ax = 1 ; int86 ( 0x33, &i, &o ) ; }changecursor ( int *shape ){ i.x.ax = 9 ; /* service number */i.x.bx = 0 ; /* actual cursor position from left */i.x.cx = 0 ; /* actual cursor position from top */i.x.dx = ( unsigned ) shape ; /* offset address of pointer image*/segread ( &s ) ; s.es = s.ds ; /* segment address of pointer */int86x ( 0x33, &i, &i, &s ) ; }
72.
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 BMove from A to CMove from B to CMove from A to BMove from C to AMove from C to BMove from A to BMove from A to CMove from B to CMove from B to AMove from C to AMove from B to CMove from A to BMove from A to CMove from B to CThis 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.-------------------------------------------------------------------------------------------------

No comments: