#include< stdio.h >
#include< conio.h >
//global declaration
int a[20],n,i,rear,front,x;
void main()
{ clrscr();
printf("Enter Total Number of Elements = ");
scanf("%d",&n);
//initialise
rear=0;
front=0;
//insert element
while(rear < n)
{ //check for Overflow
if(rear > n)
{
printf("\nQueue Full");
exit();
}
else
{
//enter element
printf("\nEnter Element a[%d] = ",rear);
scanf("%d",&x);
a[rear]=x;
//reset rear
rear=rear+1;
}
}
//Traverse Stack
printf("\n***** Elements of Queue Are ***** \n");
while(i < rear)
{
printf("_____\n");
printf("| %d |\n",a[i]);
i=i+1;
}
printf("_____\n");
getch();
//Delete Element of Queue
while(front < n)
{
printf("\nElement Deleted from stack a[%d] = %d ",front,a[front]);
//reset front
front=front+1;
}
printf("\nQueue Empty");
getch();
}
Saturday, May 29, 2010
Queue - Insertion Operation
#include< stdio.h >
#include< conio.h >
//global declaration
int a[20],n,i,rear,front,x;
void main()
{ clrscr();
printf("Enter Total Number of Elements = ");
scanf("%d",&n);
//initialise
rear=0;
front=0;
//insert element
while(rear < n)
{ //check for Overflow
if(rear > n)
{
printf("\nQueue Full");
exit();
}
else
{
//enter element
printf("\nEnter Element a[%d] = ",rear);
scanf("%d",&x);
a[rear]=x;
//reset rear
rear=rear+1;
}
}
//Traverse Stack
printf("\n***** Elements of Queue Are ***** \n");
while(i < rear)
{
printf("_____\n");
printf("| %d |\n",a[i]);
i=i+1;
}
printf("_____\n");
getch();
}
#include< conio.h >
//global declaration
int a[20],n,i,rear,front,x;
void main()
{ clrscr();
printf("Enter Total Number of Elements = ");
scanf("%d",&n);
//initialise
rear=0;
front=0;
//insert element
while(rear < n)
{ //check for Overflow
if(rear > n)
{
printf("\nQueue Full");
exit();
}
else
{
//enter element
printf("\nEnter Element a[%d] = ",rear);
scanf("%d",&x);
a[rear]=x;
//reset rear
rear=rear+1;
}
}
//Traverse Stack
printf("\n***** Elements of Queue Are ***** \n");
while(i < rear)
{
printf("_____\n");
printf("| %d |\n",a[i]);
i=i+1;
}
printf("_____\n");
getch();
}
Stack - POP Operation
//POP OPERATION of Stack
#include< stdio.h >
#include< conio.h >
//global declaration
int a[20],n,top,x,i;
void main()
{ clrscr();
printf("Enter Total Number of Elements = ");
scanf("%d",&n);
//initialise TOP
top=0;
//create and insert element in stack
while(top < n)
{ //check for Overflow
if(top > n)
{
printf("\nStack Full");
exit();
}
else
{
//enter element
printf("\nEnter Element a[%d] = ",top);
scanf("%d",&x);
a[top]=x;
top=top+1;
}
}
i=top-1;
//Traverse Stack
printf("\n***** Elements of Stack Are ***** \n");
while(i >= 0)
{
printf("_____\n");
printf("| %d |\n",a[i]);
i=i-1;
}
printf("_____\n");
//Delete Element of Stack
top=top-1;
while(top >= 0)
{
printf("\nElement Deleted from stack a[%d] = %d ",top,a[top]);
top=top-1;
}
printf("\nStack Empty");
getch();
}
#include< stdio.h >
#include< conio.h >
//global declaration
int a[20],n,top,x,i;
void main()
{ clrscr();
printf("Enter Total Number of Elements = ");
scanf("%d",&n);
//initialise TOP
top=0;
//create and insert element in stack
while(top < n)
{ //check for Overflow
if(top > n)
{
printf("\nStack Full");
exit();
}
else
{
//enter element
printf("\nEnter Element a[%d] = ",top);
scanf("%d",&x);
a[top]=x;
top=top+1;
}
}
i=top-1;
//Traverse Stack
printf("\n***** Elements of Stack Are ***** \n");
while(i >= 0)
{
printf("_____\n");
printf("| %d |\n",a[i]);
i=i-1;
}
printf("_____\n");
//Delete Element of Stack
top=top-1;
while(top >= 0)
{
printf("\nElement Deleted from stack a[%d] = %d ",top,a[top]);
top=top-1;
}
printf("\nStack Empty");
getch();
}
Stack - PUSH Operation
#include< stdio.h >
#include< conio.h >
//global declaration
int a[20],n,top,x;
void main()
{ clrscr();
printf("Enter Total Number of Elements = ");
scanf("%d",&n);
//initialise TOP
top=0;
//insert element
while(top < n)
{ //check for Overflow
if(top > n)
{
printf("\nStack Full");
exit();
}
else
{
//enter element
printf("\nEnter Element a[%d] = ",top);
scanf("%d",&x);
a[top]=x;
top=top+1;
}
}
top=top-1;
//Traverse Stack
printf("\n***** Elements of Stack Are ***** \n");
while(top >= 0)
{
printf("_____\n");
printf("| %d |\n",a[top]);
top=top-1;
}
printf("_____\n");
getch();
}
#include< conio.h >
//global declaration
int a[20],n,top,x;
void main()
{ clrscr();
printf("Enter Total Number of Elements = ");
scanf("%d",&n);
//initialise TOP
top=0;
//insert element
while(top < n)
{ //check for Overflow
if(top > n)
{
printf("\nStack Full");
exit();
}
else
{
//enter element
printf("\nEnter Element a[%d] = ",top);
scanf("%d",&x);
a[top]=x;
top=top+1;
}
}
top=top-1;
//Traverse Stack
printf("\n***** Elements of Stack Are ***** \n");
while(top >= 0)
{
printf("_____\n");
printf("| %d |\n",a[top]);
top=top-1;
}
printf("_____\n");
getch();
}
Friday, May 28, 2010
Binary Search Method
#include< stdio.h>
#include < conio.h>
//global declaration of varaible
int a[20],ub,lb,mid, i,n, s,found=0;
void main()
{
printf("\nEnter Total Number of Elements =");
scanf("%d",&n);
//enter array elements
for(i=0;i< n;i=i+1)
{
scanf("%d",&a[i]);
}
printf("\nEnter Element to be Searched = ");
scanf("%d",&s);
//initialise
lb=0;
ub=n-1;
mid=(lb+ub)/2;
//search data
while (lb < =ub)
{
if (a[mid]= =s)
{
found=1;
printf("Element %d found at postion %d,",s,mid+1);
break;
}
if(a[mid] < s)
{
lb=mid+1;
}
if(s < a[mid])
{
ub=mid-1;
}
mid=(lb+ub)/2;
}
if(found= =0)
{
printf("\n Element Not Found.");
}
}
#include < conio.h>
//global declaration of varaible
int a[20],ub,lb,mid, i,n, s,found=0;
void main()
{
printf("\nEnter Total Number of Elements =");
scanf("%d",&n);
//enter array elements
for(i=0;i< n;i=i+1)
{
scanf("%d",&a[i]);
}
printf("\nEnter Element to be Searched = ");
scanf("%d",&s);
//initialise
lb=0;
ub=n-1;
mid=(lb+ub)/2;
//search data
while (lb < =ub)
{
if (a[mid]= =s)
{
found=1;
printf("Element %d found at postion %d,",s,mid+1);
break;
}
if(a[mid] < s)
{
lb=mid+1;
}
if(s < a[mid])
{
ub=mid-1;
}
mid=(lb+ub)/2;
}
if(found= =0)
{
printf("\n Element Not Found.");
}
}
Thursday, May 27, 2010
Linear Search Method
#include< stdio.h>
#include < conio.h>
//global declaration of varaible
int a[20], i,n, s,found=0;
void main()
{
printf("\nEnter Total Number of Elements =");
scanf("%d",&n);
//enter array elements
for(i=0;i < n;i=i+1)
{
scanf("%d",&a[i]);
}
printf("\nEnter Element to be Searched = ");
scanf("%d",&s);
//Search Element Using Linear Search
for(i=0;i < n;i=i+1)
{
if(a[i]= =s)
{
printf("\nElement %d found at postion %d.", s,i+1);
found=1;
break;
}
}
if (found= =0)
{
printf("Element Not Found");
}
getch();
}
#include < conio.h>
//global declaration of varaible
int a[20], i,n, s,found=0;
void main()
{
printf("\nEnter Total Number of Elements =");
scanf("%d",&n);
//enter array elements
for(i=0;i < n;i=i+1)
{
scanf("%d",&a[i]);
}
printf("\nEnter Element to be Searched = ");
scanf("%d",&s);
//Search Element Using Linear Search
for(i=0;i < n;i=i+1)
{
if(a[i]= =s)
{
printf("\nElement %d found at postion %d.", s,i+1);
found=1;
break;
}
}
if (found= =0)
{
printf("Element Not Found");
}
getch();
}
Bubble Sort Method
//Program to Sort data in Ascending order using Bubble Sort Method
#include< stdio.h>
#include< conio.h>
void main()
{
//variable declaration
int a[50],i,j,n,temp;
clrscr();
printf("Enter Total Number of Elements :: ");
scanf("%d",&n);
printf("\n Enter Elements :: ");
//Input array element
for(i=0;i< n;i=i+1)
{
scanf("%d",&a[i]);
}
//Print Unsorted List
printf("\n Unsorted List :: \n");
for(i=0;i < n;i=i+1)
{
printf("\t%d",a[i]);
}
//Sort data in ascending order
for(i=0;i < n;i++)
{
for(j=0;j < n-i+1;j=j+1)
{
if (a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
//Print Sorted List
printf("\n Sorted List :: \n");
for(i=0;i < n;i=i+1)
{
printf("\t%d",a[i]);
}
getch();
}
Insertion Sort Method
// Program to sort data in Ascending Order Using Insertion Sort Method
#include< stdio.h >
#include< conio.h >
void main()
{
//variable declaration
int a[10],i,j,n,temp;
clrscr();
printf("Enter Total Number of Elements :: ");
scanf("%d",&n);
printf("\n Enter Elements :: ");
//Input array element
for(i=0;i< n;i=i+1)
{
scanf("%d",&a[i]);
}
//Print Unsorted List
printf("\n Unsorted List :: \n");
for(i=0;i< n;i=i+1)
{
printf("\t%d",a[i]);
}
//Sort data in ascending order
for(i=1;i < = n;i++)
{
temp=a[i];
j=i-1;
while ((temp < a[j]) && (j >=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
//Print Sorted List
printf("\n Sorted List :: \n");
for(i=0;i < n;i=i+1)
{
printf("\t%d",a[i]);
}
getch();
}
Selection Sort Method
// Program to Sort Data in ascending order using Selection Sort Method
#include< stdio.h >
#include< conio.h >
void main()
{
//variable declaration
int a[10],i,j,n,temp,small,position;
clrscr();
printf("Enter Total Number of Elements :: ");
scanf("%d",&n);
//Input array element
printf("\n Enter Elements :: ");
for(i=0;i< n;i=i+1)
{
scanf("%d",&a[i]);
}
//Print Unsorted List
printf("\n Unsorted List :: \n");
for(i=0;i< n;i=i+1)
{
printf("\t%d",a[i]);
}
//Sort data in ascending order
for(i=0;i< =n;i++)
{ small=a[i];
position=i;
for(j=i;j< =n;j=j+1)
{
// find smallest element in the range i to n
if (a[j]< small)
{
small=a[j];
position=j;
}
}
// interchange elements
temp=a[i];
a[i]=a[position];
a[position]=temp;
}
//Print Sorted List
printf("\n Sorted List :: \n");
for(i=0;i< n;i=i+1)
{
printf("\t%d",a[i]);
}
getch();
}
Program to Find Address of Element in Two Dimensional Array
#include< stdio.h >
#include< conio.h >
//global declaration of all variables
int a[5][5],i,j,m,n,c,r,l1,u1,l2,u2,eadd,badd,ch,yes=0;
//function declaration
void insert();
void row();
void column();
void main()
{
clrscr();
printf("\nEnter Lower Bound of ROW (L1) = ");
scanf("%d",&l1);
printf("\nEnter Upper Bound of ROW (U1) = ");
scanf("%d",&u1);
printf("\nEnter Lower Bound of COLUMN (L2) = ");
scanf("%d",&l2);
printf("\nEnter Upper Bound of COLUMN (U2) = ");
scanf("%d",&u2);
//calculate total elements in row
m=u1-l1+1;
//calculate total elements in columns
n=u2-l2+1;
do
{
clrscr();
printf("\n...............................................");
printf(" \n 1 to Enter Array Elements");
printf(" \n 2 to Find Address of Element in Row Major");
printf(" \n 3 to Find Address of Element in Column Major");
printf(" \n 4 to Exit");
printf("\n...............................................");
printf(" \n Enter Your Choice = ");
scanf("%d",&ch);
switch(ch)
{
case 1:insert(); //call of insert () function
printf("\n\nPress Any Key to Continue");
getch();
yes=1;
break;
case 2: row(); // call of function row() to find address in row major
printf("\n\nPress Any Key to Continue");
getch();
yes=1;
break;
case 3: column(); // call of function column() to find address in column major
printf("\n\nPress Any Key to Continue");
getch();
yes=1;
break;
case 4: yes=0;
break;
}
} while(yes==1);
getch();
}
//Function to Insert element in Array
void insert()
{ //enter array elements
printf("\nEnter Array Elemnts \n");
for(r=l1;r < u1;r=r+1)
{
for(c=l2;c < u2;c=c+1)
{
scanf("%d",&a[r][c]);
}
}
}
//Function to Calculate address of element a[i][j] in ROW major
void row()
{
// Enter postion i and j
printf("\n Address of element \n");
printf("\n i = ");
scanf("%d",&i);
printf("\n j = ");
scanf("%d",&j);
//find base address
badd=(int)&a;
//calculate address of element a[i][j] in row major
eadd=badd + (sizeof(int)*((n * (i-l1)) + (j-l2)));
printf("\n\nAddress of element a[%d][%d] = %d", i,j,eadd);
}
//Function to Calculate address of element a[i][j] in COLUMN major
void column()
{
// Enter postion i and j
printf("\n Address of element \n");
printf("\n i = ");
scanf("%d",&i);
printf("\n j = ");
scanf("%d",&j);
//find base address
badd=(int)&a;
//calculate address of element a[i][j] in column major
eadd=badd + (sizeof(int)*((m * (j-l2)) + (i-l1)));
printf("\n\nAddress of element a[%d][%d] = %d", i,j,eadd);
}
#include< conio.h >
//global declaration of all variables
int a[5][5],i,j,m,n,c,r,l1,u1,l2,u2,eadd,badd,ch,yes=0;
//function declaration
void insert();
void row();
void column();
void main()
{
clrscr();
printf("\nEnter Lower Bound of ROW (L1) = ");
scanf("%d",&l1);
printf("\nEnter Upper Bound of ROW (U1) = ");
scanf("%d",&u1);
printf("\nEnter Lower Bound of COLUMN (L2) = ");
scanf("%d",&l2);
printf("\nEnter Upper Bound of COLUMN (U2) = ");
scanf("%d",&u2);
//calculate total elements in row
m=u1-l1+1;
//calculate total elements in columns
n=u2-l2+1;
do
{
clrscr();
printf("\n...............................................");
printf(" \n 1 to Enter Array Elements");
printf(" \n 2 to Find Address of Element in Row Major");
printf(" \n 3 to Find Address of Element in Column Major");
printf(" \n 4 to Exit");
printf("\n...............................................");
printf(" \n Enter Your Choice = ");
scanf("%d",&ch);
switch(ch)
{
case 1:insert(); //call of insert () function
printf("\n\nPress Any Key to Continue");
getch();
yes=1;
break;
case 2: row(); // call of function row() to find address in row major
printf("\n\nPress Any Key to Continue");
getch();
yes=1;
break;
case 3: column(); // call of function column() to find address in column major
printf("\n\nPress Any Key to Continue");
getch();
yes=1;
break;
case 4: yes=0;
break;
}
} while(yes==1);
getch();
}
//Function to Insert element in Array
void insert()
{ //enter array elements
printf("\nEnter Array Elemnts \n");
for(r=l1;r < u1;r=r+1)
{
for(c=l2;c < u2;c=c+1)
{
scanf("%d",&a[r][c]);
}
}
}
//Function to Calculate address of element a[i][j] in ROW major
void row()
{
// Enter postion i and j
printf("\n Address of element \n");
printf("\n i = ");
scanf("%d",&i);
printf("\n j = ");
scanf("%d",&j);
//find base address
badd=(int)&a;
//calculate address of element a[i][j] in row major
eadd=badd + (sizeof(int)*((n * (i-l1)) + (j-l2)));
printf("\n\nAddress of element a[%d][%d] = %d", i,j,eadd);
}
//Function to Calculate address of element a[i][j] in COLUMN major
void column()
{
// Enter postion i and j
printf("\n Address of element \n");
printf("\n i = ");
scanf("%d",&i);
printf("\n j = ");
scanf("%d",&j);
//find base address
badd=(int)&a;
//calculate address of element a[i][j] in column major
eadd=badd + (sizeof(int)*((m * (j-l2)) + (i-l1)));
printf("\n\nAddress of element a[%d][%d] = %d", i,j,eadd);
}
Program to Find Address of Element in One Dimensional Array
#include < stdio.h >
#include < conio.h >
void main()
{
int a[10], i, badd,n,pos, eadd;
clrscr();
printf("Enter size of Array = ");
scanf("%d",&n);
printf("\n Enter Array Elements \n");
for ( i=0;i
{
scanf("%d",&a[i]);
}
printf("Address of element = ");
scanf("%d",&pos);
//find base address and convert address in int type
badd=(int)&a;
// find the address of element
eadd=badd + sizeof(int) * pos;
printf("Base Address = %d \nSize of Data Type = %d \nAddress of Element = %d\n", badd, sizeof(int), pos);
printf("Address of element a[%d] = %d",pos,eadd);
getch();
}
#include < conio.h >
void main()
{
int a[10], i, badd,n,pos, eadd;
clrscr();
printf("Enter size of Array = ");
scanf("%d",&n);
printf("\n Enter Array Elements \n");
for ( i=0;i
{
scanf("%d",&a[i]);
}
printf("Address of element = ");
scanf("%d",&pos);
//find base address and convert address in int type
badd=(int)&a;
// find the address of element
eadd=badd + sizeof(int) * pos;
printf("Base Address = %d \nSize of Data Type = %d \nAddress of Element = %d\n", badd, sizeof(int), pos);
printf("Address of element a[%d] = %d",pos,eadd);
getch();
}
Subscribe to:
Comments (Atom)