Beruflich Dokumente
Kultur Dokumente
+
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
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
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
/* 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]);
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
{
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 ;
n
CA(n)=1/n å K = 1 {CA(k1)+ CA(nk)}+(n+1)
n
nCA(n)=n(n+1)+ å K = 1 {CA(k1)+ CA(nk)}
=n(n+1)+{CA(0)+ CA(n1)+ CA(1)+ CA(n2) +
+ CA(n1) + CA(0)}
nCA(n)=n(n+1)+2{CA(0)+ CA(1)+
..+ CA(n1)}
(1)
replace n with (n1)
(n1)CA(n1)=n(n1)+2{CA(0)+ CA(1)+
..+ CA(n2)}
.(2)
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)2nlog32 log3
CA(n) »2nlog(n+1)
CA(n)=O(nlogn)