Tuesday, February 8, 2011

C-programms



/* 45. CREATE A FILE OF TEN RECORDS WHERE EACH RECORD HAS
THE FOLLOWING FIELD : IDNO,NAME,MARKS AND AGE.
READ DATA INTO THIS FILE. FIND THE AVERAGE AGE OF
THESE TEN STUDENTS AND ALSO THE NAME OF THE STUDENT
WHO SCORED THE MAXIMUM MARKS.
*/
#include
#include

#define MAX 10

struct student{
char name[20],idno[11];
int age;
int marks;
};

void main()
{
struct student stu;
FILE *fp;
int i,maxmarks;
char maxname[20];
float avg=0;
clrscr();
printf("Enter the records for each student :");
fp=fopen("c:\\student.txt","w");
for(i=0;i {
printf("\nEnter %d student's record",i+1);
printf("\n NAME : ");
scanf("%s",stu.name);
printf(" IDNO : ");
scanf("%s",stu.idno);
printf(" AGE : ");
scanf("%d",&stu.age);
printf(" MARKS : ");
scanf("%d",&stu.marks);
fprintf(fp,"%s %s %d %d\n",stu.name,stu.idno,stu.age,stu.marks);
/* fwrite(&stu,sizeof(stu),1,fp);*/
}
fclose(fp);
fp=fopen("c:\\student.txt","r");
maxmarks=0;
/* while(fread(&stu,sizeof(stu),1,fp)==1)*/
while(fscanf(fp,"%s %s %d %d\n",stu.name,stu.idno,&stu.age,&stu.marks)!=EOF)
{
avg+=stu.age;
if(stu.marks>maxmarks)
{
maxmarks=stu.marks;
strcpy(maxname,stu.name);
}
}
fclose(fp);
avg=avg/MAX;
printf("\n\tAverage age of %d students is %f",MAX,avg);
printf("\n\t\"%s\" has scored max marks of %d",maxname,maxmarks);
getch();
}

/*
=====OUT PUT=====
Enter the records for each student :
Enter 1 student's record
NAME : QQQQQ
IDNO : 001
AGE : 20
MARKS : 67

Enter 2 student's record
NAME : WWWWWWW
IDNO : 002
AGE : 21
MARKS : 35

Enter 3 student's record
NAME : EEEEEEE
IDNO : 006
AGE : 22
MARKS : 54

Enter 4 student's record
NAME : RRRRRR
IDNO : 009
AGE : 22
MARKS : 89

Enter 5 student's record
NAME : TTTTTT
IDNO : 005
AGE : 21
MARKS : 58

Enter 6 student's record
NAME : YYYYYY
IDNO : 007
AGE : 22
MARKS : 98

Enter 7 student's record
NAME : UUUUUUU
IDNO : 0011
AGE : 23
MARKS : 70

Enter 8 student's record
NAME : IIIIIII
IDNO : 0012
AGE : 20
MARKS : 45

Enter 9 student's record
NAME : OOOOOO
IDNO : 0013
AGE : 19
MARKS : 88

Enter 10 student's record
NAME : PPPPPP
IDNO : 0015
AGE : 23
MARKS : 76

Average age of 10 students is 21.299999
"YYYYYY" has scored max marks of 98

*/






















/* 46. CONVERT ALL LOWER CASE LETTERS IN A FILE TO
UPPER CASE LETTERS.
*/

#include
#include

void main()
{
FILE *fp,*fp1;
char ch;
clrscr();

fp=fopen("c:\\stu.txt","r+");
if(fp==NULL)
printf("Cannot open source file");

fp1=fopen("c:\\stu1.txt","w");
if(fp1==NULL)
printf("Cannot open dest file");

else
{
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
else if(ch>='a' && ch<='z')
{
ch=ch-32;
fprintf(fp1,"%c",ch);
/* fputc(ch,fp);*/
}
else
fprintf(fp1,"%c",ch);
/* fputc(ch,fp);*/
}

fclose(fp);
fclose(fp1);

fp1=fopen("c:\\stu1.txt","r");
if(fp1==NULL)
printf("\nCannot open source2 file");


fp=fopen("c:\\stu.txt","w");
if(fp==NULL)
printf("\nCannot open dest2 file");

while(1)
{
ch=fgetc(fp1);
if(ch==EOF)
break;
else
fputc(ch,fp);
}

fclose(fp);
fclose(fp1);
}
getch();
}

/*
=====OUT PUT=====

CONTENTS IN FILE stu.txt:
kaundinya
bharath
srikanth

AFTER EXECUTING stu.txt:

KAUNDINYA
BHARATH
SRIKANTH
*/




















/* 47. WRITE A C PROGRAM TO COPY THE CONTENTS OF ONE FILE
TO ANOTHER.
*/

#include
#include

void main()
{
FILE *fp,*fp1;
char ch;
clrscr();

fp=fopen("c:\\stu.txt","r");
if(fp==NULL)
printf("\nCannot open source file");

fp1=fopen("c:\\stu1.txt","w");
if(fp1==NULL)
printf("\nCannot open dest file");

while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
else
fputc(ch,fp1);
}

fclose(fp);
fclose(fp1);
getch();
}
/*
=====OUT PUT=====

CONTENTS OF FILE stu.txt:
BHARATH
kaundinya
srikanth
bond007

AFTER EXECUTION CONTENTS OF
FILE STU1.txt:
BHARATH
kaundinya
srikanth
bond007
*/
/*49. WRITE A C PROGRAM TO APPEND A FILE */

#include
#include

void main()
{
FILE *fp;
char ch,arr[20];
int i=0;
clrscr();

fp=fopen("c:\\stu.txt","a");
if(fp==NULL)
printf("\nCannot open source file");

printf("Enter data to append\n");
gets(arr);

while(1)
{
ch=arr[i++];
if(ch=='\0')
break;
else
fputc(ch,fp);
}

fclose(fp);
getch();
}
/*
=====OUT PUT=====

CONTENTS OF FILE stu.txt:
BHARATH
kaundinya
srikanth
bond007

AFTER APPENDING:
BHARATH
kaundinya
srikanth
bond007
All are friends
*/



/* 50. CONSIDER TWO SORTED FILES FILE1 AND FILE2. WRITE A PROGRAM
TO GENERATE FILE3 WHICH IS THE RESULT OF MERGING FILE1 AND FILE2
IN THE SORTED ORDER.
*/
#include
#include

void main()
{
FILE *fp1,*fp2,*fp;
char ch1,ch2;
clrscr();

fp1=fopen("c:\\file1.txt","r");
if(fp==NULL)
printf("\nCannot open source1 file");

fp2=fopen("c:\\file2.txt","r");
if(fp1==NULL)
printf("\nCannot open source2 file");

fp=fopen("c:\\file3.txt","w");
if(fp1==NULL)
printf("\nCannot open destination file");

ch1=fgetc(fp1);
ch2=fgetc(fp2);
while(ch1!=EOF && ch2!=EOF)
{
if(ch1==ch2)
{
fputc(ch1,fp);
fputc(ch2,fp);
ch1=fgetc(fp1);
ch2=fgetc(fp2);
}
else if(ch1 < ch2)
{
fputc(ch1,fp);
ch1=fgetc(fp1);
}
else
{
fputc(ch2,fp);
ch2=fgetc(fp2);
}
}



while(ch1!=EOF)
{
fputc(ch1,fp);
ch1=fgetc(fp1);
}

while(ch2!=EOF)
{
fputc(ch2,fp);
ch2=fgetc(fp2);
}


fclose(fp1);
fclose(fp2);
fclose(fp);
getch();
}
/*
=====OUT PUT=====

CONTENTS OF FILE1:
a b j m o q st w

CONTENTS OF FILE2:
c k p r x z

CONTENTS OF FILE3:
a b c j k m o p q r st wx z
*/




















/* 51.WRITE A C PROGRAM TO DEMONSTRATE THE WORKING OF A STACK OF
SIZE N USING AN ARRAY.THE ELEMENTS OF THE STACK MAY BE
ASSUMED TO BE OF TYPE INTEGER OR REAL. THE OPERATIONS TO BE
SUPPORTED ARE:
a.PUSH b.POP C.DISPLAY
THE SHOULD PRINT APPROPRIATE MESSAGES FOR STACK OVERFLOW,
STACK UNDERFLOW AND STACK EMPTY. YOU MAY WRITE SEPERATE
FUNCTIONS TO DETECT THESE CASES.
*/
#include
#include

#define N 5

struct stack
{
int top;
int arr[N];
}st;


int empty()
{
if(st.top==-1)
return 1;
else
return 0;
}

int full()
{
if(st.top==N-1)
return 1;
else
return 0;
}

void push()
{
if(full())
printf("\n\tSTACK OVERFLOW...");
else
{
printf("\nEnter a element : ");
scanf("%d",&st.arr[++st.top]);
}
}



void pop()
{
if(empty())
printf("\n\tSTACK UNDERFLOW...");
else
printf("\n\tPoped element is %d ",st.arr[st.top--]);

}

void display()
{
int i;
if(empty())
printf("\n NO ELEMENTS\n");
else
for(i=0;i<=st.top;i++)
printf("\t%d",st.arr[i]);

}
void main()
{
int choice=0;
clrscr();
st.top=-1;
while(1)
{
printf("\n\n1.PUSH");
printf("\n2.POP");
printf("\n3.DISPLAY");
printf("\n4.EXIT");
printf("\n\tenter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
default:exit(0);break;
}
}
}









/*
=====OUT PUT=====
1.PUSH
2.POP
3.DISPLAY
4.EXIT
enter your choice : 3

NO ELEMENTS


1.PUSH
2.POP
3.DISPLAY
4.EXIT
enter your choice : 2

STACK UNDERFLOW...

MAIN MENU

enter your choice : 1

Enter a element : 2

MAIN MENU

enter your choice : 1

Enter a element : 45

MAIN MENU

enter your choice : 1

Enter a element : 68

MAIN MENU

enter your choice : 1

Enter a element : 12

MAIN MENU

enter your choice : 1

Enter a element : 34


MAIN MENU

enter your choice : 1

STACK OVERFLOW...

MAIN MENU

enter your choice : 3
2 45 68 12 34

MAIN MENU

enter your choice : 2

Poped element is 34
*/

/*59. WRITE A PROGRAM TO SIMULATE THE WORKING OF A QUEUE OF
NAMES USING AN ARRAY.PROVIDE THE FOLLOWING OPERATIONS:
a.CQINSERT b.CQDELETE c.CQDISPLAY
*/
#include
#include
#include

#define MAX 5
#define SIZE 20

struct cqueue
{
char name[MAX][SIZE];
int rear;
int front;
}cq;

int cqempty()
{
int i,count=0,flag=1;
if(cq.front==-1 && cq.rear ==-1)
return 1; /* Empty */
else
{
for(i=cq.front;count {
if( strcmp(cq.name[i],"")==0)
count++;
else
flag=0;
}
if(count==MAX)
return 1;
}
return 0; /* Not Empty */
}

int cqfull()
{
int i,count=0,flag=1;
for(i=cq.rear;count {
if( strcmp(cq.name[i],"")==0)
flag=0;
else
count++;
}
if(count==MAX)
return 1; /* Full */
return 0; /* Not Full */
}

void cqinsert()
{
int i;
if(!cqfull())
{
if(cq.front==-1 && cq.rear==-1)
cq.front=cq.rear=0;
else
cq.rear=(cq.rear+1)%MAX;
printf("\nEnter Name to Insert\t");
scanf("%s",cq.name[cq.rear]);
printf("\n INSERTED ");
}
else
printf("\nQueue is Full");
}

void cqdelete()
{
int i;
if(!cqempty())
{
printf("\nDeleted name is %s",cq.name[cq.front]);
strcpy(cq.name[cq.front],"");
if(!cqempty())
cq.front=(cq.front+1)%MAX;
else
cq.front=cq.rear=-1;
}
else
printf("Queue is empty");
}

void cqdisplay()
{
int i;
if(cqempty())
printf("QUEUE IS EMPTY");
else
{
for(i=cq.front;i!=cq.rear ; i=(i+1)%MAX)
printf("\n%s",cq.name[i]);
printf("\n%s",cq.name[i]);
}
}

void main()
{

int i,choice=4;

cq.rear=-1;
cq.front=-1;
for(i=0;i<5 data-blogger-escaped-br="br" data-blogger-escaped-i="i"> strcpy(cq.name[i],"");

while(choice!=0)
{
clrscr();
printf("\n 1. CQINSERT");
printf("\n 2. CQDELETE");
printf("\n 3. CQDISPLAY");
printf("\n 4. EXIT \t:");
scanf("%d",&choice);
switch(choice)
{
case 1:cqinsert();getch();break;
case 2:cqdelete();getch();break;
case 3:cqdisplay();getch();break;
default:choice=0;break;
}
}
}






/*
=====OUT PUT=====

1. CQINSERT
2. CQDELETE
3. CQDISPLAY
4. EXIT :3
QUEUE IS EMPTY

1. CQINSERT
2. CQDELETE
3. CQDISPLAY
4. EXIT :2
Queue is empty

MAIN MENU
:1
Enter Name to Insert BHARATH

INSERTED

MAIN MENU
:1
Enter Name to Insert KAUNDINYA

INSERTED

MAIN MENU
:1
Enter Name to Insert SRIKANTH

INSERTED

MAIN MENU
:1
Enter Name to Insert JAYANTH

INSERTED

MAIN MENU
:1
Enter Name to Insert SHARMA

INSERTED

MAIN MENU
:1
Queue is Full


MAIN MENU
:3
BHARATH
KAUNDINYA
SRIKANTH
JAYANTH
SHARMA

MAIN MENU
:2
Deleted name is BHARATH

MAIN MENU
:3
KAUNDINYA
SRIKANTH
JAYANTH
SHARMA
*/

/* 60.USING DYNAMIC VARIABLES AND POINTERS, CONSTRUCT A
SINGLY LINKED LIST CONSISTING OF THE FOLLOWING
INFORMATION IN EACH NODE :
a.LINSERT-INSERTING IN FRONT OF THE LIST
b.LDELETE-DELETING A NODE BASED ON JOB-ID
c.LSEARCH-SEARCHING A NODE BASED ON JOB-ID
d.LDISPLAY-DISPLAYING ALL THE NODES IN THE LIST
*/
#include
#include

struct node
{
int jobid;
char jobname[20],jobdesc[20];
struct node *next;
};

typedef struct node* NODEPTR;

NODEPTR getnode()
{
NODEPTR p=(NODEPTR)malloc(sizeof(struct node));
p->next=NULL;
return p;
}




NODEPTR Linsert(NODEPTR head)
{
NODEPTR p=getnode();

printf("\nEnter job id : ");
scanf("%d",&p->jobid);
printf("Enter job name : ");
scanf("%s",p->jobname);
printf("Enter job description : ");
scanf("%s",p->jobdesc);

if(head==NULL)
head=p;
else
{
p->next=head;
head=p;
}
printf("Inserted");
getch();
return head; /* Return the address to which head is pointing */
}

NODEPTR Ldelete(NODEPTR head)
{
int tempjobid;
NODEPTR p,q;
printf("\nEnter the jobid of node u want to delete : ");
scanf("%d",&tempjobid);

for(p=head,q=NULL ; p!=NULL ; q=p,p=p->next)
{
if(p->jobid==tempjobid)
{
if(q==NULL)
head=p->next;
else
q->next=p->next;

free(p);
break; /* Once deleted leave the loop */
}
}
if(p==NULL)
printf("NO NODE WITH THAT JOBID");
else
printf("Deleted.");
getch();
return(head);
}

void Lsearch(NODEPTR head)
{
NODEPTR p;
int tempjobid;

printf("Enter the job id to search : ");
scanf("%d",&tempjobid);
for(p=head;p!=NULL;p=p->next)
{
if(p->jobid==tempjobid)
{
printf("Found");
break; /* Once found leave the loop */
}
}
if(p==NULL)
printf("NOT FOUND");
getch();
}

void Ldisplay(NODEPTR head)
{
NODEPTR p;
printf("\n\tjobid\tjobname\tjobdesc\n");
for(p=head;p!=NULL;p=p->next)
printf("\n\t%d\t%s\t%s",p->jobid,p->jobname,p->jobdesc);
getch();
}

void main()
{
int choice=0;
NODEPTR head=NULL;
clrscr();

while(1)
{
clrscr();
printf("\n1. Insert front of the list");
printf("\n2. Delete node based on job-id");
printf("\n3. Searching a node based on job-id");
printf("\n4. Displaying all nodes in list");
printf("\n5. Exit");
printf("\n\tEnter your choice : ");
scanf("%d",&choice);




switch(choice)
{
case 1:head=Linsert(head);break;
case 2:head=Ldelete(head);break;
case 3:Lsearch(head);break;
case 4:Ldisplay(head);break;
default:exit(0);break;
}
}
}

/*
=====OUT PUT=====

1. Insert front of the list
2. Delete node based on job-id
3. Searching a node based on job-id
4. Displaying all nodes in list
5. Exit
Enter your choice : 1

Enter job id : 001
Enter job name : aaaaa
Enter job description : sssss
Inserted

MAIN MENU
Enter your choice : 1

Enter job id : 002
Enter job name : DDDDD
Enter job description : FFFFF
Inserted

MAIN MENU
Enter your choice : 1

Enter job id : 003
Enter job name : GGGG
Enter job description : HHHHH
Inserted

MAIN MENU
Enter your choice : 4

jobid jobname jobdesc

3 GGGG HHHHH
2 DDDDD FFFFF
1 aaaaa sssss

MAIN MENU
Enter your choice : 3
Enter the job id to search : 002
Found

MAIN MENU
Enter your choice : 3
Enter the job id to search : 004
NOT FOUND

MAIN MENU
Enter your choice : 2

Enter the jobid of node u want to delete : 003
Deleted.

MAIN MENU
Enter your choice : 2

Enter the jobid of node u want to delete : 003
NO NODE WITH THAT JOBID

MAIN MENU
Enter your choice : 4

jobid jobname jobdesc

2 DDDDD FFFFF
1 aaaaa sssss

*/


















/*61. A DEQUE IS A LIST IN WHICH ITEMS CAN BE ADDED OR DELETED FROM
EITHER END OF THE LIST.IMPLEMENT THE WORKING OF SUCH A DEQUE OF
INTEGERS
*/
#include
#include

struct node
{
int val;
struct node *next;
};

typedef struct node* NODEPTR;

NODEPTR getnode()
{
NODEPTR p=(NODEPTR)malloc(sizeof(struct node));
p->next=NULL;
return p;
}

NODEPTR Insertleft(NODEPTR front,NODEPTR rear)
{
NODEPTR p=getnode();

printf("Enter value of node : ");
scanf("%d",&p->val);

if(front==NULL && rear ==NULL)
{
front=p;
rear=p;
}
else
{
p->next=front;
front=p;
}
return front;
}

NODEPTR Insertright(NODEPTR front,NODEPTR rear)
{
NODEPTR p=getnode();

printf("Enter value of node : ");
scanf("%d",&p->val);


if(rear==NULL && front==NULL)
front=rear=p;
else
{
rear->next=p;
rear=p;
}
return rear;
}
NODEPTR Removeleft(NODEPTR front,NODEPTR rear)
{
NODEPTR p;
if(front==NULL)
printf("No element to delete");
else
{
if(front==rear)
front=NULL;
else
{
p=front;
front=p->next;
}
printf("\nDeleted element from left is %d",p->val);
free(p);
}
return front;
}

NODEPTR Removeright(NODEPTR front,NODEPTR rear)
{
NODEPTR p;
if(rear==NULL)
printf("No elements to delete");
else
{
if(front==rear)
rear=NULL;
else
{
for(p=front;p->next!=rear;p=p->next);
rear=p;
p=p->next;
rear->next=NULL;
}
printf("\nDeleted element from right is %d",p->val);
free(p);
}
return rear;
}

void Display(NODEPTR front,NODEPTR rear)
{
NODEPTR p;
if(front==NULL && rear==NULL)
printf("No elements");
else
{
for(p=front;p!=NULL;p=p->next)
printf("\t%d",p->val);
}
}

NODEPTR placefront(NODEPTR front,NODEPTR rear)
{
if(rear==NULL)
front=NULL;
else if(front==NULL)
front=rear;
return front;
}

NODEPTR placerear(NODEPTR front,NODEPTR rear)
{
NODEPTR p;
if(front==NULL)
rear=NULL;
else
{
for(p=front;p->next!=NULL;p=p->next);
rear=p;
}
return rear;
}

void main()
{
int choice=0;
NODEPTR front=NULL,rear=NULL;
clrscr();
while(1)
{
printf("\n\n1. Insert left");
printf("\n2. Insert Right");
printf("\n3. Remove left");
printf("\n4. Remove right");
printf("\n5. Display");
printf("\n6. Exit");
printf("\n\tEnter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:front=Insertleft(front,rear);
rear=placerear(front,rear);
break;
case 2:rear=Insertright(front,rear);
front=placefront(front,rear);
break;
case 3:front=Removeleft(front,rear);
rear=placerear(front,rear);
break;
case 4:rear=Removeright(front,rear);
front=placefront(front,rear);
break;
case 5:Display(front,rear);break;
default:exit(0);break;
}
}
}

/*
=====OUT PUT=====

1. Insert left
2. Insert Right
3. Remove left
4. Remove right
5. Display
6. Exit
Enter your choice : 5
No elements

MAIN MENU
Enter your choice : 4
No elements to delete

MAIN MENU
Enter your choice : 3
No element to delete

MAIN MENU
Enter your choice : 1
Enter value of node : 1

MAIN MENU
Enter your choice : 1
Enter value of node : 2


MAIN MENU
Enter your choice : 2
Enter value of node : 5

MAIN MENU
Enter your choice : 2
Enter value of node : 4

MAIN MENU
Enter your choice : 5
2 1 5 4

MAIN MENU
Enter your choice : 3

Deleted element from left is 2

MAIN MENU
Enter your choice : 3

Deleted element from left is 1

MAIN MENU
Enter your choice : 3

Deleted element from left is 5

MAIN MENU
Enter your choice : 5
4

MAIN MENU
Enter your choice : 2
Enter value of node : 6

MAIN MENU
Enter your choice : 4

Deleted element from right is 6

MAIN MENU
Enter your choice : 4

Deleted element from right is 4

MAIN MENU
Enter your choice : 5
No elements
*/

/* 63.USING POINTERS AND DYNAMIC VARIABLES, CONSTRUCT A
BINARY SEARCH TREE (BST) OF INTEGERS.
a.GIVEN A KEY, PERFORM A SEARCH IN THE BST.IF THE KEY IS
FOUND THEN DISPLAY "KEY FOUND", ELSE INSERT THE KEY IN BST
b.WHILE CONSTRUCTING THE BST, DO NOT ADD ANY DUPLICATES.
c.DISPLAY THE TREE USING ANY ONE OF THE TRAVERSAL METHODS.
*/
#include
#include

struct node
{
int val;
struct node *left;
struct node *right;
};

typedef struct node* NODEPTR;

NODEPTR getnode()
{
NODEPTR p=(NODEPTR)malloc(sizeof(struct node));
p->left=NULL;
p->right=NULL;
return p;
}

NODEPTR searchbst(NODEPTR root)
{
NODEPTR p,q;
int num,flag=1;
printf("\n\nEnter an element to search\t");
scanf("%d",&num);

if(root == NULL)
{
p=getnode();
p->val=num;
root=p;
printf("\tINSERTED");
}
else
{
q=root;
while(flag!=0)
{

if(num==q->val)
{
printf("KEY FOUND");
flag=0;
}
else if(num < q->val) /*less than */
{
if(q->left != NULL)
q=q->left;
else
{
p=getnode();
p->val=num;
q->left=p;
flag=0;
printf("\tINSERTED");
}
}
else/* greater than */
{
if(q->right != NULL)
q=q->right;
else
{
p=getnode();
p->val=num;
q->right=p;
flag=0;
printf("\tINSERTED");
}
}

} /* end while(flag) */
} /* end else */
return root;
}

void inorder(NODEPTR root)
{
if(root!=NULL)
{
inorder(root->left);
printf(" %d ",root->val);
inorder(root->right);
}
}

void main()
{
NODEPTR root=NULL;
int choice;
clrscr();
while(choice!=0)
{
clrscr();
printf("\nEnter 1 to search");
printf("\nEnter 2 to traverse using inorder");
printf("\nEnter 3 to exit");
printf("\n\t\tchoice = ");
scanf("%d",&choice);
switch(choice)
{
case 1: root=searchbst(root);getch();break;
case 2: inorder(root);getch();break;
default:choice=0;break;
}
}
}
/*
=====OUT PUT=====

Enter 1 to search
Enter 2 to traverse using inorder
Enter 3 to exit
choice = 1
Enter an element to search 123
INSERTED

MAIN MENU
choice = 1
Enter an element to search 50
INSERTED

MAIN MENU
choice = 1
Enter an element to search 150
INSERTED
MAIN MENU
choice = 1
Enter an element to search 75
INSERTED

MAIN MENU
choice = 2
50 75 123 125 150
*/







/* 66. WRITE A C PROGRAM TO SORT A LIST OF N ELEMENTS OF
INTEGER TYPES USING THE QUICK SORT ALGORITHM.
*/
#include
#include

/* Function to Partition */
int partition(int a[],int low,int high)
{
int i,j,temp,key;
key=a[low];
i=low+1;
j=high;
while(1)
{
while(i=a[i])
i++;

while(key < a[j])
j--;

if(i temp=a[i],a[i]=a[j],a[j]=temp;
else
{
temp=a[low],a[low]=a[j],a[j]=temp;
return j;
}

}
}

/* Function to Quick Sort */
void quicksort(int a[],int low,int high)
{
int j;
if(low < high)
{
j=partition(a,low,high);
quicksort(a,low,j-1);
quicksort(a,j+1,high);
}
}







void main()
{
int i,n,a[20];
clrscr();
printf("Enter The Value for N : ");
scanf("%d",&n);
printf("Enter The Numbers To be Sorted :\n");
for(i=0;i scanf("%d",&a[i]);
quicksort(a,0,n-1);
printf("The Sorted array is ...\n");
for(i=0;i printf("\n%d",a[i]);
getch();
}

/*
=====OUT PUT=====

Enter The Value for N : 5
Enter The Numbers To be Sorted :
9
7
8
4
6
The Sorted array is ...

4
6
7
8
9

*/















/*67. WRITE A C PROGRAM TO SIMULATE THE WORKING OF
TOWERS OF HANOI PROBLEM FOR N DISC.PRINT THE NUMBER OF
MOVES BY YOUR PROGRAM.
*/
#include
#include
int count=0;
void tower(int n,int source,int temp,int destination)
{
if(n==1)
{
printf("Move disk 1 from %c to %c\n",source,destination);
count++;
return;
}

/* Move n-1 disks from A to B using C as Temporary Peg */
tower(n-1,source,destination,temp);
printf("Move disk %d from %c to %c\n",n,source,destination);
count++;

/* Move n-1 disks from B to C using A as Temporary peg */
tower(n-1,temp,source,destination);
}

void main()
{
int n;
clrscr();
printf("\n Enter the Number of disks : ");
scanf("%d",&n);
tower(n,'A','B','C');
printf("\n Total number of disk moves = %d",count);
getch();
}















/*
=====OUT PUT=====

Enter the Number of disks : 4
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C
Move disk 3 from A to B
Move disk 1 from C to A
Move disk 2 from C to B
Move disk 1 from A to B
Move disk 4 from A to C
Move disk 1 from B to C
Move disk 2 from B to A
Move disk 1 from C to A
Move disk 3 from B to C
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C

Total number of disk moves = 15
*/




























BONUS PROGRAMS

/* C PROGRAM TO PRINT ASCII CODES */

#include
#include

void main()
{
int i;
char choice;
clrscr();
printf("\t\tEnter 'n' to stop\n\n");
for(i=0;i<256 data-blogger-escaped-br="br" data-blogger-escaped-i="i"> {
printf(" %d is %c \n",i,i);
choice=getch();
if(choice=='n' || choice=='N')
break;
}

}

/* C PROGRAM TO PRINT BIOSKEY */

#include
#include
#include

void main()
{
unsigned int a;
clrscr();
printf("Enter a key : ");
a=bioskey(0);
printf("The value is %u",a);
getch();

}
http://rajkumardownloads.blogspot.com/

0 comments:

Post a Comment