Sei sulla pagina 1di 38

1.

What would be the output of the following:


main( )
{
int a[10] ;
printf ( "%u %u", a, &a ) ;
}
Ans.
If the base address of the array is 65504 then both a and &a will output :
65504 65504
Since, in C the base address of an array is pointed to by the array name itself,
so on printing the array name a the base address will be printed. Also when we
&a will give the address of the variable a( i.e. 65504 ), but a is an array
variable, so again the same address is got printed.
-------------------------------------------------------------------------------2.Point out the error, if any in the following program.
main( )
{
extern int i ;
i = 20 ;
printf ( "%d", sizeof ( i ) ) ;
}
Ans.
Linker Error : Undefined Symbol i.
By 'extern int i' we mean that the variable i has been declared before
the main( ) function or in some other file. Here i is not declared
before main( ) function, therefore it is assumed that i has been defined
in some other file. Since the file is not included by #include, hence the
Linker Error.
-------------------------------------------------------------------------------3.Point out the error in the following program. How would you avoid it?
main( )
{
struct emp
{
char n[20] ;
float s ;
};
struct emp e[10] ;

int i ;
printf ( "Enter the names and salaries of employees\n" ) ;
for ( i = 0 ; i <= 9 ; i++ )
scanf ( "%s%f", e[i].n, &e[i].s ) ;
}
Ans.
No Compile time error.
On Execution,scanf : floating point formats not linked
Abnormal program termination
This error occurs because the floating point emulator is not initialized and
generally happens when we access the float member of the structure before
any float variable.
To avoid this error initialize the floating point emulator by the following
code fragment :
extern void _floatconvert( );
#pragma extref _floatconvert
-------------------------------------------------------------------------------4.What would be the output of the following:
main( )
{
int a[3][4] = { 1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0 } ;
printf ( "\n %u %u", a + 1, &a + 1 ) ;
}
Ans.
If the base address of array a is 65000 then the output will be
65008 65024
The Address Calculation of a two dimensional array is as follows :
base + index1 * dim2 * sizeof ( type ) + index2 * sizeof ( type )
a + 1 is same as &( a + 1 ) and interpreted as
65000 + 1 * 4 * 2 + 0 i.e 65008 in this case.
&a + 1 is same as (&a) + 1 and interpreted as
base address of the two dimensional array + 1 * sizeof ( a ) , which is
65000 + 1 * 24 ( rows * cols * sizeof ( type ) ) which evaluates to be 65024
-------------------------------------------------------------------------------5.Consider the following program segment.
main( )

{
int a = 5, b = 10 ;
P(a);
P(b);
}
Define the macro P such that the output of the program comes out to be
a=5
b = 10
Ans.
#define P( x ) printf( #x " = %d\n", x ) ;
-------------------------------------------------------------------------------6.Suppose you have written two functions f( ) and g( ). How would you add them
to the standard library?
Ans.
Suppose the two functions are written in a file 'myfunc.c'. Compile this
file to create its object file (.OBJ). On the command line execute the
following commands to add the function f( ) and g( ) into a library file
'mylib.lib':
tlib mylib.lib + myfunc.obj, mylib.lst
-------------------------------------------------------------------------------7. Suppose a function f( ) has been defined in file 'file1.h'. If we #include
this file twice a redefinition of function would occur. How would you avoid it?
Ans.
To avoid the redefiniation of the function, write the following code
at the beginning of the file.
#if(!(defined __file_h))
#define __file_h
/* function definitions */
#endif
-------------------------------------------------------------------------------8. Suppose your executable (.EXE ) files have been deleted accidently and if you
want to copy all executable files to the specified directory, you have to
recompile all the files. This process would be cumbersome one. How would you
compile all the files in a single pass.
Ans.
The Solution is to use the command-line compiler (TCC) to compile to .obj and
generate the .exe file. This file is generally found in C:\TC\BIN directory. Run the
TCC from the command prompt, a list of various options will be displayed.

Among the various options available use the following command to convert .c
files
to create its .obj and .exe files.
c:\tc\bin\tcc -ms -Lc:\tc\lib -Ic:\tc\include file.c
where
-ms
- memory model
-Lc:\tc\lib - Option to search library files
-Ic:\tc\include - Option to search include files
file.c
- a C file
To compile all the C files we have to rerun this command with different filnames
'file.c' . To avoid this write the filenames along with the path (if any) in a data file
and read this data file for the filenames and run the Turbo Command Compiler
(TCC) command as above repeatedly to convert into .exe files.
The following program accomplishes the above said procedure. Compile the
program and execute it from the command prompt:
#include <stdlib.h>
#include <stdio.h>
struct file
{
char fname[20] ;
};
int main ( void )
{
char fname[20], command[127], command1[] =
{ "c:\\tc\\bin\\tcc -ms -Lc:\\tc\\lib -Ic:\\tc\\include " } ;
FILE *fp ;
struct file s ;
fp = fopen ( "filelist.dat", "wb+" ) ;
if ( fp == NULL )
{
puts ( "\nUnable to Open the file" ) ;
exit( 1 ) ;
}
dump_files( fp ) ;
strcpy ( command, command1 ) ;

printf("About to spawn command interpreter and run a DOS command\n");


rewind ( fp ) ;
while ( fread ( &s,sizeof(s),1,fp ) )
{
strcat( command, s.fname ) ;
printf ( "\n%s\n",command ) ;
getch( ) ;
system( command);
getch ( ) ;
clrscr( ) ;
strcpy ( command, command1 ) ;
}
fclose ( fp ) ;
return 0;
}
dump_files ( FILE * fp )
{
struct file s ;
char ch ;
do
{
puts ( "\nEnter a File : " ) ;
gets ( s.fname ) ;
fwrite ( &s,sizeof(s),1,fp ) ;
puts ( "\nAny More Files ( Y/N ) : " ) ;
ch = getche( ) ;
}while ( ch == 'y' || ch == 'Y' ) ;
}
-------------------------------------------------------------------------------9. There is a function f( ). Sometimes we have to pass 4 arguments to this
function and sometimes else 7 arguments. If the purpose of the function f( )
is to only print all the arguments that it receives, how would you write
function f( ) ?
Ans.
# include "stdarg.h"
main( )
{
clrscr( ) ;
f ( 4, 2, 3, 5, 10 ) ;
f ( 7, 21, 13, 15, 110, 36, 226, 291 ) ;
}

f( int c )
{
int i, num ;
va_list p ;
va_start ( p, c ) ;
printf ( "\n %d", c ) ;
for ( i = 0 ; i < c ; i++ )
{
num = va_arg ( p, int ) ;
printf ( " %d", num ) ;
}
}
-------------------------------------------------------------------------------10. Write a program to reverse a given linked list.
Ans.
#include "alloc.h"
struct node
{
int data ;
struct node *link ;
};
reverse ( struct node ** ) ;
main( )
{
struct node *p ;
p = NULL ; /* empty linked list */
addatbeg ( &p, 1 ) ;
addatbeg ( &p, 2 ) ;
addatbeg ( &p, 3 ) ;
addatbeg ( &p, 4 ) ;
addatbeg ( &p, 5 ) ;
addatbeg ( &p, 6 ) ;
clrscr( ) ;
display ( p ) ;
printf ( "\nNo. of elements in the linked list = %d", count ( p ) ) ;

reverse ( &p ) ;
display ( p ) ;
printf ( "\nNo. of elements in the linked list = %d", count ( p ) ) ;
}
addatbeg ( struct node **q, int num )
{
struct node *temp ;
/* add new node */
temp = malloc ( sizeof ( struct node ) ) ;
temp -> data = num ;
temp -> link = *q ;
*q = temp ;
}
reverse ( struct node **x )
{
struct node *q, *r, *s ;
q = *x ;
r = NULL ;
/* traverse the enetire linked list */
while ( q != NULL )
{
s=r;
r=q;
q = q -> link ;
r -> link = s ;
}
*x = r ;
}
/* displays the contents of the linked list */
display ( struct node *q )
{
printf ( "\n" ) ;
/* traverse the entire linked list */
while ( q != NULL )
{

printf ( " %d", q -> data ) ;


q = q -> link ;
}
}
/* counts the number of nodes present in the linked list */
count ( struct node *q )
{
int c = 0 ;
/* traverse the entire linked list */
while ( q != NULL )
{
q = q -> link ;
c++ ;
}
return c ;
}
-------------------------------------------------------------------------------11. Suppose a file 'emp.dat' consists of 10 dates. Write a program to sort and
print the sorted dates on the screen.
Ans.
/* This program creates data file */
#include "stdio.h"
#pragma extref _floatconvert
extern void _floatconvert( );
struct date
{
int dd, mm, yy ;
};
struct emp
{
char name[10] ;
struct date jd ;
float salary ;
};

main( )
{
int i ;
struct emp e ;
FILE * fp ;
fp = fopen ( "emp.dat", "wb+" ) ;
if ( fp == NULL )
{
printf ( "Unable to open" ) ;
exit( ) ;
}
for ( i = 0 ; i < 10 ; i++ )
{
scanf ( "%s%d%d%d%f", e.name, &e.jd.dd, &e.jd.mm,
&e.jd.yy, &e.salary ) ;
fwrite ( &e, sizeof ( e ), 1, fp ) ;
}
fclose ( fp ) ;
}
/* This program sorts and displays data file */
#include "stdio.h"
struct date
{
int dd, mm, yy ;
};
struct emp
{
char name[10] ;
struct date jd ;
float salary ;
};
main( )
{
FILE *fp ;
struct emp e1, e2 ;
int i, j ,n = 10 ;
long pos ;
int fcmp_dates ( struct date *d1, struct date *d2 ) ;

clrscr( ) ;
fp = fopen ( "c:\\tc\\bin\\emp.dat", "rb+" ) ;
if ( fp == NULL )
{
printf ( "Unable to open" ) ;
exit( ) ;
}
for ( i = 0 ; i < 9 ; i++,n-- )
{
fread ( &e1, sizeof(e1), 1, fp ) ;
for ( j = 1 ; j < n ; j++ )
{
fseek ( fp, 0L, SEEK_CUR ) ;
fread ( &e2, sizeof(e2), 1, fp ) ;
if( fcmp_dates ( &e1.jd, &e2.jd ) > 0 )
{
fseek ( fp, -2L * sizeof( struct emp ), SEEK_CUR ) ;
fwrite ( &e2, sizeof ( e2 ), 1, fp ) ;
fwrite ( &e1, sizeof ( e1 ), 1, fp ) ;
}
else
e1 = e2 ;
}
rewind ( fp ) ;
}
rewind ( fp ) ;
printf ( "\nThe Sorted List is : \n" ) ;
while ( fread ( &e1, sizeof( e1 ), 1, fp ) )
printf ( "\n%s\t %d/%d/%d \t%7.2f", e1.name, e1.jd.dd, e1.jd.mm, e1.jd.yy,
e1.salary ) ;
fclose ( fp ) ;
}
fcmp_dates ( struct date *d1, struct date *d2 )
{
int x, y ;
x = ( d1->yy - 1980 ) * 512 + d1->mm * 32 + d1->dd ;
y = ( d2->yy - 1980 ) * 512 + d2->mm * 32 + d2->dd ;
return ( x - y ) ;

}
-------------------------------------------------------------------------------12. All possible combinations of 1, 2, 3 are 1 2 3, 1 3 2, ... These turn out to be 3!
combinations.
If we take 4 numbers, there would be 4! combinations. If we take n numbers, there would
be n combinations. If n is received from keyboard, write a program to generate and
print these combinations.
Ans.
#include "stdio.h"
#define MAX 10
main( )
{
int arr[MAX], begin[MAX], end[MAX], lastsub ;
unsigned long int n, i ;
printf ( "Enter any number" ) ;
scanf ( "%lu", &n ) ;
lastsub = n - 1 ;
for ( i = 0 ; i <= lastsub ; i++ )
{
begin[i] = 1 ;
end[i] = n ;
}
clrscr( ) ;
/* Explicitly nested for statements. The number of for's
would change depending upon the value of n */
for ( arr[0] = begin[0] ; arr[0] <= end [0] ; arr[0]++ )
{
for ( arr[1] = begin[1] ; arr[1] <= end[1] ; arr[1]++ )
{
for ( arr[2] = begin[2] ; arr[2] <= end [2] ; arr[2]++ )
work ( arr, lastsub ) ;
}
}
getch( ) ;
printf ( "\n\n" ) ;
/* Recursively nested for statements */
recfor ( arr, begin, end, 0, lastsub ) ;

}
recfor ( int *arr, int *bval, int *eval, int p, int lastsub )
{
arr[p] = bval[p] ;
while ( arr[p] <= eval[p] )
{
if ( p == lastsub )
work ( arr, lastsub ) ;
else
recfor ( arr, bval, eval, ( p + 1 ), lastsub ) ;
arr[p]++ ;
}
}
work ( int *arr, int lastsub )
{
int p, j, num ;
for ( p = 0 ; p <= lastsub ; p++ )
{
num = arr[p] ;
for ( j = p + 1 ; j <= lastsub ; j++ )
{
if ( num == arr[j] )
return ;
}
}
for ( p = 0 ; p <= lastsub ; p++ )
printf ( "arr[%d] = %d ", p, arr[p] ) ;
printf ( "\n" ) ;
getch( ) ;
}
-------------------------------------------------------------------------------13. Write a program to add two matrices of dimensions 3 x 4. Each element of the
matrices are complex numbers.
Ans.
#define ROW 3
#define COL 4
struct complex

{
double re ;
double im ;
};
main( )
{
struct complex a[ROW][COL] = {
10.9, 2.6, 5.3, 29.0, 3.8,4.8,
5.4, 6.5, 7.4, 8.6, 7.33, 2.9,
11.9, 12.6, 15.3, 129.0, 13.8,14.8,
51.4, 61.5, 17.4, 18.6, 71.33, 1
};
struct complex b[ROW][COL] = {
10.9, 29.0, 3.8,4.8, 4.5, 23.36,
5.4, 6.5,7.4, 8.6, 4.5, 7,98,
11.9, 21.6, 15.3, 12.0, 31.8,14.8,
15.4, 61.5, 71.4, 18.6, 71.33, 21.9
};
struct complex c[ROW][COL] ;
int i, j ;
clrscr( ) ;
for ( i = 0 ; i < ROW ;i++ )
{
for ( j = 0 ; j < COL ; j++ )
{
c[i][j].re = a[i][j].re + b[i][j].re ;
c[i][j].im = a[i][j].im + b[i][j].im ;
}
}
for ( i = 0 ; i < ROW ;i++ )
{
for ( j = 0 ; j < COL ; j++ )
{
printf (" %lf ", c[i][j].re ) ;
printf (" %lf ", c[i][j].im ) ;
}
printf ("\n" ) ;
}
getch( ) ;
}

-------------------------------------------------------------------------------14. Write a program to generate 10 different random numbers?


Ans.
#include "dos.h"
main( )
{
unsigned int i ;
for ( i = 0 ; i < 10 ; i++ )
printf ( "%d ", time( ) * i % 255 ) ;
getch( ) ;
}
time( )
{
union REGS i, o ;
unsigned int t ;
i.h.ah = 0x00 ;
int86 ( 0x1A, &i, &o ) ;
t = i.x.cx ;
}
-------------------------------------------------------------------------------15. Write a program to arrange the queens on a 8 x 8 chess board so that no one
can take the other and print the arrangements of possible combinations.
Ans.
int v,i,j,k,l,s,a[99];main( ){
for(scanf("%d",&s);*a-s;v=a[j*=v]-a[i],k=i<s,j+=(v=j<
s&&(!k&&!!printf(2+"\n\n%c"-(!l<<!j)," #Q"[l^v?(l^j)&1:2])&&
++l||a[i]<s&&v&&v-i+j&&v+i-j))&&!(l%=s),v||(i==j?a[i+=k]=0:++
a[i])>=s*k&&++a[--i]);}
--------------------------------------------------------------------------------

16. Write a program 'EVALUATE.C' to accept an arithmetic expression as string on the


command line and find the value of the expression.
For example
EVALUATE 3 + 5 * 2 should output 13.
Ans.
main( int argc, char *argv[] )
{
int i, j, r = 0, r1 = 0, result = 0, top1 = 0, top2 = 0, opn[20] ;
char e[20], opr[20], temp[20] ;
if ( argc != 2 )
{
printf ( "\nInsufficient Arguments" ) ;
exit( ) ;
}
for ( i = 0 ; *( argv[1] +i ) != '\0' ; i++ )
{
if ( *( argv[1] + i ) > '0' && *( argv[1] + i) < '9' )
opn[top1++] = * ( argv[1] + i ) - '0' ;
else
opr[top2++] = *( argv[1] +i ) ;
}
while ( top1 > 0 && top2 > 0 )
{
switch ( opr[--top2] )
{
case '+' :
r += opn[--top1] ;
if ( opr[top2-1] == '*' )
{
top2-- ;
r1 += opn[--top1] ;
r1 *= opn[--top1] ;
}
else if ( opr[top2-1] == '/' )
{
top2-- ;
r1 += opn[--top1] ;
r1 *= opn[--top1] ;
}
r += r1 ;
result += r ;

r = r1 = 0 ;
break ;
case '-' :
r += opn[--top1] ;
if ( opr[top2-1] == '*' )
{
top2-- ;
r1 += opn[--top1] ;
r1 *= opn[--top1] ;
}
else if ( opr[top2-1] == '/' )
{
top2-- ;
r1 += opn[--top1] ;
r1 *= opn[--top1] ;
}
r -= r1 ;
result += r ;
r = r1 = 0 ;
break ;
case '*' :
r += opn[--top1] ;
r *= opn[--top1] ;
result += r ;
r=0;
break ;
case '/' :
r += opn[--top1] ;
r /= opn[--top1] ;
result += r ;
r=0;
break ;
}
}
printf ( "%d", result ) ;
}
-------------------------------------------------------------------------------17. A magic square is an n x n matrix, where each of the integers 1, 2, 3, ..., n2,
appear exactly once and the sums of every row, columnn and diagonal are equal.
It has been proved that for even values of n, no magic square exits. Here is a
magic square for n = 3:
|8|1|6|
|3|5|7|

|4|9|2|
The following is a procedure for constructing an n x n magic square for any
odd integer n.
Place initial number in the middle of the top row. Then afterr integer k has
been placed, move up one row and one column to the right to place the next
integer k + 1, unless one of the following occurs :
a)
If a move takes you above the top row in the jth column, move to the
bottom of the jth column and place the integer there.
b)
If a move takes you outside to the right of the square in the ith row,
place the integer in the ith row at the left side.
c)
If a move takes you to an already filled square or if you move out of the
square at the upper-right-hand corner, place k + 1 immediately below k.
Write a program to construct a magic square for any odd integer n.
Ans.
#include "conio.h"
int a[81] ;
main( )
{
int sr = 5, sc = 30, er, ec ;
int n, num, row, col, i, j, l, k, sum = 0 ;
printf ( "Enter the size of odd square matrix" ) ;
scanf ( "%d", &n ) ;
if ( !(n % 2) )
{
puts ( "Not an Odd number\n" ) ;
exit( ) ;
}
printf ( "Enter initial number of the series" ) ;
scanf ( "%d", &num ) ;
clrscr ( ) ;
gotoxy ( 35, 1 ) ;
er = sr + n * 2 ; /* ending row */
ec = sc + n * 3 ; /* ending column */

drawbox ( sr, sc, er, ec ) ;


i = 1 ; /* top row */
j = 1 + n / 2 ; /* middle of top row */
for ( k = 0 ; k < n * n ; k++ )
{
/* find row & col wherenumber is to be placed on screen */
row = ( sr + 1 ) + ( i - 1 ) * 2 ;
col = ( sc + 1 ) + ( j - 1 ) * 3 ;
gotoxy ( col, row ) ;
printf ( "%d", num ) ;
/* calculate position where number is to be place in array */
l=(i-1)*n+(j-1);
if ( a[l] == 0 )
a[l] = num ;
num++ ;
/* go one row up */
i-- ;
/* go one column to right */
j++ ;
/* check if we reach outside the square */
if ( i == 0 )
i=n;
if ( i == n + 1 )
i=1;
if ( j == 0 )
j=n;
if ( j == n + 1 )
j=1;
l=(i-1)*n+(j-1);
/* if square already filled */
if ( a[l] != 0 )
{
/* increment row by 2, decrement col by 1 */
i += 2 ;
j-- ;
/* adjust row and col if outside the box */
if ( i > n )
i=i-n;

/* adjust row and col if outside the box */


if ( j == 0 )
j=n;
}
}
for ( i = 0 ; i <= ( n - 1 ) ; i++ )
sum = sum + a[i] ;
gotoxy ( sc, er + 2 ) ;
printf ( "Horizontal, Vertical & Diagonal sum = %d", sum ) ;
}
/* draw squares into which numbers are to be displayed */
drawbox ( sr, sc, er, ec )
int sr, sc, er, ec ;
{
int i, j ;
for ( j = sr ; j <= er ; j += 2 )
{
for ( i = sc + 1 ; i < ec ; i++ )
{
gotoxy ( i, j ) ;
printf ( "%c", 196 ) ;
}
}
for ( i = sc ; i <= ec ; i += 3 )
{
for ( j = sr + 1 ; j < er ; j++ )
{
gotoxy ( i, j ) ;
printf ( "%c", 179 ) ;
}
}
for ( i = sc + 3 ; i < ec ; i += 3 )
{
gotoxy ( i, sr ) ;
printf ( "%c", 194 ) ;
gotoxy ( i, er ) ;
printf ( "%c", 193 ) ;
}

gotoxy ( sc, sr ) ;
printf ( "%c", 218 ) ;
gotoxy ( ec, er ) ;
printf ( "%c", 217 ) ;
gotoxy ( ec, sr ) ;
printf ( "%c", 191 ) ;
gotoxy ( sc, er ) ;
printf ( "%c", 192 ) ;
}
-------------------------------------------------------------------------------19. Accept two dates and the day of the first date from the keyboard. Write a
program to determine and print the day of the other date.
Ans.
main( )
{
struct date
{
int dd, mm, yy ;
} d1, d2, d3, d4 ;
int i, d, flag = 0, l = 0, diff ;
char ch, day[4], *days[] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" } ;
printf ( "\nEnter today's date as dd-mm-yyyy : " ) ;
scanf ( "%d%*[-]%d%*[-]%d", &d1.dd, &d1.mm, &d1.yy ) ;
printf ( "\nEnter the day of the date ( first three letters ) : " ) ;
scanf ( "%s", day ) ;
d3 = d1 ;
for ( i = 0 ; i < 7 ; i++ )
{
if ( !strcmp ( day , days[i] ) )
{
d=i+1;
flag = 1 ;
break ;
}
}
if ( !flag )
printf ( "Invalid Day" ), exit( ) ;

do
{
printf ( "\nEnter the date, whose day is unknown as dd-mm-yyyy : " ) ;
scanf ( "%d%*[-]%d%*[-]%d", &d2.dd, &d2.mm, &d2.yy ) ;
printf ( "\nThe day is " ) ;
d=i+1;
diff = l = 0 ;
d1 = d3 ;
d4 = d2 ;
if ( d2.yy > d1.yy )
{
if ( d2.dd > d1.dd )
{
while ( d1.dd != d2.dd )
{
d1.dd++ ;
d++ ;
if ( d > 7 )
d -= 7 ;
}
}
else
{
while ( d1.dd != d2.dd )
{
d1.dd-- ;
d-- ;
if ( d <= 0 )
d += 7 ;
}
}
if ( d2.mm > d1.mm )
{
while ( d1.mm != --d2.mm )
{
switch ( d1.mm )
{
case 1 :
case 3 :
case 5 :
case 7 :

case 8 :
case 10 :
case 12 :
d += 3 ;
if ( d > 7 )
d -= 7 ;
break ;
case 4 :
case 6 :
case 9 :
case 11 :
d += 2 ;
if ( d > 7 )
d -= 7 ;
break ;
}
if ( d1.mm == 2 && leap ( d1.yy ) )
{
d++ ;
if ( d > 7 )
d -= 7 ;
d1.mm++ ;
}
}
}
else
{
while ( d1.mm != d2.mm )
{
switch ( --d1.mm )
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
d -= 3 ;
if ( d <= 0 )
d += 7 ;
break ;
case 4 :
case 6 :

case 9 :
case 11 :
d -= 2 ;
if ( d <= 0 )
d += 7 ;
break ;
}
if ( d1.mm == 2 && leap ( d1.yy ) )
{
d-- ;
if ( d <= 0 )
d += 7 ;
}
}
}
diff = d2.yy - d1.yy ;
if ( diff > 0 && d2.mm > 2 && leap ( d1.yy ) )
l--, d1.yy += 4 ;
else
{
if ( leap ( d1.yy ) && d4.mm < 2 )
l++ ;
while ( !leap ( d1.yy++ ) ) ;
d1.yy-- ;
}
if ( diff )
while ( d1.yy <= d2.yy )
l++ , d1.yy += 4 ;
if ( d4.mm < 2 && leap ( d4.yy ) )
l-- ;
diff += l ;
d += diff ;
while ( d >= 7 )
d -= 7 ;
if ( d <= 0 )
d += 7 ;
}
else
{

if ( d2.dd > d1.dd )


{
while ( d1.dd != d2.dd )
{
d1.dd++ ;
d++ ;
if ( d > 7 )
d -= 7 ;
}
}
else
{
while ( d1.dd != d2.dd )
{
d1.dd-- ;
d-- ;
if ( d <= 0 )
d += 7 ;
}
}
if ( d2.mm > d1.mm )
{
while ( d1.mm != d2.mm )
{
switch ( d1.mm )
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
d += 3 ;
if ( d > 7 )
d -= 7 ;
break ;
case 4 :
case 6 :
case 9 :
case 11 :
d += 2 ;
if ( d > 7 )
d -= 7 ;
break ;

}
if ( d1.mm == 2 && leap ( d1.yy ) )
{
d++ ;
if ( d > 7 )
d -= 7 ;
}
d1.mm++ ;
}
}
else
{
while ( d1.mm != d2.mm )
{
switch ( --d1.mm )
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
d -= 3 ;
if ( d <= 0 )
d += 7 ;
break ;
case 4 :
case 6 :
case 9 :
case 11 :
d -= 2 ;
if ( d <= 0 )
d += 7 ;
break ;
}
if ( d1.mm == 2 && leap ( d1.yy ) )
{
d-- ;
if ( d <= 0 )
d += 7 ;
}
}

}
diff = d1.yy - d2.yy ;
if ( diff > 0 && d4.mm > 2 && leap ( d1.yy ) )
l++, d1.yy -= 4 ;
else
{
if ( leap ( d1.yy ) && d4.mm < 2 )
l-- ;
if ( diff )
while ( !leap ( d1.yy++ ) ) ;
d1.yy-- ;
}
if ( diff )
while ( d2.yy <= d1.yy )
l++ , d1.yy -= 4 ;
if ( diff && d4.mm > 2 && leap ( d4.yy ) )
l-- ;
if ( diff )
{
diff += l ;
d -= diff ;
}
while ( d <= 0 )
d += 7 ;
}
printf ( "%sday", days[d-1] ) ;
printf ( "\nAny More Queries ( Y/N ) : " ) ;
ch = getche( ) ;
}while ( ch == 'y' || ch == 'Y' ) ;
}
leap ( int y )
{
return ( y % 4 == 0 && y % 100 != 0 || y % 400 == 0 ) ;
}
20. Write a program which converts the numeric figures into the words.
e.g. 123 should be printed as , One Hundred Twenty Three.

Ans.
# include <ctype.h>
# include <stdio.h>
char *ot[3][9] = {
{ " One", " Two", " Three", " Four", " Five",
" Six", " Seven", " Eight", " Nine" },
{ " Ten", " Twenty", " Thirty", " Forty", " Fifty",
" Sixty", " Seventy", " Eighty", " Ninety" },
{ " Eleven", " Twelve", " Thirteen", " Fourteen",
" Fifteen"," Sixteen"," Seventeen"," Eighteen", "
Nineteen"}
};
char *a[5] = { " Hundred", " Thousand", " Lakhs", " Crore", " Arab" } ;
char result[250] = "" ;
char *t[50] ;
main( )
{
int i, j, ind = 0, c, r, pr = -1, e = 0 ;
unsigned long n ;
unsigned long q ;
clrscr( ) ;
printf ( "\nEnter a long unsigned number : " ) ;
scanf ( "%ld", &n ) ;
printf ( "%ld\n", n ) ;
q=n;
if ( n == 0 )
strcpy ( result, "Zero" ) ;
else
{
for( i = 0 ; q > 0 ; i++ )
{
if( ( i % 2 ) && i > 2 )
e++ ;
r = q % 10 ;
q /= 10 ;
if ( r != 0 )
{
if ( i == 0 )
t[++ind] = ot[i][r-1];
else if ( i == 1 )

{
if ( r == 1 && pr == 0 )
t[++ind] = ot[i][r-1] ;
else if ( r == 1 && pr > 0 )
t[--ind] = ot[2][r-1] ;
else
t[++ind] = ot[i][r-1] ;
}
else if ( i >= 2 )
{
if ( i == 2 )
{
t[++ind] = a[e] ;
t[++ind] = ot[0][r-1] ;
}
else
{
if ( i % 2 )
{
t[++ind] = a[e] ;
t[++ind] = ot[0][r-1] ;
}
else
{
if ( pr == 0 )
{
t[++ind] = a[e] ;
t[++ind] = ot[1][r-1] ;
}
else if ( r == 1)
t[ind] = ot[2][pr-1] ;
else
t[++ind] = ot[1][r-1] ;
}
}
}
}
pr = r ;
}
}/* End for */
for ( i = ind ; i >= 0 ; i-- )
strcat ( result, t[i] ) ;
printf( "\n%s", result ) ;

} /* End main */
-------------------------------------------------------------------------------21. Pascal triangle's elements are the Binomial coefficients and can be calculated
from the formula :
n!/((n-r)!! * r! ).
Write a program to print the pascal's triangle without using the formula or
arrays.
Ans.
main( )
{
int n, l, m = 1, f = 1, v, i, k, w, j ;
printf ( "Enter the number of rows required for pascal triangle " ) ;
scanf ( "%d", &n ) ;
printf( "%d\n", n ) ;
for( i = 1 ; i <= n ; i++ )
{
w = 5 + ( ( n-1 ) * 3 ) - ( ( i - 1 ) * 3 ) , m = 1 , f = 1 ;
for ( j = 1 ; j <= ( ( i / 2 ) + ( i % 2 ) ) ; j++ )
{
if ( i == 1 )
{
printf ( "%*d", w, j ) ;
break ;
}
else if( j == 1)
printf ( "%*d", w, j ), w = 6 ;
else
{
m = 1, f = 1 ;
for ( k = 1 ; k <= j-1 ; k++ )
{
m *= ( i - k ) ;
f *= k ;
}
printf ( "%*d", w, ( m / f ) ) ;
}
}
for( j = i / 2 ; j >= 1 ; j-- )
{
if ( i == 1)
break ;
else if ( j == 1 )

{
printf ( "%*d", w, j ) ;
break ;
}
else
{
m = 1, f = 1 ;
for ( k = j - 1 ; k >= 1 ; k-- )
m *= ( i - k ), f *= k ;
printf ( "%*d", w, ( m / f ) ) ;
}
} /*End of for loop*/
printf ( "\n" ) ;
} /*End of for loop*/
getch( ) ;
} /*End of main*/
-------------------------------------------------------------------------------22. Write a program to print the calendar, which is constraint to only 5 rows as :
January 2000
Sun Mon Tue Wed Thu Fri Sat
30 31
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
Ans.
static char *months[] = { "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November",
"December"
};
main( )
{
static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } ;
long int ndays, ldays, tydays, tdays ;
int d, i, m, fday, y ;
char ch ;
clrscr() ;

printf ( "Enter year(1900 onwards) & month " ) ;


scanf ( "%d %d", &y, &m ) ;
ndays = ( y - 1 ) * 365l ;
ldays = ( y - 1 ) / 4 - ( y - 1 ) / 100 + ( y - 1 ) / 400 ;
tdays = ndays + ldays ;
if ( ( y % 100 == 0 &&y % 400 == 0 ) || ( y % 4 == 0 && y % 100 != 0 ) )
days[1] = 29 ;
else
days[1] = 28 ;
d = days[m-1] ;
tydays = 0 ;
for ( i = 0 ; i <= m - 2 ; i++ )
tydays = tydays + days[i] ;
tdays = tydays + tdays ;
fday = tdays % 7 ;
clrscr( ) ;
printf("%*s %d\n" ,strlen( months[m-1] )/2 + 1, months[m-1] , y) ;
printf(" Sun Mon Tue Wed Thu Fri Sat\n" ) ;
print_cal_num ( fday + 2, days[m-1] ) ;
}
print_cal_num ( int d, int n )
{
int i, j, st, flag = 0, k = 1 ;
st = 7 - d + 1 ;
f ( st == 1 || st == 2 && n >= 30 )
flag = 1 ;
for ( i = 1 ; i < d ; i++ )
{
if ( flag && ( 29 + i ) <= n )
{
printf ( "%5d", 29 + i ) ;
}
else
printf ( "%5s", " " ) ;
}

for ( i = 1 ; i <= 5 ; i++ )


{
for ( j = d ; j <= 7 ; j++ )
{
if ( k > n )
break ;
printf ( "%5d", k++ ) ;
}
d=1;
printf ( "\n" ) ;
}
}
-------------------------------------------------------------------------------23. Write a program to find the determinant value of any given square matrix using
recursion.
Ans.
#include "stdlib.h"
#include "alloc.h"
main( )
{
int *arr, sum, n, i, j, pos, num ;
printf ( "\nEnter value of n for ( n x n ) matrix " ) ;
scanf ( "%d", &n ) ;
/* allocate memory to accomodate the determinant */
arr = calloc ( n * n, 2 ) ;
printf ( "\nEnter numbers :\n" ) ;
for ( i = 0 ; i < n ; i++ )
{
for ( j = 0 ; j < n ; j++ )
{
scanf ( "%d", &num ) ;
pos = i * n + j ;
arr[pos] = num ;
}
}
sum = detmat ( arr, n ) ;
free ( arr ) ;

printf ( "\n%d", sum ) ;


}
detmat ( int *arr, int order )
{
int sign = 1, sum = 0, i, j, k, count, *arr2 ;
int newsize, newpos, pos ;
if ( order == 1 )
return ( arr[0] ) ;
for ( i = 0 ; i < order ; i++, sign *= -1 )
{
/* copy n-1 by n-1 array into another array */
newsize = ( order - 1 ) * ( order - 1 ) ;
arr2 = calloc ( newsize, 2 ) ;
for ( j = 1 ; j < order ; j++ )
{
for ( k = 0, count = 0 ; k < order ; k++ )
{
if ( k == i )
continue ;
pos = j * order + k ;
newpos = ( j - 1 ) * ( order - 1 ) + count ;
arr2[newpos] = arr[pos] ;
count++ ;
}
}
/* find determinant value of n-1 by n-1 array and add it to sum */
sum = sum + arr[i] * sign * detmat ( arr2, order - 1 ) ;
free ( arr2 ) ;
}
return ( sum ) ;
}
-------------------------------------------------------------------------------24. Write a program to compare two binary search trees using recursion.
Ans.
/* Program to traverse a binary search tree using inorder traversal without recursion */
#include "alloc.h"

#define MAX 10
struct btreenode
{
struct btreenode *leftchild ;
int data ;
struct btreenode *rightchild ;
};
main( )
{
struct btreenode *bt1, *bt2 ;
int req, i = 1, num ;
bt1 = NULL ; /* empty tree */
bt2 = NULL ; /* empty tree */
clrscr( ) ;
printf ( "Specify the number of data to be inserted : " ) ;
scanf ( "%d", &req ) ;
while ( i++ <= req )
{
printf ( "Enter the data : " ) ;
scanf ( "%d", &num ) ;
insert ( &bt1, num ) ;
}
i=1;
while ( i++ <= req )
{
printf ( "Enter the data : " ) ;
scanf ( "%d", &num ) ;
insert ( &bt2, num ) ;
}
clrscr( ) ;
if ( inorder ( bt1, bt2 ) )
printf ( "\nBoth the Binary Search trees are Equal" ) ;
}
/* inserts a new node in a binary search tree */
insert ( struct btreenode **sr, int num )
{
if ( *sr == NULL )
{
*sr = malloc ( sizeof ( struct btreenode ) ) ;

( *sr ) -> leftchild = NULL ;


( *sr ) -> data = num ;
( *sr ) -> rightchild = NULL ;
return ;
}
else /* search the node to which new node will be attached */
{
if ( num < ( *sr ) -> data )
insert ( & ( ( *sr ) -> leftchild ), num ) ;
else
/* else traverse to right */
insert ( & ( ( *sr ) -> rightchild ), num ) ;
}
return ;
}
/* traverse a binary search tree in a LDR fashion */
inorder ( struct btreenode *currentnode1, struct btreenode *currentnode2 )
{
static int top = 0 ;
static struct btreenode *nodestack1[MAX], *nodestack2[MAX] ;
if ( currentnode1 == NULL )
return ;
else
{
top++ ;
if ( top > MAX )
{
printf ( "\n Stack is full....." ) ;
exit( ) ;
}
else
{
/* remembers the previous nodes whose right side is
yet to be traversed */
nodestack1[top] = currentnode1 ;
currentnode1 = currentnode1 -> leftchild ;
nodestack2[top] = currentnode2 ;
currentnode2 = currentnode2 -> leftchild ;
inorder ( currentnode1, currentnode2 ) ;
}
}

/* pop previous nodes one by one */


if ( top != 0 )
{
currentnode1 = nodestack1[top] ;
currentnode2 = nodestack2[top] ;
top-- ;
/* print the data field */
if ( currentnode1 -> data == currentnode2 -> data )
{
currentnode1 = currentnode1 -> rightchild ;
currentnode2 = currentnode2 -> rightchild ;
inorder ( currentnode1, currentnode2 ) ;
}
else
{
printf ( "\nBoth the Binary Search Trees are Unequal\n" ) ;
exit( ) ;
}
}
else
return 1 ;
}
-------------------------------------------------------------------------------25. Write a program to compare two linked lists using recursion.
Ans.
/* Program to compare two linked lists using recursion */
#include "alloc.h"
struct node
{
int data ;
struct node * link ;
};
main( )
{
struct node *first, *second ;
first = second = NULL ; /* empty linked lists */

append ( &first, 1 ) ;
append ( &first, 2 ) ;
append ( &first, 3 ) ;
append ( &second, 1 ) ;
append ( &second, 2 ) ;
append ( &second, 3 ) ;
clrscr( ) ;
if ( compare ( first, second ) )
printf ( "Both linked lists are EQUAL" ) ;
else
printf ( " Linked lists are DIFFERENT" ) ;
}
/* adds a node at the end of a linked list */
append ( struct node **q, int num )
{
struct node * temp ;
temp = *q ;
if ( *q == NULL ) /* if the list is empty, create first node */
{
*q = malloc ( sizeof ( struct node ) ) ;
temp = *q ;
}
else
{
/* go to last node */
while ( temp -> link != NULL )
temp = temp -> link ;
/* add node at the end */
temp -> link = malloc ( sizeof ( struct node ) ) ;
temp = temp -> link ;
}
/* assign data to the last node */
temp ->data = num ;
temp -> link = NULL ;
}
/* compares 2 linked lists and returns 1 if linked lists are equal and 0 if
unequal */
compare ( struct node *q, struct node *r )

{
static int flag ;
if ( ( q == NULL ) && ( r == NULL ) )
flag = 1 ;
else
{
if ( q == NULL || r == NULL )
flag = 0 ;
if ( q -> data != r-> data )
flag = 0 ;
else
compare ( q -> link, r -> link ) ;
}
return flag ;
}

Potrebbero piacerti anche