Showing posts with label LAB Programs. Show all posts
Showing posts with label LAB Programs. Show all posts

Thursday, September 22, 2011

MERGE SORT Algorithm Implementation Program

IMPLEMENTATION OF MERGE SORT

PROGRAM:

#include
#include
#include
int n;
void main()
{
int i,low,high;
int a[20];
void mergesort(int a[10],int low,int high);
void display(int a[10]);
clrscr();
printf("\n\tMerge Sort");
printf("\n Enter the size of elements");
scanf("%d",&n);
printf("\n Enter the list element");
for(i=0;iscanf("%d",&a[i]);
low=0;
high=n-1;
mergesort(a,low,high);
display(a);
getch();
}
void mergesort(int a[10],int low,int high)
{
int mid;
void combine(int a[10],int low,int mid,int high);
if(low{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
combine(a,low,mid,high);
}
}
void combine(int a[10],int low,int mid,int high)
{
int i,j,k;
int temp[10];
k=low;
i=low;
j=mid+1;
while(i<=mid&&j<=high)
{
if(a[i]<=a[j])
{
temp[k]=a[i];
i++;
k++;
}
else
{
temp[k]=a[j];
j++;
k++;
}
}
while(i<=mid)
{
temp[k]=a[i];
i++;
k++;
}
while(j<=high)
{
temp[k]=a[j];
j++;
k++;
}
for(k=low;k<=high;k++)
a[k]=temp[k];
}
void display(int a[10])
{
int i;
printf("\n\n The Sorted Array is.......");
for(i=0;iprintf("%d\t",a[i]);
}









OUTPUT:

Merge Sort
Enter the size of elements 5

Enter the list element
9
8
7
6
5


The Sorted Array is.......5 6 7 8 9




CIET college Programs,LAB Programs for Engineering Students,DAA LAB Programs,DSA LAB Programs,Remoboys,karthik,Remokn,Student3k,programs source code,Design Analysis And Algorithms LAB Programs,Data Structures and Algorithms LAB Programs,LAB Codings,Coimbatore Institute of Engineering and Technology ( CIET )

Read more »

Wednesday, September 22, 2010

KNAPSACK Algorithm Implementation Program in C

IMPLEMENTATION OF KNAPSACK ALGORITHM

PROGRAM:

#include
#include
#include
int w[]={18,15,10};
int p[]={25,24,15};
int m=20;
int n=3;
int weight[10];
void main()
{
int i,u,a[10];
float t1,t2,t,wp[10],max;
float profit[]={0.0,0.0,0.0},tp=0.0,tpl=0.0,x[4];
clrscr();
cout<<"\n\t\t knapsack problem using greedy method";
cout<<"\n\t\t...........";
for(i=0;iwp[i]=(float)p[i]/w[i];
for(i=0;i{
for(int j=i+1;j{
if(wp[i]{
t1=p[i];
p[i]=p[j];
p[j]=t1;
t2=w[i];
w[i]=w[j];
w[j]=t2;
t=wp[i];
wp[i]=wp[j];
wp[j]=t;
}
}
}
u=m;
for(i=0;i{
profit[i]=0.0;
x[i]=0.0;
}
for(i=0;i{
if(w[i]>u)
break;
x[i]=1.0;
u=u-w[i];
profit[i]=p[i];
tp=tp+profit[i];
}
if(i{
x[i]=(float)u/w[i];
profit[i]=x[i]*p[i];
tp=tp+profit[i];
}
cout<<"\n object \t domain included \t profit gained";
for(i=0;i{
cout<<"\n"<cout<cout<<"\t\t\t"<}
cout<<"\n total profit gained"<tpl=0.0;
u=m;
for(i=0;i{
x[i]=0.0;
weight[i]=0.0;
}
for(i=0;i{
if(w[i]>u)
break;
x[i]=1.0;
u=u-w[i];
weight[i]=w[i];
tpl=tpl+weight[i];
}
if(i{
cout<<"\t\t\t"<}
cout<<"\n total profit gained"<tpl=0.0;
u=m;
for(i=0;i{
x[i]=0.0;
weight[i]=0.0;
}
for(i=0;i{
if(w[i]>u)
break;
x[i]=1.0;
u=u-w[i];
weight[i]=w[i];
tpl=tpl+weight[i];
}
if(i{
x[i]=(float)/p[i];
weight[i]=x[i]*p[i];
tp1=tp1+weight[i];
}
cout<<”\n\n\n object \t partial included \n profit gained”;
for(i=0;i{
cout<<”\n”<cout<cout<<”\t\t\t”<cout<<”\n\n\n total weight gained=”<getch();
}











OUTPUT:

Knapsack problem using greedy method
...........
object domain included profit gained
1 1 24
2 0.5 7.5
3 0 0
total profit gained31.5


object portion included profit gained
1 1 15
2 0.33 5
3 0 0
total weight gained=20






CIET college Programs,LAB Programs for Engineering Students,DAA LAB Programs,DSA LAB Programs,Remoboys,karthik,Remokn,Student3k,programs source code,Design Analysis And Algorithms LAB Programs,Data Structures and Algorithms LAB Programs,LAB Codings,Coimbatore Institute of Engineering and Technology ( CIET )

Read more »

PRIM'S Algorithm Implementation Program

IMPLEMENTATION OF PRIMS ALGORITHM

PROGRAM:

#include
#include
#define size 20
#define INFINITY 32767
void prim(int g[][10],int nodes)
{
int tree[size],i,j,k;
int min_dist,v1,v2,total=0;
for(i=0;itree[i]=0;
printf("\nthe minimum spanning tree is");
tree[0]=1;
for(k=1;k<=nodes-1;k++)
{
min_dist=INFINITY;
for(i=0;i{
for(j=0;j{
if(g[i][j]&&((tree[i]&&!tree[j])||(!tree[i]&&tree[j])))
{
if(g[i][j]{
min_dist=g[i][j];
v1=i;
v2=j;
}
}
}
}
printf("\n edge(%d%d)and weight=%d",v1,v2,min_dist);
tree[v1]=tree[v2]=1;
total=total+min_dist;
}
printf("\ntotal path length is =%d",total);
}
void main()
{
int g[size][size],nodes;
int v1,v2,length,i,j,n;
clrscr();

printf("\nprims algorithm");
printf("\nenter the no of nodes");
scanf("%d",&nodes);
printf("\nenter the no of edges");
scanf("%d",&n);
for(i=0;ifor(j=0;jg[i][j]=0;
printf("\nenter the edges and weight ");
for(i=0;i{
printf("\nenter the edge by v1&v2");
printf("\nread the graph from starting node");
scanf("%d%d",&v1,&v2);
printf("\nenter the corresponding weight");
scanf("%d",&length);
g[v1][v2]=g[v2][v1]=length;
}
getch();
printf("\n\t");
clrscr();
prim(g,nodes);
getch();
}






















OUTPUT:

prims algorithm

enter the no of nodes4

enter the no of edges6

enter the edges and weight
enter the edge by v1&v2
read the graph from starting node0 1

enter the corresponding weight1

enter the edge by v1&v2
read the graph from starting node0 3

enter the corresponding weight3

enter the edge by v1&v2
read the graph from starting node0 2

enter the corresponding weight3

enter the edge by v1&v2
read the graph from starting node1 2

enter the corresponding weight4

enter the edge by v1&v2
read the graph from starting node2 3

enter the corresponding weight7

enter the edge by v1&v2
read the graph from starting node1 3

enter the corresponding weight5


the minimum spanning tree is
edge(01)and weight=1
edge(20)and weight=3
edge(03)and weight=3
total path length is =7






CIET college Programs,LAB Programs for Engineering Students,DAA LAB Programs,DSA LAB Programs,Remoboys,karthik,Remokn,Student3k,programs source code,Design Analysis And Algorithms LAB Programs,Data Structures and Algorithms LAB Programs,LAB Codings,Coimbatore Institute of Engineering and Technology ( CIET )

Read more »

KRUSKAL's Algorithm Implementation Program

IMPLEMENTATION OF KRUSKALS ALGORITHM

PROGRAM:

#include
#include
#define INFINITY 999
typedef struct graph
{
int v1;
int v2,cost;
}GR;
GR g[20];
int tot_edges,tot_nodes;
void create();
void spanning_tree();
int minimum(int);
void main()
{
clrscr();
printf("\n Graph creation by adjacency matrix");
create();
spanning_tree();
getch();
}
void create()
{
int k;
printf("\n Enter the total no., of nodes");
scanf("%d",&tot_nodes);
printf("\n Enter the total no0., of edges");
scanf("%d",&tot_edges);
for(k=0;k{
printf("\n Enter the edges in(v1,v2) from");
scanf("%d%d",&g[k].v1,&g[k].v2);
printf("\n Enter the corresponding cost");
scanf("%d",&g[k].cost);
}
}
void spanning_tree()
{
int count,k,v1,v2,i,j,tree[10][10],pos,parent[10];
int sum;
int find(int i,int parent[]);
void union1(int i,int j,int parent[]);
count=0;
k=0;
sum=0;
for(i=0;iparent[i]=i;
while(count!=tot_nodes-1)
{
pos=minimum(tot_edges);
if(pos==-1)
break;
v1=g[pos].v1;
v2=g[pos].v2;
i=find(v1,parent);
j=find(v2,parent);
if(i!=j)
{
tree[k][0]=v1;
tree[k][1]=v2;
k++;
count++;
sum+=g[pos].cost;
union1(i,j,parent);
}
g[pos].cost=INFINITY;
}
if(count==tot_nodes-1)
{
printf("\n The Spanning tree is...,,");
printf("\n ..............\n ");
for(i=0;i{
printf("%d",tree[i][0]);
printf("_");
printf("%d",tree[i][0]);
printf("]");
}
printf("\n......");
printf("\n Cost of the spanning tree is=%d",sum);
}
else
{
printf("\n There is no spanning tree");
}
}
int minimum(int n)
{
int i,small,pos;
small=INFINITY;
pos=-1;
for(i=0;i{
if(g[i].cost{
small=g[i].cost;
pos=i;
}
}
return pos;
}
int find(int v2,int parent[])
{
while(parent[v2]!=v2)
{
v2=parent[v2];
}
return v2;
}
void union1(int i, int j, int parent[])
{
if(iparent[j]=i;
else
parent[i]=j;
}







OUTPUT:

Graph creation by adjacency matrix

Enter the total no of nodes 7

Enter the total no of edges 12

Enter the corresponding cost 1
Enter the edges in(v1,v2) from 1 2

Enter the corresponding cost 4
Enter the edges in(v1,v2) from 2 5

Enter the corresponding cost 7
Enter the edges in(v1,v2) from 5 7

Enter the corresponding cost 6
Enter the edges in(v1,v2) from 7 6

Enter the corresponding cost 5
Enter the edges in(v1,v2) from 6 3

Enter the corresponding cost 1
Enter the edges in(v1,v2) from 3 1

Enter the corresponding cost 2
Enter the edges in(v1,v2) from 1 4

Enter the corresponding cost 3
Enter the edges in(v1,v2) from 2 4

Enter the corresponding cost 6
Enter the edges in(v1,v2) from 7 4

Enter the corresponding cost 2
Enter the edges in(v1,v2) from 6 4

Enter the corresponding cost 4
Enter the edges in(v1,v2) from 3 4

The Spanning tree is...

[1_3][3_3][1_1][6_6][5_5][7_7]

Cost of the spanning tree is=15






CIET college Programs,LAB Programs for Engineering Students,DAA LAB Programs,DSA LAB Programs,Remoboys,karthik,Remokn,Student3k,programs source code,Design Analysis And Algorithms LAB Programs,Data Structures and Algorithms LAB Programs,LAB Codings,Coimbatore Institute of Engineering and Technology ( CIET )

Read more »

IMPLEMENTATION OF NON RECURSIVE BINARY SEARCH TREE PROGRAM

IMPLEMENTATION OF NON RECURSIVE BINARY SEARCH TREE

PROGRAM:

#include
#include
#define size 10
int n;
void main()
{
int a[size],key,i,flag;
int binsearch(int a[size],int key);
clrscr();
printf("\n How many elements for an array");
scanf("%d",&n);
printf("Enter the Elements");
for(i=0;iscanf("%d",&a[i]);
printf("\n Enter the no which is to be searched");
scanf("%d",&key);
flag=binsearch(a,key);
if(flag==-1)
printf("\nThe element is not Present");
else
printf("\n The element is a[%d] localtion",flag);
getch();
}
int binsearch(int a[size],int key)
{
int low,high,m ;
low=0;
high=n-1;
while(low<=high)
{
m=(low+high)/2;
if(key==a[m])
return m;
else if(keyhigh=m-1;
else
low=m+1;
}
return -1;
}


OUTPUT:

How many elements for an array 5
Enter the Elements
1
2
3
4
5

Enter the no which is to be searched 4

The element is a [3] location





CIET college Programs,LAB Programs for Engineering Students,DAA LAB Programs,DSA LAB Programs,Remoboys,karthik,Remokn,Student3k,programs source code,Design Analysis And Algorithms LAB Programs,Data Structures and Algorithms LAB Programs,LAB Codings,Coimbatore Institute of Engineering and Technology ( CIET )

Read more »

IMPLEMENTATION OF RECURSIVE BINARY SEARCH TREE PROGRAM

IMPLEMENTATION OF RECURSIVE BINARY SEARCH TREE

PROGRAM:

#include
#include
#define size 10
int n;
void main()
{
int a[size],flag,i,key,low,high;
int binsearch(int a[size],int key,int low,int high);
clrscr();
printf("\n How many are there in array");
scanf("%d",&n);
printf("\n Enter the Element");
for(i=0;iscanf("%d",&a[i]);
printf("\n Enter the Element which is to be searched");
scanf("%d",&key);
low=0;
high=n-1;
flag=binsearch(a,key,low,high);
printf("\n Element a[%d] location",flag);
getch();
}
int binsearch(int a[size],int key,int low,int high)
{
int m;
m=(low+high)/2;
if(key==a[m])
return m;
else if(keybinsearch(a,key,low,m-1);
else
binsearch(a,key,m+1,high);
}









OUTPUT:

How many are there in array 5

Enter the Element
1
2
3
4
5

Enter the Element which is to be searched 3

Element a [2] location





CIET college Programs,LAB Programs for Engineering Students,DAA LAB Programs,DSA LAB Programs,Remoboys,karthik,Remokn,Student3k,programs source code,Design Analysis And Algorithms LAB Programs,Data Structures and Algorithms LAB Programs,LAB Codings,Coimbatore Institute of Engineering and Technology ( CIET )

Read more »

IMPLEMENTATION OF HEAP SORT Programs

IMPLEMENTATION OF HEAP SORT

PROGRAM:

#include
#include
#include
#define MAX 10
void main()
{
int i,n;
int arr[MAX];
void makeheap(int arr[MAX],int n);
void heapsort(int arr[MAX],int n);
void display(int arr[MAX],int n);
clrscr();
for(i=0;iarr[i]=0;
printf("\n How many elements you want to sort");
scanf("%d",&n);
printf("Enter the elements");
for(i=0;iscanf("%d",&arr[i]);
printf("\n The elements are....");
display(arr,n);
makeheap(arr,n);
printf("\n Heapified");
display(arr,n);
heapsort(arr,n);
printf("\n Elements sorted by heapsort");
display(arr,n);
getch();
}
void makeheap(int arr[MAX],int n)
{
int i,val,j,father;
for(i=1;i{
val=arr[i];
j=i;
father=(j-1)/2;
while(j>0&&arr[father]{
arr[j]=arr[father];
j=father;
father=(j-1)/2;
}
arr[j]=val;
}
}
void heapsort(int arr[MAX],int n)
{
int i,k,temp,j;
for(i=n-1;i>0;i--)
{
temp=arr[i];
arr[i]=arr[0];
k=0;
if(i==1)
j=-1;
else
j=1;
if(i>2&&arr[2]>arr[j])
j=2;
while(j>=0&&temp{
arr[k]=arr[j];
k=j;
j=2*k+1;
if(j+1<=i-1&&arr[j]j++;
if(j>i-1)
j=-1;
}
arr[k]=temp;
}
}
void display(int arr[MAX],int n)
{
int i;
for(i=0;iprintf("%d",arr[i]);
}









OUTPUT:

How many elements you want to sort 5
Enter the elements
9
8
7
6
5

The elements are....
9
8
7
6
5
Heapified
9
8
7
6
5
Elements sorted by heapsort
5
6
7
8
9




CIET college Programs,LAB Programs for Engineering Students,DAA LAB Programs,DSA LAB Programs,Remoboys,karthik,Remokn,Student3k,programs source code,Design Analysis And Algorithms LAB Programs,Data Structures and Algorithms LAB Programs,LAB Codings,Coimbatore Institute of Engineering and Technology ( CIET )

Read more »

IMPLEMENTATION OF SELECTION SORT PROGRAMs

IMPLEMENTATION OF SELECTION SORT

PROGRAM:

#include
#include
int n;
void main()
{
int a[10],i,j;
void selection(int a[10]);
clrscr();
printf("\n\t\tSelection Sort");
printf("\nHow many Elements are there");
scanf("%d",&n);
printf("\nEnter the element");
for(i=0;iscanf("%d",&a[i]);
selection(a);
getch();
}
void selection(int a[10])
{
int i,j,min,temp;
for(i=0;i<=n-2;i++)
{
min=i;
for(j=i+1;j<=n-1;j++)
{
if(a[j]min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
printf("\n Sorted List is\n");
for(i=0;iprintf("\n%d",a[i]);
}






OUTPUT:


Selection Sort
How many Elements are there 5

Enter the element
5
4
3
2
1


Sorted List is

1
2
3
4
5





CIET college Programs,LAB Programs for Engineering Students,DAA LAB Programs,DSA LAB Programs,Remoboys,karthik,Remokn,Student3k,programs source code,Design Analysis And Algorithms LAB Programs,Data Structures and Algorithms LAB Programs,LAB Codings,Coimbatore Institute of Engineering and Technology ( CIET )

Read more »

IMPLEMENTATION OF QUICK SORT PROGRAM

IMPLEMENTATION OF QUICK SORT

PROGRAM:

#include
#include
#define size 10
void quick(int a[size],int,int);
int partition(int a[size],int,int);
void swap(int a[size],int *,int*);
int n;
void main()
{
int i;
int a[size];
clrscr();
printf("\n Quick Sort");
printf("\n Enter the no of sort");
scanf("%d",&n);
for(i=0;i{
printf("\nEnter the %d st number",i+1);
scanf("\n%d",&a[i]);
}
quick(a,0,n-1);
printf("\nSorted Array");
for(i=0;iprintf("%d",a[i]);
getch();
}
void quick(int a[size],int low,int high)
{
int m,i;
if(low{
m=partition(a,low,high);
quick(a,low,m-1);
quick(a,m+1,high);
}
}
int partition(int a[size],int low,int high)
{
int pivot=a[low],i=low,j=high;
while(i<=j)
{
while(a[i]<=pivot)
i++;
while(a[j]>pivot)
j--;
if(iswap(a,&i,&j);
}
swap(a,&low,&j);
return j;
}
void swap(int a[size],int *i,int*j)
{
int temp;
temp=a[*i];
a[*i]=a[*j];
a[*j]=temp;
}






























OUTPUT:

Quick Sort

Enter the no of sort 5

Enter the 1 st number 9

Enter the 2 st number 7

Enter the 3 st number 5

Enter the 4 st number 3

Enter the 5 st number 1

Sorted Array 1 3 5 7 9







CIET college Programs,LAB Programs for Engineering Students,DAA LAB Programs,DSA LAB Programs,Remoboys,karthik,Remokn,Student3k,programs source code,Design Analysis And Algorithms LAB Programs,Data Structures and Algorithms LAB Programs,LAB Codings,Coimbatore Institute of Engineering and Technology ( CIET )
Read more »

IMPLEMENTATION OF MERGE SORT

IMPLEMENTATION OF MERGE SORT

PROGRAM:

#include
#include
#include
int n;
void main()
{
int i,low,high;
int a[20];
void mergesort(int a[10],int low,int high);
void display(int a[10]);
clrscr();
printf("\n\tMerge Sort");
printf("\n Enter the size of elements");
scanf("%d",&n);
printf("\n Enter the list element");
for(i=0;iscanf("%d",&a[i]);
low=0;
high=n-1;
mergesort(a,low,high);
display(a);
getch();
}
void mergesort(int a[10],int low,int high)
{
int mid;
void combine(int a[10],int low,int mid,int high);
if(low{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
combine(a,low,mid,high);
}
}
void combine(int a[10],int low,int mid,int high)
{
int i,j,k;
int temp[10];
k=low;
i=low;
j=mid+1;
while(i<=mid&&j<=high)
{
if(a[i]<=a[j])
{
temp[k]=a[i];
i++;
k++;
}
else
{
temp[k]=a[j];
j++;
k++;
}
}
while(i<=mid)
{
temp[k]=a[i];
i++;
k++;
}
while(j<=high)
{
temp[k]=a[j];
j++;
k++;
}
for(k=low;k<=high;k++)
a[k]=temp[k];
}
void display(int a[10])
{
int i;
printf("\n\n The Sorted Array is.......");
for(i=0;iprintf("%d\t",a[i]);
}









OUTPUT:

Merge Sort
Enter the size of elements 5

Enter the list element
9
8
7
6
5


The Sorted Array is.......5 6 7 8 9




CIET college Programs,LAB Programs for Engineering Students,DAA LAB Programs,DSA LAB Programs,Remoboys,karthik,Remokn,Student3k,programs source code,Design Analysis And Algorithms LAB Programs,Data Structures and Algorithms LAB Programs,LAB Codings,Coimbatore Institute of Engineering and Technology ( CIET )

Read more »

SHORTEST Path Finding Algorithm Implementation Program

IMPLEMENTATION OF SHORTEST PATH ALGORITHM

PROGRAM:

#include
#include
#include
class path
{
int a[10][10],c[10][10],key[10][10],num,min,i,j,k;
public:
void findpath();
void read();
void output(int,int);
void out(int i,int j);
};
void path::read()
{
cout<<"Number of rows:";
cin>>num;
for(i=1;i<=num;i++)
for(j=1;j<=num;j++)
{
if(i==j)
a[i][j]=0;
else
{
cout<<"The cost for["<cin>>a[i][j];
}
c[i][j]=a[i][j];
key[i][j]=0;
}
}
void path::findpath()
{
int t1,t2,t3;
for(k=1;k<=num;k++)
for(i=1;i<=num;i++)
for(j=1;j<=num;j++)
{
t1=c[i][k];
t2=c[k][j];
t3=c[i][j];
if(t1!=0&&t2!=0&&(t3==0||t1+t2{
c[i][j]=t1+t2;
key[i][j]=k;
}
}
}
void path::output(int i,int j)
{
min=0;
if(c[i][j]==0)
{
cout<<"No path exist";
return;
}
else
{
cout<<"The path is"<out(i,j);
cout<cout<<"\n";
}
}
void path::out(int i,int j)
{
if(i==j)
return;
if(key[i][j]==0)
{
cout<<"->"<min+=a[i][j];
}
else
{
out(i,key[i][j]);
out(key[i][j],j);
}
}
void main()
{
clrscr();
int ch=1,n1,n2;
path p;
p.read();
p.findpath();
cout<cout<<"2.Exist"<while(ch!=2)
{
cout<cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the source node";
cin>>n1;
cout<<"Enter the designation node";
cin>>n2;
p.output(n1,n2);
break;
case 2:
exit(0);
}
}
getch();
}

























OUTPUT:

number of nodes3
The cost for[1,2]1
The cost for[1,3]2
The cost for[2,1]3
The cost for[2,3]4
The cost for[3,1]5
The cost for[3,2]6

1,Shortest path
2.Exit

choice...1
Enter the source node1
Enter the destination node2
The path is:1->2
cost is:1

choice...1
Enter the source node3
Enter the destination node2
The path is:3->2
cost is:6

choice...



CIET college Programs,LAB Programs for Engineering Students,Computer Networks LAB Programs,Remoboys,karthik,Remokn,Student3k,TCP programs source code

Read more »

IMPLEMENTATION OF SLIDING WINDOW PROTOCOL

IMPLEMENTATION OF SLIDING WINDOW PROTOCOL

PROGRAM:

#include
#include
void main()
{
int a[20],b[20],i=0,j=0,k=0,l=0,n;
clrscr();
printf("\nEnter the size of window:");
scanf("%d",&n);
printf("\nenter the values:\n");
for(i=n-1;i>=0;i--)
scanf("%d",&a[i]);
printf("\nValues of sender and receiver:\n");
for(i=0;iprintf("%d\t",a[i]);
for(j=0;jprintf("%d\t",b[i]);
for(i=n-1,j=0;i>=0;i--,j++)
{
b[j]=a[i];
a[i]=0;
printf("\nSENDER\n");
for(k=0;kprintf("%d\t",a[k]);
printf("\nRECEIVER\n");
for(l=0;lprintf("%d\t",b[l]);
printf("\nDATA RECEIVED\n");
}
for(k=0;k{
b[k]=0;
}
printf("\nDATA RECEIVED SUCCESSFULLY\n");
getch();
}







OUTPUT:

Enter the size of window:4

enter the values:
1
5
7
4

Values of sender and receiver:
4 7 5 1 0 0 0 0
SENDER
4 7 5 0
RECEIVER
1 0 0 0
DATA RECEIVED

SENDER
4 7 0 0
RECEIVER
1 5 0 0
DATA RECEIVED

SENDER
4 0 0 0
RECEIVER
1 5 7 0
DATA RECEIVED

SENDER
0 0 0 0
RECEIVER
1 5 7 4
DATA RECEIVED

DATA RECEIVED SUCCESSFULLY



CIET college Programs,LAB Programs for Engineering Students,Computer Networks LAB Programs,Remoboys,karthik,Remokn,Student3k,TCP programs source code

Read more »

IMPLEMENTATION OF UDP ( Universal Datagram Protocol ) Programs

IMPLEMENTATION OF UDP
PROGRAM:

CLIENT:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define REMOTE_SERVER_PORT 1896s
#define MAX_MSG 100
int main(int argc,char*argv[])
{
int sd,rc,i;
struct sockaddr_in cliaddr,remoteservaddr;
struct hostent *h;
if(argc<3)
{
printf("Usage:%s..\n",argv[0]);
exit(1);
}
h=gethostbyname(argv[1]);
if(h==NULL)
{
printf("%s:unknown host%s\n",argv[0],argv[1]);
exit(1);
}
printf("%s:sending data to '%s'(IP:%s)\n",argv[0],h->h_name,inet_ntoa(*(struct i
n_addr*)h->h_addr_list[0]));
remoteservaddr.sin_family=h->h_addrtype;
memcpy((char*)&remoteservaddr.sin_addr.s_addr,h->h_addr_list[0],h->h_length);
remoteservaddr.sin_port=htons(REMOTE_SERVER_PORT);
sd=socket(AF_INET,SOCK_DGRAM,0);
if(sd<0)
{
printf("%s:cannot open socket\n",argv[0]);
exit(1);
}
cliaddr.sin_family=AF_INET;
cliaddr.sin_addr.s_addr=htonl(INADDR_ANY);
cliaddr.sin_port=htons(0);
rc=bind(sd,(struct sockaddr*)&cliaddr,sizeof(cliaddr));
if(rc<0)
{
printf("%s:cannot bindport\n",argv[0]);
exit(1);
}
for(i=2;i{
rc=sendto(sd,argv[i],strlen(argv[i])+1,0,(struct sockaddr*)&remoteservaddr,sizeo
f(remoteservaddr));
if(rc<0)
{
printf("%s:cannot send data%d\n",argv[0],i-1);
close(sd);
exit(1);
}
}
return 1;
}


SERVER:

#include
#include
#include
#include
#include
#include
#include
#define LOCAL_SERVER_PORT 1896
#define MAX_MSG 100
int main(int argc,char *argv[])
{
int sd,rc,n,clilen;
struct sockaddr_in cliaddr,servaddr;
char msg[MAX_MSG];
sd=socket(AF_INET,SOCK_DGRAM,0);
if(sd<0)
{
printf("%s:cannot open socket\n",argv[0]);
exit(1);
}
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(LOCAL_SERVER_PORT);
rc=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));
if(rc<0)
{
printf("%s:cannot bind port number %d\n",argv[0],LOCAL_SERVER_PORT);
exit(1);
}
printf("%s:waiting for data on port UDP%u\n",argv[0],LOCAL_SERVER_PORT);
while(1)
{
memset(msg,0x0,MAX_MSG);
clilen=sizeof(cliaddr);
n=recvfrom(sd,msg,MAX_MSG,0,(struct sockaddr*)&cliaddr,&clilen);
if(n<0)
{
printf("%s:cannot receive data\n",argv[0]);
continue;
}
printf("%s:from%s:UDP%u:%s\n",argv[0],inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.
sin_port),msg);
}
return 0;
}
























OUTPUT:

IN SERVER:

[testciet @linuxcentre ~]$ ./a.out
./a.out:waiting for data on port UDP1896
./a.out:from127.0.0.1:UDP32771:hi ……


IN CLIENT:

[testciet@linuxcentre ~]$ ./a.out localhost hi ……
./a.out:sending data to 'linuxcentre'(IP:127.0.0.1)



CIET college Programs,LAB Programs for Engineering Students,Computer Networks LAB Programs,Remoboys,karthik,Remokn,Student3k,TCP programs source code

Read more »

IMPLEMENTATION OF TCP (Transmission Control Protocol) Source Code in C Language

PROGRAM:

CLIENT ( Type This cilent Program in The Editor ):

#include
#include
#include
#include
#include
#include
#include
int main(int argc,char **argv)
{
struct sockaddr_in saddr;
struct hostent *server;
int n,i,ssid,csid,pid;
char buffer[1024];
if(argc<2)
fprintf(stderr,"port # not specified\n");
csid=socket(AF_INET,SOCK_STREAM,0);
if(csid<0)
perror("socket failed error");
bzero((char*)&saddr,sizeof(saddr));
server=gethostbyname(argv[1]);
saddr.sin_family=AF_INET;
saddr.sin_port=htons(atoi(argv[2]));
bcopy((char*)server->h_addr,(char*)&saddr.sin_addr.s_addr,server->h_length);
ssid=connect(csid,(struct sockaddr*)&saddr,sizeof(saddr));
if(ssid<0)
perror("socket connect error");
bzero(buffer,1024);
printf("Type msg to server");
fgets(buffer,1024,stdin);
n=write(csid,buffer,sizeof(buffer));
if(n==0)
perror("Socket write error");
n=read(csid,buffer,sizeof(buffer));
perror("Socket read buffer");
printf("msg from server :\n");
for(i=0;iprintf("%c",buffer[i]);
return 0;
}


SERVER:

#include
#include
#include
#include
#include
int main(int argc,char **argv)
{
struct sockaddr_in saddr,caddr;
int n,len,ssid,csid,pid;
char buffer[1024];
if(argc<2)
fprintf(stderr,"port # not specified\n");
ssid=socket(AF_INET,SOCK_STREAM,0);
if(ssid<0)
perror("Socket failed error");
bzero((char*)&saddr,sizeof(saddr));
saddr.sin_family=AF_INET;
saddr.sin_port=htons(atoi(argv[1]));
saddr.sin_addr.s_addr=INADDR_ANY;
if(bind(ssid,(struct sockaddr*)&saddr,sizeof(saddr))<0)
perror("Socket bind error");
listen(ssid,5);
len=sizeof(caddr);
csid=accept(ssid,(struct sockaddr*)&caddr,&len);
if(csid<0)
perror("socket accept error");
bzero(buffer,1024);
n=read(csid,buffer,1024);
if(n==0)
perror("Socket read error");
printf("msg from client:%s\n",buffer);
n=write(csid,buffer,1024);
if(n<0)
perror("write error");
return 0;
}









OUTPUT:

IN SERVER:
[testciet @linuxcentre ~]$ cc tcpserver.c
[testciet @linuxcentre ~]$ ./a.out 1678
Msg from client: hi

IN CLIENT:
[testciet @linuxcentre ~]$ cc tcpclient.c
[testciet @linuxcentre ~]$ ./a.out localhost 1678
Type msg to server:hi
Socket read buffer: Success
msg from server :
hi


Keywords :
CIET college Programs,LAB Programs for Engineering Students,Computer Networks LAB Programs,Remoboys,karthik,Remokn,Student3k,TCP programs source code
Read more »

Free Projects Download :

Free Projects Download :
Free students projects download for all.

Popular Posts( Last 7 Days )