Program No. / Binary Search With Time Calculation
Program No. / Binary Search With Time Calculation
start=clock();
printf("\n\nEnter the value to be searched:");
scanf("%d",&elt);
//binary search
while((first<=last)&&(pos==-1))
{ mid=(first+last)/2;
if(a[mid]==elt)
pos=mid;
else
if(a[mid]<elt)
first=mid+1;
else
last=mid-1;
}
end=clock();
var=(end-start)/CLK_TCK;
if(pos>(-1))
printf("\nThe position of the element is %d",pos);
else
printf("\nUnsucessful search!");
printf("\nTime taken is %fsec",var);
puts("\nWanna search more?");
fflush(stdin);
ch=getchar();
fflush(stdin);
}while(ch!='n'&&ch!='N');
getch();
}
PROGRAM NO.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\n\n\n\t\t\tNewline, Backspace and Tab");
printf("\n\n\t\t\t\t are");
printf("\n\n\t The most commonly used escape sequences in C language ");
printf("\n\n\nFor example different output of word INDIA using these escape sequences are");
printf("\n\nINDIA :Using New line");
printf("\n\n\tINDIA :Using Tab");
printf("\n\n\tINDIA\b :Using Backspace after Tab");
getch();
}
PROGRAM NO.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num1,num2;
printf("\nEnter the two numbers to be compared");
scanf("%d",&num1);
scanf("%d",&num2);
(num1>=num2)?(num1=num1):(num1=num2);
getchar();
getchar();
}
PROGRAM NO.
getch();
}
PROGRAM NO.
#include<stdio.h>
#include<conio.h>
void main()
{ int a,t;
clrscr();
printf("Enter valid angle of your choice: ");
scanf("%d",&a);
a=(a%360);
t=a/90;
switch(t)
{ case 0: printf("\nFirst Quadrant");
break;
case 1: printf("\nSecond Quadrant");
break;
case 2: printf("\nThird Quadrant");
break;
case 3: printf("\nFourth Quadrant");
break;
default: printf("\nWrong!!! Input");
break;
}
getch();
}
PROGRAM NO.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define NULL 0
typedef struct list
{ int coeff,exp;
struct list*next;
}node;
node *ptr,*np,*start,*rear,*newpt;
void display(node *ptr)
{ while(ptr!=NULL)
{ printf("%dx^%d + ",ptr->coeff,ptr->exp);
ptr=ptr->next;
}
printf("0");
}
node* create_node()
{ int c,e;
ptr=(node*)malloc(sizeof(node));
printf("\nenter the coeff and exp :::::::");
scanf("%d%d",&c,&e);
ptr->coeff=c;
ptr->exp=e;
ptr->next=NULL;
return(ptr);
}
void insert_end(node*n)
{ rear->next=n;
rear=n;
}
void main()
{ char ch='y';
clrscr();
printf("\ncreating node!!!!!!");
np=create_node();
start=rear=np;
while(ch=='y'||ch=='Y')
{
printf("\n inserting the polynomial");
newpt=create_node();
insert_end(newpt);
printf("\n wanna insert more::::");
fflush(stdin);
scanf("%c",&ch);
}
# include<stdio.h>
# include<malloc.h>
struct node
{
int info;
struct node *link;
} *top=NULL;
main()
{
int choice;
while(1)
{ printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ") ;
scanf("%d", &choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
}/*End of main() */
push()
{
struct node *tmp;
int pushed_item;
tmp = (struct node *)malloc(sizeof(struct node));
printf("Input the new value to be pushed on the stack : ");
scanf("%d",&pushed_item);
tmp->info=pushed_item;
tmp->link=top;
top=tmp;
}/*End of push()*/
pop()
{
struct node *tmp;
if(top == NULL)
printf("Stack is empty\n");
else
{ tmp=top;
printf("Popped item is %d\n",tmp->info);
top=top->link;
free(tmp);
}
}/*End of pop()*/
display()
{ struct node *ptr;
ptr=top;
if(top==NULL)
printf("Stack is empty\n");
else
{
printf("Stack elements :\n");
while(ptr!= NULL)
{
printf("%d\n",ptr->info);
ptr = ptr->link;
}/*End of while */
}/*End of else*/
}/*End of display()*/
/*WAP to store a graph in computer memory using Linked List representation*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 5
struct node
{ int vertex;
struct node*next;
};
typedef struct node V;
V*adj[MAX];
void create (int);
void input (int);
void print (int);
void main()
{ int n;
clrscr();
printf("How many nodes in graph ?: \n");
scanf("%d",&n);
create (n);
input (n);
print (n);
getch();
}
void create(int n)
{ int i;
for(i=1;i<=n;i++)
adj[i]=NULL;
}
void input (int n)
{ V*p, *last;
int i,j,k,val;
for(i=1;i<=n;i++)
{ last=NULL;
printf("How many nodes in adjanceny list ?: \n");
scanf("%d",&k);
for (j=1;j<=k;j++)
{ printf("Enter the node: %d ",j);
scanf("%d",&val);
p=(V*)malloc(sizeof(V));
p->vertex=val;
p->next=NULL;
if(adj[i]==NULL)
adj[i]=last=p;
else
{ last->next=p;
last=p;
}
}
}
}
void print(int n)
{ V*p;
int i;
for(i=1;i<=n;i++)
{ p=adj[i];
printf("%d",i);
while(p!=NULL)
{ printf("->%d",p->vertex);
p=p->next;
}
printf("\n");
}
}
PROGRAM NO.
#include<stdio.h>
#include<conio.h>
#define MAX 20
void main()
{
int i,j,k,n;
int w_adj[MAX][MAX],adj[MAX][MAX],path[MAX][MAX];
clrscr();
printf("Enter number of vertices : ");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
path[i][j]=adj[i][j];
for(k=0;k<n;k++)
{
printf("P%d is :\n",k);
display(path,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
path[i][j]=( path[i][j] || ( path[i][k] && path[k][j] ) );
}
printf("Path matrix P%d of the given graph is :\n",k);
display(path,n);
getch();
}/*End of main() */
display(int matrix[MAX][MAX],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%3d",matrix[i][j]);
printf("\n");
}
}/*End of display()*/
PROGRAM NO.
/*WAP for insertion sort*/
#include<stdio.h>
#include<conio.h>
#define MAX 20
void main()
{
int arr[MAX],i,j,k,n;
clrscr();
printf("Enter the number of elements : ");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d", &arr[i]);
}
printf("Unsorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
/*Insertion sort*/
for(j=1;j<n;j++)
{
k=arr[j]; /*k is to be inserted at proper place*/
for(i=j-1;i>=0 && k<arr[i];i--)
arr[i+1]=arr[i];
arr[i+1]=k;
j--;
if( i < j)
swap(&list[i],&list[j]);
}
swap(&list[m],&list[j]);
quicksort(list,m,j-1);
quicksort(list,j+1,n);
}
}
void main()
{
int list[MAX], num;
clrscr();
printf("\n***** Enter the number of elements Maximum [10] *****\n");
scanf("%d",&num);
read_data(list,num);
printf("\n\nElements in the list before sorting are:\n");
print_data(list,num);
quicksort(list,0,num-1);
printf("\n\nElements in the list after sorting are:\n");
print_data(list,num);
getch();
}
PROGRAM NO.
/******************************************************************
PROGRAM SHOWING STATIC IMPLEMENTATION OF QUEUE HOLDING A STRING
OF CHARACTERS
'APPLE' WITH initialize(), enque(), deque() OPERATIONS
******************************************************************/
#include<conio.h>
#include<stdio.h>
#define SIZE 15
void main()
{
char string[SIZE],ch;
int i;
clrscr();
printf("\n Enter the string:::::::");
scanf("%s",string);
initialize(queue);
//adding
printf("Inserting in the queue..");
for(i=0;string[i]!='\0';i++)
enque(queue,string[i]);
//deleting
printf("\n\nDeleating from the queue..");
for(i=0;string[i]!='\0';i++)
{ ch=deque(queue);
printf("\n character deleted::%c",ch);
}
getch();
}
PROGRAM NO.
/******************************************************************
PROGRAM SHOWING STATIC IMPLEMENTATION OF STACK HOLDING A STRING
OF CHARACTER
'APPLE' WIYH push(), pop(), intitialize() OPERATIONS
******************************************************************/
#include<conio.h>
#include<stdio.h>
#define SIZE 15
char stack[SIZE];
int top;
void initialize( char stack[])
{ top=-1;
printf("Initializing................\n");
printf("\nstack is initialized");
}
void push(char stack[] ,char item)
{ if(top==SIZE-1)
printf("\noverflow");
else
top++;
stack[top]=item;
printf("\n");
putchar(item);
}
char pop(char stack[])
{ char item;
if(top==-1)
printf("\nunderflow");
else
{ item=stack[top];
top--;
}
return item;
}
void main()
{
char string[SIZE],ch;
int i;
clrscr();
printf("\n enter the string:::::::");
scanf("%s",string);
initialize(stack);
//pushing
for(i=0;string[i]!='\0';i++)
push(stack,string[i]);
//popping
printf("\nPoping out from the stack...........");
for(i=0;string[i]!='\0';i++)
{ ch=pop(stack);
printf("\n character deleted::%c",ch);
}
getch();
}
PROGRAM NO.
/*WAP for selection sort*/
#include <stdio.h>
#include <conio.h>
#define MAX 20
void main()
{
int arr[MAX], i,j,k,n,temp,smallest;
clrscr();
printf("Enter the number of elements : ");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d", &arr[i]);
}
printf("Unsorted list is : \n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
/*Selection sort*/
for(i = 0; i< n - 1 ; i++)
{
/*Find the smallest element*/
smallest = i;
for(k = i + 1; k < n ; k++)
{
if(arr[smallest] > arr[k])
smallest = k ;
}
if( i != smallest )
{
temp = arr [i];
arr[i] = arr[smallest];
arr[smallest] = temp ;
}
printf("After Pass %d elements are : ",i+1);
for (j = 0; j < n; j++)
printf("%d ", arr[j]);
printf("\n");
}
printf("Sorted list is : \n");
# include<stdio.h>
# include<malloc.h>
struct node
{
int info;
struct node *link;
}*front=NULL,*rear=NULL;
main()
{
int choice;
while(1)
{ printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
insert()
{
struct node *tmp;
int added_item;
tmp = (struct node *)malloc(sizeof(struct node));
printf("Input the element for adding in queue : ");
scanf("%d",&added_item);
tmp->info = added_item;
tmp->link=NULL;
if(front==NULL) /*If Queue is empty*/
front=tmp;
else
rear->link=tmp;
rear=tmp;
}/*End of insert()*/
del()
{
struct node *tmp;
if(front == NULL)
printf("Queue Underflow\n");
else
{
tmp=front;
printf("Deleted element is %d\n",tmp->info);
front=front->link;
free(tmp);
}
}
display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
printf("Queue is empty\n");
else
{
printf("Queue elements :\n");
while(ptr != NULL)
{
printf("%d ",ptr->info);
ptr = ptr->link;
}
printf("\n");}/*End of display()*/