0% found this document useful (0 votes)
137 views31 pages

Program No. / Binary Search With Time Calculation

The document contains code for multiple C programs demonstrating different programming concepts: 1) The first program performs a binary search on an array and calculates the time taken. 2) The second program creates a database of Commonwealth countries with country names and capital cities. 3) Subsequent programs demonstrate sequential search, escape sequences, conditional operators, typedef, data type sizes, menu driven programs, switch statements, linked lists for polynomials, and checking for leap years.

Uploaded by

Utkarsh Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views31 pages

Program No. / Binary Search With Time Calculation

The document contains code for multiple C programs demonstrating different programming concepts: 1) The first program performs a binary search on an array and calculates the time taken. 2) The second program creates a database of Commonwealth countries with country names and capital cities. 3) Subsequent programs demonstrate sequential search, escape sequences, conditional operators, typedef, data type sizes, menu driven programs, switch statements, linked lists for polynomials, and checking for leap years.

Uploaded by

Utkarsh Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 31

PROGRAM NO.

/*Binary search with time calculation */


#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<process.h>
void main()
{ int a[1000],i,n,pos,elt;
int first,last,mid; //for time calculation
float var;
char ch;
int clock_t,start,end;
clrscr();
printf("Enter the size of the array(max:1000):");
scanf("%d",&n);
printf("\nThe array is:");
for(i=0;i<n;i++)
{a[i]=2*i;
printf("%4d",a[i]);
}
do
{ first=0;
pos=-1;
last=n-1;

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.

/*Database of commonwealth countries*/


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ char con[11][15]={"","Australia","Bangladesh","Canada","India","Kenya","Malaysia","New
Zealand","Pakistan","Sri Lanka","South Africa"};
char cap[11][15]={"Canberra","Dhaka","Ottawa","New Delhi","Nairobi","Kuala
Lumpur","Wellington","Islamabad","Colombo","Cape Town"};
int i,opt;
char ch;
clrscr();
do
{ printf("\t\t Database of Common Wealth Countries ");
printf("\n ---------------------------------------------------------------------------------------");
printf("\n | S.no.| Common Wealth Countries | Country Code |");
printf("\n ---------------------------------------------------------------------------------------");
for(i=1;i<11;i++)
{ printf(" | %d",i);
printf("\t|\t%s\n",con[i]);
printf("\t\t\t\t\t\t\t |\t %d\t |",i);
}
printf("\n ---------------------------------------------------------------------------");
printf("\nenter ur option:(1-10)\n");
scanf("%d",&opt);
for(i=0;i<11;i++)
{if(opt==(i))
printf("the capital of %s is %s\n",con[i],cap[i-1]);
}
puts("wanna continue?");
fflush(stdin);
ch=getchar();
fflush(stdin);
}
while((ch!='n')&&(ch!='N'));
getch();
}
PROGRAM NO.

/*Write a program for sequential searching*/


# include<stdio.h>
#include<conio.h>
void main()
{
int arr[20],n,i,item;
clrscr();
printf("How many elements you want to enter in the array : ");
scanf("%d",&n);

for(i=0; i < n;i++)


{
printf("Enter element %d : ",i+1);
scanf("%d", &arr[i]);
}
printf("Enter the element to be searched : ");
scanf("%d",&item);
for(i=0;i < n;i++)
{
if(item == arr[i])
{
printf("%d found at position %d\n",item,i+1);
break;
}
}/*End of for*/
if(i == n)
printf("Item %d not found in array\n",item);
getch();
}
PROGRAM NO.

/*Program to show usage of escape sequences ( \t )Tab, ( \b )


Backspace, ( \n ) New line.*/

#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.

/*WAP to compare two number using Conditional Operator*/

#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);

printf ("\nThe greatest number is %d",num1);

getchar();
getchar();

}
PROGRAM NO.

/*WAP showing the use of typedef function*/


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
typedef int Id;
typedef float salary;
typedef char *string;
Id comp;
salary pay;
string comp_name=" RELIANCE INFRASTRUCTURE ";
clrscr();
printf("\nEnter your Company ID no.: ");
scanf("%d",&comp);
printf("\nEnter your Payment: ");
scanf("%f",&pay);
printf("\nDetails are:");
printf("\n\nCompany:- ");
printf(comp_name);
printf("\nCompany ID:- %d \nPayment:- %f ",comp,pay);
getch();
}
PROGRAM NO.

/*Display size of Different data types using Sizeof() function*/


#include<stdio.h>
#include<conio.h>
void main()
{
int a;
char c;
float r;
double d;
clrscr();
printf("\nSize of varios DATA types are ");
printf("\n\n int is %d bytes ", sizeof(a));
printf("\n Char is %d bytes ", sizeof(c));
printf("\n Float is %d bytes ", sizeof(r));
printf("\n Double is %d bytes ", sizeof(d));
getch();
}
PROGRAM NO.

/*A Menu Driven program*/


#include<stdio.h>
#include<conio.h>
void main()
{
int ch;
char choice;
clrscr();
printf("\nmenu \noption 1: 1 \n");
printf("\noption 2: 2\n \noption 3: 3 \n \noption 4: 4 \n");
repeat:
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\none is alone");
break;
case 2:
printf("\ntwo is company");
break;
case 3:
printf("\nthree is crowd");
break;
case 4:
printf("\nquit");
break;
default:
printf("\nwrong entry");
break;
}
printf("\nHit y if you wish to continue:");
choice=getch();
printf("%c",choice);
if(choice=='y')
goto repeat;

getch();

}
PROGRAM NO.

/* PROGRAM TO FIND ANGLE QUADRANT USING SWITCH*/

#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.

/* PROGRAM TO EXECUTE SINGLE LINKED LIST WITH


STORING A POLYNOMIAL*/

#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);
}

printf("\nTHE POLYNOMIAL IS ::::");


display(start);
getch();
}
PROGRAM NO.

/*WAP using if-else to check leap year or not*/


#include<stdio.h>
#include<conio.h>
void main()
{ int y,ch;
clrscr();
leap:
printf("\nEnter the desired year: ");
scanf("%d",&y);
if(y%4==0)
printf("\nThe year %d is a leap year ",y);
else
printf("\nThe Year %d is not a leap year ",y);
printf("\nDo you want to continue(Y/N)?");
ch=getch();
printf("%c",ch);
if(ch!='y')
printf("\n\nEXIT");
else
goto leap;
getch();
}
PROGRAM NO.
/* Program of stack using linked list*/

# 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.

/*WAP to store a graph in computer memory using adjacency matrix


representation and then finding the path matrix using warshalls algorithm*/

#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);

printf("Enter weighted adjacency matrix :\n");


for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&w_adj[i][j]);

printf("The weighted adjacency matrix is :\n");


display(w_adj,n);

/* Make weighted adjacency matrix into boolean adjacency matrix*/


for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(w_adj[i][j]==0)
adj[i][j]=0;
else
adj[i][j]=1;

printf("The adjacency matrix is :\n");


display(adj,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;

printf("Pass %d, Element inserted in proper place: %d\n",j,k);


for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
printf("Sorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
getch();
}
PROGRAM NO.

/*WAP to implement Quick sort*/

include < stdio.h >


#include<conio.h>
#define MAX 10

void swap(int *m,int *n)


{
int temp;
temp = *m;
*m = *n;
*n = temp;
}
int get_key_position(int x,int y )
{
return((x+y) /2);
}

// Function for Quick Sort


void quicksort(int list[],int m,int n)
{
int key,i,j,k;
if( m < n)
{
k = get_key_position(m,n);
swap(&list[m],&list[k]);
key = list[m];
i = m+1;
j = n;
while(i <= j)
{
while((i <= n) && (list[i] <= key))
i++;
while((j >= m) && (list[j] > key))

j--;
if( i < j)
swap(&list[i],&list[j]);
}
swap(&list[m],&list[j]);
quicksort(list,m,j-1);
quicksort(list,j+1,n);
}
}

// Function to read the data


void read_data(int list[],int n)
{
int j;
printf("\n\nEnter the elements:\n");
for(j=0;j < n;j++)
scanf("%d",&list[j]);
}

// Function to print the data


void print_data(int list[],int n)
{
int j;
for(j=0;j < n;j++)
printf("%d\t",list[j]);
}

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 TO CREATE A FILE*/


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{ char name[20];
char branch[20];
int id;
char opt;
FILE *fp;
clrscr();
fp=fopen("stud.dat","a"); //opening a file stud.dat in append mode
if(fp==NULL)
{
printf("unable to open!!!");
exit(0);
}
do
{ printf("\nEnter name, branch, id of the student:\n");
scanf("%s%s%d",&name,&branch,&id);
fprintf(fp,"%s%s%d\n",name,branch,id); //writing in the file

printf("do u wanna add more?(y/n):");


scanf("%s",&opt);
}
while(opt=='y');
fclose(fp); //closing the file stud.dat
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

char queue[SIZE]; //global declaration


int front,rear;
void initialize( char queue[]) //function to initialize the array
{ front=rear=-1;
printf("\nQueue is initializing..............\n");
printf("\nQueue is initialized\n");
}
void enque(char queue[] ,char item) //inserting in the queue
{ if(rear==SIZE-1) // checking whether insertion can be done or not
printf("\noverflow");
else
rear++;
queue[rear]=item;
printf("\n");
putchar(item);
}
char deque(char queue[]) //function deleating from the queue
{ char item;
if(front==rear==-1) //checking whether deletion can be done or not
printf("\nunderflow");
else
{
front++;
item=queue[front];
}
return item;
}

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");

for (i = 0; i < n; i++)


printf("%d ", arr[i]);
printf("\n");
getch();
}
PROGRAM NO.

/* Program of queue using linked list*/

# 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()*/

You might also like