Friday, October 2, 2009

ADA 5th sem lab prgms

Prgm no 5b
#include
#include
#include
int size, array[5][5],p,q,r;
void algorithm()
{
for(r=1;r<=size;++r)
for(p=1;p<=size;++p)
for(q=1;q<=size;++q)
if((array[p][r]+array[r][q]) < array[p][q])
array[p][q]=array[p][r]+array[r][q];
}
void main()
{
printf("enter the size of graph");
scanf("%d",&size);
printf("enter graph data");
for(p=1;p<=size;++p)
for(q=1;q<=size;++q)
{
scanf("%d",&array[p][q]);
if(array[p][q]==0)array[p][q]=999;
}
algorithm();
printf("shortest path");
for(p=1;p<=size;++p)
{
for(q=1;q<=size;++q)
printf("/t %d",array[p][q]);
printf("/n");
}
}

ADA 5th sem lab prgms

/* Program No. 5a
Write A C Program to find the topological ordering of vertices in the given grapgh*/
#include
#include
void main()
{
int p,q,r,vertices,num[10][10],x[10],t[10],calc=0;
printf("Enter the number of vertices: ");
scanf("%d",&vertices);
printf("Enter the Adjacency Matrix:\n");
for(p=1;p<=vertices;p++)
for(q=1;q<=vertices;q++)
scanf("%d",&num[p][q]);
for(p=1;p<=vertices;p++)
x[p]=0,t[p]=0;
for(p=1;p<=vertices;p++)
for(q=1;q<=vertices;q++)
x[p]+=num[q][p];
printf("The Topological order is: \n");
while(calc {
for(r=1;r<=vertices;r++)
if((x[r]==0)&&(t[r]==0))
{
printf("%d\t",r);
t[r]=1;
calc++;
for(p=1;p<=vertices;p++)
if(num[r][p]==1)
x[p]--;
}
}
}
/* Output:
Enter the number of vertices: 6
Enter the Adjacency Matrix:
0 0 1 1 0 0
0 0 0 1 1 0
0 0 0 1 0 1
0 0 0 0 0 1
0 0 0 0 0 1
0 0 0 0 0 0
The Topological order is:
1 2 3 4 5 6
...*/



ADA 5th sem lab prgms

/* Program No: 4.
Sort a given set of elements using the Selection Sort method..*/
#include
#include
int x[10], num;
void algorithm()
{
int m,n,situation,low,var;
for(m=0;m {
low=x[m];
situation=m;
for(n=m+1;n if(x[n] {
low=x[n],situation=n;
var=x[situation];
x[situation]=x[m];
x[m]=var;
}
}
}
void main()
{
int m,n;
printf("ENTER THE NUMBER OF ELEMENTS\n");
scanf("%d",&num);
printf("----------------------------------\n");
printf("ENTER THE %d ELEMENTS\n",num);
printf("----------------------------------\n");
for(m=0;m scanf("%d",&x[m]);
algorithm();
printf("-------------------------------------\n");
printf("THE SORTED ELEMENTS ARE\n");
printf("-------------------------------------\n");
for(m=0;m printf("%d\n",x[m]);
printf("***********************\n");
getche();
}
/*
Output:
ENTER THE NUMBER OF ELEMENTS
4
----------------------------------
ENTER THE 5 ELEMENTS
----------------------------------
20
10
40
30
-------------------------------------
THE SORTED ELEMENTS ARE
-------------------------------------
10
20
30
40
***********************
*/

ADA 5th sem lab prgms

Prgm no.3
/*Sort a given set of elements using the Merge Sort method..*/
#include
#include
int num[10], x[10];
void algorithm(int small, int s, int big)
{
int m,n,p;
p=small;
m=small;
n=s+1;
while((m<=s)&&(n<=big))
{
if(x[m] num[p++]=x[m++];
else
num[p++]=x[n++];
}
while(m<=s)
num[p++]=x[m++];
while(n<=big)
num[p++]=x[n++];
for(m=small;m<=p-1;m++)
x[m]=num[m];
}
void sort(int small,int big)
{
int s;
if(small {
s=(small+big)/2;
sort(small,s);
sort(s+1,big);
algorithm(small,s,big);
}
}
void main()
{
int i,n;
clrscr();
printf("Enter the number of Elements\n");
scanf("%d",&n);
printf("Enter the Elements\n");
for(i=0;i scanf("%d",&x[i]);
sort(0,n-1);
printf("The Sorted Element are\n");
for(i=0;i printf("%d\n",num[i]);
getche();
}

/*
Output:
Enter the number of Elements
5
Enter the Elements
20
10
50
40
30
The Sorted Element are
10
20
30
40
50
*/

ADA 5th sem lab prgms

/* Program No: 02.
Sort a given set of elements using the Heap Sort method..*/
/* C program to sort a given list of numbers using Heap Sort*/
#include
#include
int num[50], elements=0, old, new;
int highest(int num[])
{
int m, max=0, index;
for(m=1;m<=elements+1;m++)
if(max max=num[m],index=m;
num[index]=0;
return max;
}
int compare(int a, int b)
{
if(num[a]==0&&num[b]==0)
return -1;
if(num[a]>num[b])
return 0;
else
return -1;
}
void exchange(int a, int b)
{
int temp;
temp=num[a];
num[a]=num[b];
num[b]=temp;
}
void insert(int new)
{
int k;
elements++;
num[elements]=new;
k=elements;
while(k>1)
{
old=k/2;
if(compare(k,old)>=0)
break;
exchange(k,old);
k=old;
}
}
void rem()
{
int k=1,new;
printf("%d\n",num[1]);
num[1]=highest(num);
while((2*k)<=elements)
{
if(num[2*k]==0||num[2*k+1]==0)
{
if(num[2*k]==0)
new=2*k+1;
else
new=2*k;
}
else
if(compare(2*k,2*k+1)<0)
new=2*k;
else
new=2*k+1;
if(compare(k,new)<0)
break;
exchange(k,new);
k=new;
}
}
void main()
{
int a,i,n,par=0;
clrscr();
printf("input the number of elements\n");
scanf("%d",&n);
printf("input the %d elements\n",n);
for(i=0;i {
scanf("%d",&a);
insert(a);
}
printf("sorted order is\n");
for(i=0;i rem();
par=highest(num);
printf("%d\n",par);
getche();
}

/* Output:
input the number of elements
5
inter the 5 elements
64
24
46
84
28
sorted order is
24
28
46
64
84
..........*/

ADA 5th sem lab prgms

/* Program No. 1 (b)
C Program to search an element using Linear Search and to find the time taken to search an element..*/
#include
#include
#include
int num[20], max, element, m;
void ls(int m)
{
if(element==num[m])
{
printf("Element is available at the position: %d\n", m+1);
return;
}
if(element>=max)
{
printf("Element is not available\n");
return;
}
ls(m+1);
}
void main()
{
clock_t start, end;
clrscr();
printf("Enter the array size\n");
scanf("%d",&max);
printf("Enter the array elements\n");
for(m=0;m
scanf("%d",&num[m]);
printf("Enter the element to be searched\n");
scanf("%d",&element);
start=clock();
ls(0);
end=clock();
printf("Time taken for linear search is %f",(end-start)/CLK_TCK);
}