Ada Final Lab PG
Ada Final Lab PG
ADA LABORATORY
MCA(4TH SEM) LAB PROGRAM
ALOK KUMAR(4NI09MCA02)
3/5/2011
1
http://www.google.co.in/imgres?imgurl=http://images.htcampus.com/media/uploads/images/institute
/newtons-instituteengineering/logo_NIE.80x80.jpg&imgrefurl=http://www.htcampus.com/institute/newtons-instituteengineering/&usg=__BxaijnkAy8u3FJK3t1snoh4Bpos=&h=80&w=80&sz=4&hl=en&start=0&sig2=N0PFPi
1K-ptFaWP5iz43tg&zoom=1&tbnid=X9JAaE7PBKlU3M:&tbnh=74&tbnw=74&ei=OLa6TcaFFYuvgOH_sy1BQ&prev=/search%3Fq%3DNIE%2Bcollege%2Bmysore%2Blogo%26um%3D1%26hl%3Den%26
sa%3DN%26biw%3D1366%26bih%3D575%26tbm%3Disch&um=1&itbs=1&iact=hc&vpx=850&vpy=213&
dur=15&hovh=74&hovw=74&tx=81&ty=39&page=1&ndsp=20&ved=1t:429,r:17,s:0
Pg6.
#include<stdio.h>
int max(int a,int b)
{
return a>b ? a:b;
}
void knapsack(int n,int w[],int m,int v[][10],int p[])
{
int i,j;
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
if(i==0 || j==0)
v[i][j]=0;
else if(j<w[i])
v[i][j]=v[i-1][j];
else
v[i][j]=max(v[i-1][j],v[i-1][j-w[i]]+p[i]);
}
}
}
int main()
{
int m,i,j,n,p[10],w[10],v[10][10];
printf("\nEnter the number of object\n");
scanf("%d",&n);
printf("\nEnter the waight of n object\n");
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
printf("\nEnter the profit of n objects\n");
for(i=1;i<=n;i++)
scanf("%d",&p[i]);
printf("\nEnter the capacity of knapsack: ");
scanf("%d",&m);
knapsack(n,w,m,v,p);
printf("\nThe output is:\n");
for(i=0;i<=n;i++)
{
2
for(j=0;j<=m;j++)
printf("%d\t",v[i][j]);
printf("\n");
}
printf("\noptimal solution is: %d",v[n][m]);
}
3
}
}
}
}
void print_path(int source,int destination,int d[],int p[])
{
int i;
i=destination;
while(i!=source)
{
printf("%d<-",i);
i=p[i];
}
printf("%d=%d\n",i,d[destination]);
}
void read_matrix(int n,int a[][10])
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
}
int main()
{
int n,i,j,cost[10][10],d[10],p[10],s[10];
printf("\nEnter the number of nodes\n");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix\n");
read_matrix(n,cost);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
dijkstra(n,cost,i,j,d,p);
if(d[j]==9999)
printf("%d is not reachable from %d\n",j,i);
else
{
printf("\nThe shortest path and distance is shown below\n");
print_path(i,j,d,p);
}
}
}
}
Pg9 . Find Miximum cost Spanning Tree of a given undirected graph using
Kruskals algorithm
#include<stdio.h>
int nv,ne,mincost,p[30],k,l,i,j;
struct edge
{
int v,u,cost;
}e[30],st[30];
void input()
{
printf("enter no of vertex and edges:");
scanf("%d\n%d",&nv,&ne);
for(i=1;i<=ne;i++)
{
printf("enter edge along with cost:");
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].cost);
}
}
int find(int x)
{
while(p[x]>0)
x=p[x];
return (x);
}
void sortedges()
{
struct edge temp;
for(i=1;i<=ne-1;i++)
for(j=i+1;j<=ne;j++)
if(e[i].cost>e[j].cost)
{
temp=e[i];
e[i]=e[j];
e[j]=temp;
}
}
void kruskal()
{
int m,n;
sortedges();
5
k=l=1;
for(i=1;i<=nv;i++)
p[i]=0;
while(k<=ne && l<=ne)
{
n=find(e[l].u);
m=find(e[l].v);
if(n!=m)
{
st[k]=e[l];
mincost+=e[l].cost;
p[n]=m;
k++;
}
l++;
}
}
void output()
{
int sum=0;
printf("minimum cost spanning tree:");
for(i=1;i<k;i++)
printf("\n(%d,%d):cost=%d\n",st[i].u,st[i].v,st[i].cost);
for(i=1;i<=k;i++)
sum=sum+st[i].cost;
printf("\nand the total cost is=%d\n",sum);
}
int main()
{
input();
kruskal();
output();
}
Pg12a.
#include<stdio.h>
char text[500],pattern[500];
int table[300],m,n;
void shifttable();
int horspool_match();
void shifttable()
{
6
int i;
for(i=0;i<=256;i++)
table[i]=m;
for(i=0;i<m-1;i++)
table[pattern[i]]=m-1-i;
}
int horspool_match()
{
printf("Enter the Text : \n");
scanf("%s",text);
printf("Enter the Pattern to Find : \n");
scanf("%s",pattern);
n = strlen(text);
m = strlen(pattern);
shifttable();
int i = m-1;
while(i<=n-1)
{
int k=0;
while(k<=m-1 && pattern[m-1-k]==text[i-k])
k = k+1;
if(k==m)
return(i-m+1);
else
i = i+table[text[i]];
}
return -1;
}
int main(void)
{
int v;
v=horspool_match();
if(v==-1)
printf("No Match Found!!!\n");
else
printf("Match Found at Positions :%d\n",(v+1));
}
7
int binomial(int n,int r);
int main()
{
int n,r;
printf("\nEnter the value of n and r(n>r)\n");
scanf("%d%d",&n,&r);
printf("\nBinomial co-efficent:\n");
printf("c(%d,%d)=%d\n",n,r,binomial(n,r));
printf("\n--------------------------------\n");
for(i=0;i<=n;i++)
{
for(j=0;j<=r;j++)
{
if(i>=j)
printf("%d\t",c[i][j]);
}
printf("\n");
}
printf("\n--------------------------------\n");
}
int binomial(int n,int r)
{
if(n<r)
{
printf("\nnot possible for binomial co-efficient\n");
exit(0);
}
for(i=0;i<=n;i++)
{
for(j=0;j<=r;j++)
{
if(j==0 || i==j)
c[i][j]=1;
else
c[i][j]=c[i-1][j-1]+c[i-1][j];
}
}
return c[n][r];
}
8
#include<stdio.h>
void prims(int n,int cost[10][10])
{
int i,j,u,v,min=9999;
int sum,k,t[10][2],p[10],d[10],s[10],source=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(cost[i][j]!=0 && cost[i][j]<=min)
{
min=cost[i][j];
source=i;
}
}
for(i=0;i<n;i++)
{
d[i]=cost[source][i];
s[i]=0;
p[i]=source;
}
s[source]=1;
sum=0;k=0;
for(i=1;i<n;i++)
{
min=9999;
u=-1;
for(j=0;j<n;j++)
if((s[j]==0) && (d[j]<=min))
{
min=d[j];
u=j;
}
t[k][0]=u;
t[k][1]=p[u];
k++;
sum+=cost[u][p[u]];
s[u]=1;
for(v=0;v<n;v++)
if(s[v]==0 && cost[u][v]<d[v])
{
d[v]=cost[u][v];
p[v]=u;
}
}
if(sum>=9999)
printf("spanning tree doesnot exist\n");
else
{
9
printf("the minimum spanning tree is\n");
for(i=0;i<n-1;i++)
printf("%d-->%d\n",t[i][0],t[i][1]);
printf("the cost of spanning tree:%d\n",sum);
}
}
void read(int n,int a[10][10])
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==0)
a[i][j]=9999;
if(i==j)
a[i][j]=0;
}
}
int main()
{
int n,cost[10][10];
printf("enter the no.of nodes\n");
scanf("%d",&n);
printf("enter the cost matrix of graph\n");
read(n,cost);
prims(n,cost)
}
10
printf("\nEnter 0 and 1 only\n");
scanf("%d",&a[i][j]);
}
}
warshall(a,n);
}
void warshall(int a[][10],int n)
{
int i,j,k;
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(a[i][k]==1 && a[k][j]==1)
a[i][j]=1;
printf("\nResultant %dth row & %dth column",k,k);
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
}
}
}
Pg15.
#include<stdio.h>
int count=0;
int place(int k,int x[])
{
int i;
for(i=1;i<k;i++)
{
if((x[i]==x[k])||(i-x[i]==k-x[k]||(i+x[i]==k+x[k])))
return 0;
}
return 1;
}
void queen(int n)
{
int k,x[40],i,j;
k=1;
x[k]=0;
while(k!=0)
{
x[k]=x[k]+1;
11
while(x[k]<=n && !place(k,x))
x[k]=x[k]+1;
if(x[k]<=n)
{
if(k==n)
{
count++;
printf("solution %d\n",count);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
if(j==x[i])
printf("Q");
else
printf("*");
printf("\n\n");
}
printf("\n\n");
}
else
{
k=k+1;
x[k]=0;
}
}
else
k=k-1;
}
}
int main()
{
int n;
printf("enter the no of queens\n");
scanf("%d",&n);
queen(n);
printf("total no of solutions is %d\n",count);
return 0;
}