Sie sind auf Seite 1von 11

Appendix

+
Solved Question Papers
C Programming and Data Structures
May/June 2007

SET- 1

1. (a) Write a program to determine and print the sum of the following harmonic series
for a given value of n:1+1/2+1/3+1/4+1/5………+1/n.
#include<stdio.h>
#include<conio.h>
main()
{
int i,n;
float sum=0;
clrscr();
printf(“enter the value of n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
sum+=1/(float)i;
printf(“ the value of the given series is %f\n”,sum);
getch();
}
(b) What are the logical operators used in C ? Illustrate with examples.
Logical operators used in C language are
&& - logical AND
|| - logical OR
! - logical NOT

Appendix-C.p65 1 7/29/08, 5:21 PM


C.2 Solved Question Papers
Truth table for logical operators:

A B A&&B A||B !A
T T T T F
T F F T F
F T F T T
F F F F T

Logical AND is true only when all the inputs are true and logical OR is true if any one of
the inputs is true. Logical NOT is a unary operator, it is true when input is false and vice-
versa.
Sample program for logical operators:
#include<stdio.h>
main()
{
int a,b,c,nc=0,nw=0;
char ch;
FILE *fptr;
printf(“enter the values of a,b,c\n”);
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(b>c)) /* logical AND */
printf(“a is the largest value”);
fptr=fopen(“input.c”,”r”);
while(!foef(fptr)) /* logical NOT */
{
ch=getc(fptr);
if(ch==’ ‘||ch==’\t’) /* logical OR */
nw++;
else
nc++;
}
fclose(fptr);
printf(”number of words =%d\n”,nw);
printf(”number of characters =%d\n”,nc);
}
getch();
}
2. (a) What is a preprocessor directive?
The preprocessor is a program that processes the source program before it is passed on to
the compiler.
The program typed in the editor is the source to the preprocessor. The preprocessor direc-
tives are generally initialized at the beginning of the program. It begins with a symbol
#(hash).
Example : #define pi 3.14

Appendix-C.p65 2 7/29/08, 5:21 PM


Solved Question Papers C.3
(b) Distinguish between function and preprocessor directive.

Function Preprocessor Directive


Function is a block of statements enclosed Preprocessor directives are mostly
within braces. single line statements usually used to
define symbolic constants.
Compiler moves to the function where it Compiler replaces the code when it is
is located when a function is invoked. encountered.
Functions can accept and return values Preprocessor can neither accept nor
from and to the calling function. return values.
Example: sum(int a, int b) Example: #define pi 3.14
{ return a+b;}

(c) What is the significance of conditional compilation?


Conditional compilation allows the user to compile the portions of the program based on
certain conditions. The directives used for conditional compilation are #if, #else, #endif,
etc.
The syntax for #if directive is
#ifdef <identifier>
{
statement1;
statement2;
.
.
}
#else
{
statement x;
statement y;
.
.
}
#endif
( d) How is the unedifining of a pre-defined macro done?
A preprocessor directive that is defined using #define can be undefined using #undef direc-
tive.
Syntax : #undef identifier
It is useful when we do not want to allow the use of macros in any portion of the program.
Example:
#include<stdio.h>
#define wait getch()
main()
{
int a;
clrscr();
#undef wait getch()

Appendix-C.p65 3 7/29/08, 5:21 PM


C.4 Solved Question Papers
for(a=1;a<=10;a++)
{
printf(“%d\t”,a);
wait;/* returns an error message since wait is undefined */
}
}
3. (a) What is a pointer? How is a pointer initiated? Give an example.
A pointer is a variable which stores the address of another variable. The syntax for declar-
ing a pointer is data type * pointer_name;
Example:
int *ptr;
char *cptr;
float *p;
Once a pointer variable is declared, it can be assigned the address of a variable using as-
signment statement.
Example: int a, *ptr;
ptr=&a; ‘&’ is called address of operator. ptr is assigned the address of a.
The pointer and variable must be of the same data type.
(b) State whether each of the following statements is true or false. Give reasons.
(i) An integer can be added to a pointer.
True.
Example: ptr=ptr+1; here the pointer is incremented depending on its data type. If it is
an integer then the scale factor is 2 bytes, character 1 byte, and so on.
(ii) A pointer can never be subtracted from another pointer.
False.
Because C supports pointer arithmetic, by which a pointer can be subtracted or added
to another pointer.
(iii) When an array is passed as an argument to a function, a pointer is passed.
True. Because the array name is itself a constant pointer to that array.
(iv) Pointers cannot be used as formal parameters in headers to function
definitions.
False. C supports call by reference in which the formal parameters are pointers.
(c) If m and n have been declared as integers and p1 and p2 as pointers to integers, then
find out the errors, if any, in the following statements.
(i) p1=&m;
The given statement is correct.
(ii) p2 = n;
The given statement is an error as the compiler cannot convert int to int *.
(iii) m=p2-p1;
The given statement is correct as C supports pointer arithmetic.
(iv) *p1=&n;
The given statement is not correct as the compiler cannot convert int* to int.

Appendix-C.p65 4 7/29/08, 5:21 PM


Solved Question Papers C.5
4. (a) What are bit fields? What are its advantages? What is its syntax?
Bit fields are the special features provided in C to change the order of allocation of memory
from bytes to bits. Using bit fields we can specify the exact number of bits required for the
storage of values. The number of bits required for a variable is specified by a non-negative
integer preceded by a colon.
Advantages:
· Changes order of allocation of memory storage.
· Minimizes the memory wastage.
· Memory can be used more effectively and efficiently.
Syntax for using bit fields:
unsigned variable : size;
Example:
unsigned pass:1;
unsigned fail:0;
(b) Write a C program to store the information of vehicles, use bit fields to store the
status information. Assume the vehicle object consists of type, fuel and model mem-
ber fields. Assume appropriate number of bits for each field.
#include<stdio.h>
#include<conio.h>
#define petrol 1
#define diesel 2
#define two_wheeler 3
#define four_wheeler 4
#define old 5
#define new 6

main()
{
struct vehicle
{
unsigned type:3;
unsigned fuel:2;
unsigned model:3;
}v;
v.type=four_wheeler;
v.fuel=diesel;
v.model=new;
printf(“Type of vehicle :%d\n,v.type);
printf(“Fuel :%d\n”);
printf(“Model :%d\n”);
getch();
}
OUTPUT:
Type of vehicle :4
Fuel :2
Model :6

Appendix-C.p65 5 7/29/08, 5:21 PM


C.6 Solved Question Papers
5. Write a C program to read a text file and to count
(a) number of characters
(b) number of words
(c) number of sentences and write in an output file
#include<stdio.h>
#include<conio.h>
main ()
{
FILE *fptr;
int nl=0,nw=0,nc=0;
char ch;
clrscr();
fptr=fopen(“input.c”,”r”);
if (fptr!=NULL)
{
while (!foef(fptr))
{
ch=getc (fptr);
if (ch==’ ‘||ch==’\t’)
nw++;
else if(ch==’\n’)
nl++;
else
nc++;
}
fclose (fptr);
fptr=fopen (“output.c” , ”w”);
fprintf (fptr,”number of lines =%d\n” , nl);
fprintf (fptr,”number of words =%d\n” , nw);
fprintf (fptr,”number of characters =%d\n” , nc);
}
else
printf(“the input file does not exist\n”);
getch();
}
6. Declare a circular queue of integers such that F points to the front and R points to the
rear. Write functions
(a) to insert an element into the queue
(b) to delete an element from the queue
#include<stdio.h>
#define size 20
int cq[size],F=0,R=0,count=0;
main()
{
int a,ch;
clrscr();

Appendix-C.p65 6 7/29/08, 5:21 PM


Solved Question Papers C.7
while(1)
{
printf(“main menu\n”);
printf(“1.insertion\t2.deletion\t3.display\t4.exit\n”);
printf(“enter your choice\n”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“enter the value to be inserted\n”);
scanf(“%d”,&a);
insert(a);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf(“wrong choice\n”);
}
getch(); }

/* insertion function */
insert(int x)
{
if(F==R&&count==size-1)
{
printf(“circular queue is full\n”);
return;
}
cq[R]=x;
R=(R+1)%size;
}

/* deletion function */
delete()
{
if(F==R&&count==0)
{
printf(“circular queue is empty\n”);
return;
}
printf(“the deleted element is %d\n”,cq[F]);

Appendix-C.p65 7 7/29/08, 5:21 PM


C.8 Solved Question Papers

F=(F+1)%size;
}

/* display function */
display()
{
int i;
if(F==R&&count==0)
{
printf(“circular queue is empty\n”);
return;
}
for(i=F;i!=R;i=(i+1)%size)
printf(“%d”,cq[i]);
}
7. Write a function in C to form a list containing the intersection of the elements of two lists.
Intersection()
{
if(first1==NULL)
{
printf(“list is empty. So no elements in common\n”);
return;
}
else if(first2==NULL)
{
printf(“list is empty. So no elements in common\n”);
return;
}
else
{
ptr1=first1;
ptr2=first2;
while(ptr1->next!=NULL)
{
ptr2=first2;
while(ptr2->next!=NULL)
{
if(ptr1->data==ptr2->data)
{
if(first3==NULL)
{
first3=(node *)malloc(sizeof(node));
first3->data=ptr1->data;
first3->next=NULL;
}
else
{

Appendix-C.p65 8 7/29/08, 5:21 PM


Solved Question Papers C.9

ptr3=first3;
while(ptr3->next!=NULL)
ptr3=ptr3->next;
fresh=(node *)malloc(sizeof(node));
fresh->data=ptr1->data;
ptr3->next=fresh;
fresh->next=NULL;
}/* close of else */
}/* close of if */
}/* close of inner while loop */
}/* close of outer while loop */
}/* close of function intersection */
8. (a) Write a C program to sort given integers using partition exchange sort.
Partition exchange sort is also called quick sort.
/* main function */
#include<stdio.h>
#include<conio.h>
main()
{
int i,n,a[20];
clrscr();
printf(“enter the size of array\n”);
scanf(“%d”,&n);
printf(“enter the elements into array\n”);
for(i=0;i<n;i++)
scanf(“%d”,a+i);
quicksort(a,0,n-1);
prints(“elements after sorting are\n”);
for(i=0;i<n;i++)
printf(“%d\t”,*(a+i));
getch();
}
/* QUICK SORT FUNCTION */
quicksort(int *k,int lb,in tub)
{
int pivot,i,j;
if(lb<ub)
{
i=lb;
j=ub;
pivot=i;
while(i<j)
{
while((*(k+i)<=*(k+pivot))&&(i<ub))
i++;
while(*(k+j)>*(k+pivot))
j– – ;

Appendix-C.p65 9 7/29/08, 5:21 PM


C.10 Solved Question Papers
if(i<j)
swap(k+i,k+j);
}
swap(k+j,k+pivot);
quicksort(k,lb,j-1);
quicksort(k,j+1,ub);
}
return;
}

/* Function for swapping */


swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
(b) Derive the time complexity of the following partition exchange sort.

n
CA(n)=1/n å K = 1 {CA(k–1)+ CA(n–k)}+(n+1)
n
nCA(n)=n(n+1)+ å K = 1 {CA(k–1)+ CA(n–k)}
=n(n+1)+{CA(0)+ CA(n–1)+ CA(1)+ CA(n–2) +……+ CA(n–1) + CA(0)}
nCA(n)=n(n+1)+2{CA(0)+ CA(1)+……..+ CA(n–1)}……(1)
replace n with (n–1)
(n–1)CA(n–1)=n(n–1)+2{CA(0)+ CA(1)+……..+ CA(n–2)}….(2)

Subtract (2) from (1)

nCA(n)– (n–1)CA(n–1)=n2+n–n2+n+2 CA(n–1)


nCA(n)=2n+2 CA(n–1)+ (n–1)CA(n–1)
nCA(n)=2n+ CA(n–1)[n–1+2]
nCA(n)=2n+ CA(n–1)[n+1]
divide on both sides by n(n+1)
(CA(n)/(n+1))=(2/(n+1))+ (CA(n–1)/n)
(CA(n)/(n+1))=(2/(n+1))+ 2/n+(CA(n–2)/(n–1))
=2/(n+1)+2/n+2/(n–1)+2/(n–2)+..+2/4+ CA(2)/3+2/3+ CA(1)/2
n +1
=2å (1/k)
K =3

Appendix-C.p65 10 7/29/08, 5:21 PM


Solved Question Papers C.11

n +1
=2ò 1/kdk
3
n +1
=2[logk] 3
(CA(n)/(n+1))=2[log(n+1)–log3]
CA(n)=2(n+1)log(n+1)–2(n+1)log3
CA(n)=2nlog(n+1)+2 log(n+1)–2nlog3–2 log3
CA(n) »2nlog(n+1)
CA(n)=O(nlogn)

Appendix-C.p65 11 7/29/08, 5:21 PM

Das könnte Ihnen auch gefallen