#include<iostream.h> float powerec(float b, float r) { if(r==0) return 1; else return(b*powerec(b,r-1)); } void main() { float b,r; b=0; r=0; cout<<"\nEnter The Base: "; cin>>b; cout<<"\nEnter The Power: "; cin>>r; float res=powerec(b,r); cout<<"\nThe Result Is: "<<res; }
Showing posts with label c plus plus. Show all posts
Showing posts with label c plus plus. Show all posts
Sunday, May 18, 2014
X to the power of Y using recursion in C ++
Labels:
c plus plus,
cpp program,
data structures,
recursion,
x to the power y
Friday, May 9, 2014
Binary Search Tree Program in C++
#include <iostream.h> #include <process.h> class tnode { public: int data; tnode *lchild; tnode *rchild; tnode(int n,tnode *l=0,tnode *r=0) { data=n; lchild=l; rchild=r; } }; class Queue { private: int front,rear,size; tnode **a; public: Queue(int size=0) { front=rear=-1; if (size!=0) a=new tnode*[size]; else a=0; this->size=size; } void enqueue(tnode* el); tnode* dequeue(); bool isempty() const; }; void Queue::enqueue(tnode* el) { if(isempty()) { front=rear=0; a[front]=el; } else { rear=(rear+1)%size; a[rear]=el; } } tnode* Queue::dequeue() { tnode* i; if (front==-1) cout<<"Empty list. Deletion not possible"; else if (front==rear) { i=a[front]; front=rear=-1; } else { i=a[front]; front=(front+1)%size; } return i; } bool Queue::isempty() const { return(front==-1); } class BST { private: tnode *root; void preorder(tnode *); void postorder(tnode *); void inorder(tnode *); public: BST() { root=0; } void insert(int x); void insert(tnode* &p,int x); void delete_by_copying(int x); void delete_by_merging(int x); tnode* search(int x); tnode* search(tnode * p,int x); void preorder(); void postorder(); void inorder(); void level_by_level_traversal(); int count_leafnode(); int count_leafnode(tnode *t); int count_nonleaf(); int count_nonleaf(tnode *t); int height(); int height(tnode *t); tnode* mirror(); tnode* mirror(tnode *p); }; // Inserting an element void BST::insert(int x) { treenode*t=new treenode(x); if(root==NULL) { root=t; return; } treenode*p,*fp; p=root; fp=0; while(p!=0) { fp=p; if(x==p->data) throw"DUPLICATE VALUE"; else if(x<p->data) p=p->lchild; else p=p->rchild; } if(x<fp->data) fp->lchild=t; else fp->rchild=t; } // Deletion By Copying void BST::delete_by_copying(int x) { if (root==0) throw "Empty tree"; tnode *p=root; tnode *fp=0; while(p!=0 && p->data!=x) { fp=p; if (x<p->data) p=p->lchild; else p=p->rchild; } if (p==0) throw "Element not present "; bool lchild; if (fp!=0) lchild=(x<fp->data)?true:false; // if deleted node is a leaf if ((p->lchild==0) && (p->rchild)==0) { if (fp==0) { root=0;return; } if (lchild) fp->lchild=0; else fp->rchild=0; } // if deleted node has lchild only else if (p->rchild==0) { if (fp==0) { root=p->lchild; return; } if (lchild) fp->lchild=p->lchild; else fp->rchild=p->lchild; } // if deleted node has rchild only else if (p->lchild==0) { if (fp==0) { root=p->rchild; return; } if (lchild) fp->lchild=p->rchild; else fp->rchild=p->rchild; } // if both the children exists else { tnode *q=p->rchild; tnode *fq=p; while (q->lchild!=0) { fq=q; q=q->lchild; } p->data=q->data; if (q->data<fq->data) fq->lchild=q->rchild; else fq->rchild =q->rchild; delete q; } } // Deletion by Merging void BST::delete_by_merging(int x) { if (root==0) throw "Empty tree"; tnode *p=root; tnode *fp=0; while(p!=0 && p->data!=x) { fp=p; if (x<p->data) p=p->lchild; else p=p->rchild; } if (p==0) throw "Element not present "; bool lchild; if (fp!=0) lchild=(x<fp->data)?true:false; // if deleted node is a leaf if ((p->lchild==0) && (p->rchild)==0) { if (fp==0) { root=0;return; } if (lchild) fp->lchild=0; else fp->rchild=0; } // if deleted node has lchild only else if (p->rchild==0) { if (fp==0) { root=p->lchild; return; } if (lchild) fp->lchild=p->lchild; else fp->rchild=p->lchild; } // if deleted node has rchild only else if (p->lchild==0) { if (fp==0) { root=p->rchild; return; } if (lchild) fp->lchild=p->rchild; else fp->rchild=p->rchild; } // if both the children exists else { tnode *q=p->lchild; tnode *rc=p->rchild; while (q->rchild!=0) { q=q->rchild; } q->rchild=rc; if (fp==0) root=p->lchild; else { if (p->data<fp->data) fp->lchild=p->lchild; else fp->rchild=p->lchild; } delete p; } } //Searching an element tnode* BST::search(int x) { if (root==0) return(0); if (root->data==x) return (root); if (x<root->data) return(search(root->lchild,x)); else return(search(root->rchild,x)); } tnode* BST::search(tnode * p,int x) { if (p==0) return(0); if (p->data==x) return (p); if (x<p->data) return(search(p->lchild,x)); else return(search(p->rchild,x)); } // Inorder Traversal void BST::inorder() { if (root==0) return; inorder(root->lchild); cout<<root->data<<" "; inorder(root->rchild); } void BST::inorder(tnode* r) { if (r==0) return; inorder(r->lchild); cout<<r->data<<" "; inorder(r->rchild); } // Preorder Traversal void BST::preorder() { if (root==0) return; cout<<root->data<<" "; preorder(root->lchild); preorder(root->rchild); } void BST::preorder(tnode* r) { if (r==0) return; cout<<r->data<<" "; preorder(r->lchild); preorder(r->rchild); } //Postorder Traversal void BST::postorder() { if (root==0) return; postorder(root->lchild); postorder(root->rchild); cout<<root->data<<" "; } void BST::postorder(tnode* r) { if (r==0) return; postorder(r->lchild); postorder(r->rchild); cout<<r->data<<" "; } // Level by Level Traversal void BST::level_by_level_traversal() { if(root==0) { cout<<"Empty tree"; return; } cout<<"\nLevel by level traversal is "; Queue q(10); q.enqueue(root); while(!q.isempty()) { tnode *p=q.dequeue(); cout<<p->data<<" "; if(p->lchild!=0) q.enqueue(p->lchild); if(p->rchild!=0) q.enqueue(p->rchild); } } //Count leaf nodes int BST::count_leafnode() { if(root==0) return 0; else if(root->lchild==0&&root->rchild==0) return 1; else return(count_leafnode(root->lchild)+count_leafnode(root->rchild)); } int BST::count_leafnode(tnode *t) { if(t==0) return 0; else if(t->lchild==0&&t->rchild==0) return 1; else return(count_leafnode(t->lchild)+count_leafnode(t->rchild)); } //Count non leaf nodes int BST::count_nonleaf() { if(root==0) return 0; else if(root->lchild==0&&root->rchild==0) return 0; else return(1+count_nonleaf(root->lchild)+count_nonleaf(root->rchild)); } int BST::count_nonleaf(tnode *t) { if(t==0) return 0; else if(t->lchild==0&&t->rchild==0) return 0; else return(1+count_nonleaf(t->lchild)+count_nonleaf(t->rchild)); } //Calculate height of tree int BST::height() { if (root==0) return 0; else if (root->lchild==0 && root->rchild==0) return 1; else { int hl=height(root->lchild); int hr=height(root->rchild); int max=hl>hr?hl:hr; return(max+1); } } int BST::height(tnode *t) { if(t==0) return 0; else if(t->lchild==0&&t->rchild==0) return 1; else { int x=height(t->lchild); int y=height(t->rchild); if(x>y) return(1+x); else return (1+y); } } //Mirror image of tree tnode* BST::mirror() { if(root == NULL) return root ; else { mirror(root->lchild); mirror(root->rchild); tnode *temp; temp=root->lchild; root->lchild=root->rchild; root->rchild=temp; } return root; } tnode* BST::mirror(tnode *p) { if(p == NULL) return p ; else { mirror(p->lchild); mirror(p->rchild); tnode *temp; temp=p->lchild; p->lchild=p->rchild; p->rchild=temp; } return p; } void main() { BST b; int i,n; tnode *p; int ch; do { cout<<"\n\nMAIN MENU"; cout<<"\n1.)Insertion "; cout<<"\n2.)Deletion by copying "; cout<<"\n3.)Deletion by merging"; cout<<"\n4.)Search a no in BST "; cout<<"\n5.)In-Order Traversal "; cout<<"\n6.)Pre-Order Traversal "; cout<<"\n7.)Post-Order Traversal "; cout<<"\n8.)Level-by-level Traversal "; cout<<"\n9.)Count no of non leaf nodes "; cout<<"\n10.)Count no of leaf nodes "; cout<<"\n11.)Height of tree "; cout<<"\n12.)Mirror image "; cout<<"\n13.)Exit "; cout<<"\n\n\t\t\t Enter your choice: "; cin>>ch; switch(ch) { case 1 : cout<<"\n Enter the node to be inserted : "; cin>>n; b.insert(n); cout<<"\n The tree in inorder traversal is: "; b.inorder(); break; case 2 : cout<<"\n Enter the node to be deleted : "; cin>>n; b.delete_by_copying(n); cout<<"\n The tree in inorder traversal is: "; b.inorder(); break; case 3 : cout<<"\n Enter the node to be deleted : "; cin>>n; b.delete_by_merging(n); cout<<"\n The tree in inorder traversal is: "; b.inorder(); break; case 4 : cout<<"\n Enter the node to be searched: "; cin>>n; p=b.search(n); if(p!=0) cout<<"\n Node "<<p<<" found"; else cout<<"\n Node not present"; break; case 5 : cout<<"\n Inorder traversal is: "; b.inorder(); break; case 6 : cout<<"\n Preorder traversal is: "; b.preorder(); break; case 7 : cout<<"\n Postorder traversal is: "; b.postorder(); break; case 8 : b.level_by_level_traversal(); break; case 9 : n=b.count_nonleaf(); cout<<"No. of non leaf nodes= "<<n; break; case 10: n=b.count_leafnode(); cout<<"No. of leaf nodes= "<<n; break; case 11: n=b.height(); cout<<"Height of tree= "<<n; break; case 12: p=b.mirror(); cout<<"\n The tree in inorder traversal is: "; b.inorder(); break; default: exit(0); } }while(ch!=13); }
Labels:
binary search trees,
c plus plus,
cpp program,
data structures
Saturday, March 22, 2014
STOP AND WAIT PROTOCOL PROGRAM IN C++
#include <conio.h> #include <dos.h> #include <stdio.h> #include <stdlib.h> #define TIMEOUT 5 #define MAX_SEQ 1 #define TOT_PACKETS 8 #define inc(k) if(k<MAX_SEQ) k++; else k=0; typedef struct { int data; }packet; typedef struct { int kind; int seq; int ack; packet info; int err; }frame; frame DATA; typedef enum{frame_arrival,err,timeout,no_event} event_type; void from_network_layer(packet *); void to_network_layer(packet *); void to_physical_layer(frame *); void from_physical_layer(frame *); void wait_for_event_sender(event_type *); void wait_for_event_reciever(event_type *); void reciever(); void sender(); int i=1; //Data to be sent by sender char turn; //r , s int DISCONNECT=0; /*__________________________________________________________________________*/ void main() { clrscr(); randomize(); while(!DISCONNECT) { sender(); delay(400); reciever(); } getch(); } /*__________________________________________________________________________*/ void sender() { static int frame_to_send=0; static frame s; packet buffer; event_type event; static int flag=0; if(flag==0) { from_network_layer(&buffer); s.info = buffer; s.seq = frame_to_send; printf("SENDER : Info = %d Seq No = %d ",s.info,s.seq); turn = 'r'; to_physical_layer(&s); flag = 1; } wait_for_event_sender(&event); if(turn=='s') { if(event==frame_arrival) { from_network_layer(&buffer); inc(frame_to_send); s.info = buffer; s.seq = frame_to_send; printf("SENDER : Info = %d Seq No = %d ",s.info,s.seq); turn = 'r'; to_physical_layer(&s); } if(event==timeout) { printf("SENDER : Resending Frame "); turn = 'r'; to_physical_layer(&s); } } } /*__________________________________________________________________________*/ void reciever() { static int frame_expected=0; frame r,s; event_type event; wait_for_event_reciever(&event); if(turn=='r') { if(event==frame_arrival) { from_physical_layer(&r); if(r.seq==frame_expected) { to_network_layer(&r.info); inc(frame_expected); } else printf("RECIEVER : Acknowledgement Resent\n"); turn = 's'; to_physical_layer(&s); } if(event==err) { printf("RECIEVER : Garbled Frame\n"); turn = 's'; //if frame not recieved } //sender shold send it again } } /*__________________________________________________________________________*/ void from_network_layer(packet *buffer) { (*buffer).data = i; i++; } /*___________________________________________________________________________*/ void to_physical_layer(frame *s) { // 0 means error s->err = random(4); //non zero means no error DATA = *s; //probability of error = 1/4 } /*___________________________________________________________________________*/ void to_network_layer(packet *buffer) { printf("RECIEVER :Packet %d recieved , Ack Sent\n",(*buffer).data); if(i>TOT_PACKETS) //if all packets recieved then disconnect { DISCONNECT = 1; printf("\nDISCONNECTED"); } } /*___________________________________________________________________________*/ void from_physical_layer(frame *buffer) { *buffer = DATA; } /*___________________________________________________________________________*/ void wait_for_event_sender(event_type * e) { static int timer=0; if(turn=='s') { timer++; if(timer==TIMEOUT) { *e = timeout; printf("SENDER : Ack not recieved=> TIMEOUT\n"); timer = 0; return; } if(DATA.err==0) *e = err; else { timer = 0; *e = frame_arrival; } } } /*____________________________________________________________________________*/ void wait_for_event_reciever(event_type * e) { if(turn=='r') { if(DATA.err==0) *e = err; else *e = frame_arrival; } } /* OUTPUT SENDER : Info = 1 Seq No = 0 RECIEVER :Packet 1 recieved , Ack Sent SENDER : Ack not recieved=> TIMEOUT SENDER : Resending Frame RECIEVER : Acknowledgement Resent SENDER : Info = 2 Seq No = 1 RECIEVER :Packet 2 recieved , Ack Sent SENDER : Info = 3 Seq No = 0 RECIEVER :Packet 3 recieved , Ack Sent SENDER : Ack not recieved=> TIMEOUT SENDER : Resending Frame RECIEVER : Acknowledgement Resent SENDER : Info = 4 Seq No = 1 RECIEVER :Packet 4 recieved , Ack Sent SENDER : Info = 5 Seq No = 0 RECIEVER :Packet 5 recieved , Ack Sent SENDER : Info = 6 Seq No = 1 RECIEVER :Packet 6 recieved , Ack Sent SENDER : Info = 7 Seq No = 0 RECIEVER :Packet 7 recieved , Ack Sent SENDER : Info = 8 Seq No = 1 RECIEVER : Garbled Frame SENDER : Ack not recieved=> TIMEOUT SENDER : Resending Frame RECIEVER :Packet 8 recieved , Ack Sent DISCONNECTED */
SELECTIVE REPEAT PROTOCOL PROGRAM IN C++
// SIMULATE SELECTIVE REPEAT PROTOCOL # include <iostream.h> # include <conio.h> # include <stdlib.h> # include <time.h> # include <math.h> # define TOT_FRAMES 500 # define FRAMES_SEND 10 class sel_repeat { private: int fr_send_at_instance; int arr[TOT_FRAMES]; int send[FRAMES_SEND]; int rcvd[FRAMES_SEND]; char rcvd_ack[FRAMES_SEND]; int sw; int rw; // tells expected frame public: void input(); void sender(int); void reciever(int); }; void sel_repeat :: input() { int n; // no of bits for the frame int m; // no of frames from n bits cout << "Enter the no of bits for the sequence number "; cin >> n; m = pow (2 , n); int t = 0; fr_send_at_instance = (m / 2); for (int i = 0 ; i < TOT_FRAMES ; i++) { arr[i] = t; t = (t + 1) % m; } for (i = 0 ; i < fr_send_at_instance ; i++) { send[i] = arr[i]; rcvd[i] = arr[i]; rcvd_ack[i] = 'n'; } rw = sw = fr_send_at_instance; sender(m); } void sel_repeat :: sender(int m) { for (int i = 0 ; i < fr_send_at_instance ; i++) { if ( rcvd_ack[i] == 'n' ) cout << " SENDER : Frame " << send[i] << " is sent\n"; } reciever (m); } void sel_repeat :: reciever(int m) { time_t t; int f; int f1; int a1; char ch; srand((unsigned) time(&t)); for (int i = 0 ; i < fr_send_at_instance ; i++) { if (rcvd_ack[i] == 'n') { f = rand() % 10; // if = 5 frame is discarded for some reason // else frame is correctly recieved if (f != 5) { for (int j = 0 ; j < fr_send_at_instance ; j++) if (rcvd[j] == send[i]) { cout << "RECIEVER : Frame " << rcvd[j] << " recieved correctly\n"; rcvd[j] = arr[rw]; rw = (rw + 1) % m; break; } if (j == fr_send_at_instance) cout << "RECIEVER : Duplicate frame " << send[i] << " discarded\n"; a1 = rand() % 5; // if a1 == 3 then ack is lost // else recieved if (a1 == 3) { cout << "(Acknowledgement " << send[i] << " lost)\n"; cout << " (SENDER TIMEOUTS --> RESEND THE FRAME)\n"; rcvd_ack[i] = 'n'; } else { cout << "(Acknowledgement " << send[i] << " recieved)\n"; rcvd_ack[i] = 'p'; } } else { int ld = rand() % 2; // if = 0 then frame damaged // else frame lost if (ld == 0) { cout << "RECIEVER : Frame " << send[i] << " is damaged\n"; cout << "RECIEVER : Negative acknowledgement " << send[i] << " sent\n"; } else { cout << "RECIEVER : Frame " << send[i] << " is lost\n"; cout << " (SENDER TIMEOUTS --> RESEND THE FRAME)\n"; } rcvd_ack[i] = 'n'; } } } for ( int j = 0 ; j < fr_send_at_instance ; j++) { if (rcvd_ack[j] == 'n') break; } i = 0 ; for (int k = j ; k < fr_send_at_instance ; k++) { send[i] = send[k]; if (rcvd_ack[k] == 'n') rcvd_ack[i] = 'n'; else rcvd_ack[i] = 'p'; i++; } if ( i != fr_send_at_instance ) { for ( int k = i ; k < fr_send_at_instance ; k++) { send[k] = arr[sw]; sw = (sw + 1) % m; rcvd_ack[k] = 'n'; } } cout << "Want to continue..."; cin >> ch; cout << "\n"; if (ch == 'y') sender(m); else exit(0); } void main() { clrscr(); sel_repeat sr; sr.input(); getch(); } /* OUTPUT Enter the no of bits for the sequence number 6 SENDER : Frame 24 is sent SENDER : Frame 32 is sent SENDER : Frame 32 is sent SENDER : Frame 27 is sent SENDER : Frame 28 is sent SENDER : Frame 29 is sent SENDER : Frame 30 is sent SENDER : Frame 31 is sent RECIEVER : Frame 24 is lost (SENDER TIMEOUTS --> RESEND THE FRAME) RECIEVER : Frame 32 recieved correctly (Acknowledgement 32 recieved) RECIEVER : Frame 33 recieved correctly (Acknowledgement 34 recieved) RECIEVER : Frame 27 recieved correctly (Acknowledgement 34 recieved) RECIEVER : Frame 28 is damaged RECIEVER : Negative acknowledgement 28 sent RECIEVER : Frame 29 recieved correctly (Acknowledgement 35 recieved) RECIEVER : Frame 30 recieved correctly (Acknowledgement 36 lost) (SENDER TIMEOUTS --> RESEND THE FRAME) RECIEVER : Frame 31 recieved correctly (Acknowledgement 37 recieved) Want to continue...n */
Labels:
c plus plus,
computer networks,
cpp program,
csa,
fraMes,
networks,
selective repeat protocol
GO BACK N PROTOCOL PROGRAM IN C++
# include <iostream.h> # include <conio.h> # include <stdlib.h> # include <time.h> # include <math.h> # define TOT_FRAMES 500 # define FRAMES_SEND 10 class gobkn { private: int fr_send_at_instance; int arr[TOT_FRAMES]; int arr1[FRAMES_SEND]; int sw; int rw; // tells expected frame public: gobkn(); void input(); void sender(int); void reciever(int); }; gobkn :: gobkn() { sw = 0; rw = 0; } void gobkn :: input() { int n; // no of bits for the frame int m; // no of frames from n bits cout << "Enter the no of bits for the sequence no "; cin >> n; m = pow (2 , n); int t = 0; fr_send_at_instance = (m / 2); for (int i = 0 ; i < TOT_FRAMES ; i++) { arr[i] = t; t = (t + 1) % m; } sender(m); } void gobkn :: sender(int m) { int j = 0; for (int i = sw ; i < sw + fr_send_at_instance ; i++) { arr1[j] = arr[i]; j++; } for (i = 0 ; i < j ; i++) cout << " SENDER : Frame " << arr1[i] << " is sent\n"; reciever (m); } void gobkn :: reciever(int m) { time_t t; int f; int f1; int a1; char ch; srand((unsigned) time(&t)); f = rand() % 10; // if = 5 frame is discarded for some reason // else they are correctly recieved if (f != 5) { for (int i = 0 ; i < fr_send_at_instance ; i++) { if (rw == arr1[i]) { cout << "RECIEVER : Frame " << arr1[i] << " recieved correctly\n"; rw = (rw + 1) % m; } else cout << "RECIEVER : Duplicate frame " << arr1[i] << " discarded\n"; } a1 = rand() % 15; // if a1 belongs to 0 to 3 then // all ack after this (incl this one) lost // else // all recieved if (a1 >= 0 && a1 <= 3) { cout << "(Acknowledgement " << arr1[a1] << " & all after this lost)\n"; sw = arr1[a1]; } else sw = (sw + fr_send_at_instance) % m; } else { f1 = rand() % fr_send_at_instance; // f1 gives index of the frame being lost for (int i = 0 ; i < f1 ; i++) { if (rw == arr1[i]) { cout << " RECIEVER : Frame " << arr1[i] << " recieved correctly\n"; rw = (rw + 1) % m; } else cout << " RECIEVER : Duplicate frame " << arr1[i] << " discarded\n"; } int ld = rand() % 2; // ld == 0 frame damaged // else frame lost if (ld == 0) cout << " RECIEVER : Frame " << arr1[f1] << " damaged\n"; else cout << " (Frame " << arr1[f1] << " lost)\n"; for (i = f1 + 1 ; i < fr_send_at_instance ; i++) cout << " RECIEVER : Frame " << arr1[i] << " discarded\n"; cout << " (SENDER TIMEOUTS --> RESEND THE FRAME)\n"; sw = arr1[f1]; } cout << "Want to continue..."; cin >> ch; if (ch == 'y') sender(m); else exit(0); } void main() { clrscr(); gobkn gb; gb.input(); getch(); } /* OUTPUT Enter the no of bits for the sequence no 4 SENDER : Frame 0 is sent SENDER : Frame 1 is sent SENDER : Frame 2 is sent SENDER : Frame 3 is sent SENDER : Frame 4 is sent SENDER : Frame 5 is sent SENDER : Frame 6 is sent SENDER : Frame 7 is sent RECIEVER : Frame 0 recieved correctly RECIEVER : Frame 1 recieved correctly RECIEVER : Frame 2 recieved correctly RECIEVER : Frame 3 recieved correctly RECIEVER : Frame 4 recieved correctly RECIEVER : Frame 5 recieved correctly RECIEVER : Frame 6 recieved correctly RECIEVER : Frame 7 recieved correctly Want to continue...n */
Labels:
c plus plus,
computer networks,
cpp program,
fraMes,
go back n protocol,
networks
DISTANCE VECTOR ROUTING PROGRAM IN C++
// SIMULATE DISTANCE VECTOR ROUTING // ALWAYS DRY RUN FOR BETTER UNDERSTANDING # include <iostream.h> # include <conio.h> # include <stdio.h> # include <string.h> class router { private: char name[20]; int dist[10]; public: void get_r_table (int , int); friend void find_table (router *); }; void find_table (router *); int total_r; int adj; char names[10][20]; char N[20]; void router :: get_r_table (int no , int flag) { if ( flag == 1 ) { cout << "Enter the name of the adjacent router " << no << " : "; gets (name); } else strcpy (name , N); cout << "Enter the distances of each router from this router :\n"; cout << "\tROUTER\tDISTANCE\n"; cout << "\t------\t--------\n"; for (int i = 0 ; i < total_r ; i++) { cout << "\t" << names[i] << "\t"; if ( strcmp ( names[i] , name ) == 0 ) { dist[i] = 0; cout << dist[i] << "\n"; } else cin >> dist[i]; } } void find_table(router *r) { cout << "Routing table for router " << N << " :-\n"; cout << "\tRouter\tDistance\n"; cout << "\t------\t--------\n"; for ( int i = 0 ; i < total_r ; i++ ) { int temp; if ( strcmp (N , names[i] ) == 0 ) temp = 0; else { temp = r[0].dist[i]; for ( int j = 1 ; j < adj + 1 ; j++ ) if ( r[j].dist[i] < temp && r[j].dist[i] != 0 ) temp = r[j].dist[i] + r[0].dist[j]; } r[0].dist[i] = temp; // r[0] contains data of the router whose routing table is to be found out } for ( i = 0 ; i < total_r ; i++ ) { cout << "\t" << names[i] << "\t"; cout << r[0].dist[i] << "\n"; } } void main() { router r[10]; clrscr(); cout << "Enter the total no of routers in the subnet [MAX 10] :- "; cin >> total_r; cout << "Enter the names of all the routers :-\n"; for (int i = 0 ; i < total_r ; i++) { cout << "\t" << i + 1 << " . "; gets (names[i]); } cout << "Enter the router whose routing table is to be found out : "; gets (N); cout << "Enter the no of routers adjacent to this router : "; cin >> adj; int flag = 0; for ( i = 0 ; i < adj + 1; i++ ) { r[i].get_r_table (i , flag); flag = 1; } find_table (r); getch(); } /* OUTPUT Enter the total no of routers in the subnet [MAX 10] :- 6 Enter the names of all the routers :- 1 . A 2 . B 3 . C 4 . D 5 . E 6 . F Enter the router whose routing table is to be found out : C Enter the no of routers adjacent to this router : 3 Enter the distances of each router from this router : ROUTER DISTANCE ------ -------- A 11 B 6 C 0 D 3 E 5 F 6 Enter the name of the adjacent router 1 : B Enter the distances of each router from this router : ROUTER DISTANCE ------ -------- A 5 B 0 C 8 D 12 E 6 F 2 Enter the name of the adjacent router 2 : D Enter the distances of each router from this router : ROUTER DISTANCE ------ -------- A 16 B 12 C 6 D 0 E 9 F 10 Enter the name of the adjacent router 3 : E Enter the distances of each router from this router : ROUTER DISTANCE ------ -------- A 7 B 6 C 3 D 9 E 0 F 1 Routing table for router C :- Router Distance ------ -------- A 10 B 6 C 0 D 3 E 5 F 4 */
Labels:
c plus plus,
computer networks,
cpp program,
networks
Cyclic Redundancy Check (CRC) Program in C++
// SIMULATE CYCLIC REDUNDANCY CHECK (CRC) #include<iostream.h> #include<conio.h> #include<math.h> #include<stdlib.h> #include<graphics.h> #include<DOS.h> int digitinbin(int number) { int i=0; int n=number; while(n!=0) { n=n/2; i++; } return i; } int bintodec(int *a,int d) { int num=0; for(int i=0;i<d;i++) { if(a[i]==1) num=num+pow(2,i); } return num; } int* dectobin(int n) { int *A=new int[32]; int i=0; int num=n; while(num!=0) { A[i]=num%2; num=num/2; i++; } return A; } int sender(int mess,int poly) { int k=0,num1; int d1=digitinbin(poly); mess=mess*pow(2,d1-1); int d2=digitinbin(mess); int *B=dectobin(mess); int counter=0; int times=d2-d1+1; int *temp=new int[d1]; int a=pow(2,d1-1); while(counter<times) { int j=0; if(counter==0) { for(int i=times-counter-1;i<d2-counter;i++) { temp[j]=B[i]; j++; } num1=bintodec(temp,j); } int num2=poly; if(counter!=0) num1=k; if(num1>=a) k=num1^num2; else k=num1; if(d2-d1-counter-1>=0) { if(B[d2-d1-counter-1]==1) k=k*2+1; else k=k*2; } counter++; } return (k^mess); } void receiver(int mess,int poly) { int k=0,num1; int d1=digitinbin(poly); int d2=digitinbin(mess); int *B=dectobin(mess); int counter=0; int times=d2-d1+1; int *temp=new int[d1]; int a=pow(2,d1-1); while(counter<times) { int j=0; if(counter==0) { for(int i=times-counter-1;i<d2-counter;i++) { temp[j]=B[i]; j++; } num1=bintodec(temp,j); } int num2=poly; if(counter!=0) num1=k; if(num1>=a) k=num1^num2; else k=num1; if(d2-d1-counter-1>=0) { if(B[d2-d1-counter-1]==1) k=k*2+1; else k=k*2; } counter++; } if(k==0) cout<<"MESSAGE TRANSMITTED SUCCESSFULLY\n"<<endl; else cout<<"ERROR IN TRANSMISSION\n"<<endl; } void main() { clrscr(); int message,genpoly,ch=1; cout<<"enter the number to be transmitted"<<endl; cin>>message; cout<<"Enter the generator polynomial number"<<endl; cin>>genpoly; int gp=sender(message,genpoly); while(ch==1 || ch==2) { cout<<"MENU"<<endl; cout<<"1-SUCCESSFUL TRANSMISSION"<<endl; cout<<"2-ERROR IN TRANSMISSION"<<endl; cout<<"PRESS ANY OTHER KEY TO EXIT"<<endl; cout<<"Enter your choice"<<endl; cin>>ch; if(ch==1) { cout<<"TRANSMITTING MESSAGE .......\n"<<endl; delay(5000); receiver(gp,genpoly); int *B=dectobin(message); int dig=digitinbin(message); cout<<"The transmitted message is "; for(int i=dig-1;i>=0;i--) cout<<B[i]; cout<<"\n"<<endl; } else if(ch==2) { cout<<"TRANSMITTING MESSAGE .......\n"<<endl; delay(5000); receiver(gp+1,genpoly); cout<<endl; } } cout<<"PROGRAM ENDS"<<endl; getch(); } /* OUTPUT enter the number to be transmitted 729 Enter the generator polynomial number 19 MENU 1-SUCCESSFUL TRANSMISSION 2-ERROR IN TRANSMISSION PRESS ANY OTHER KEY TO EXIT Enter your choice 1 TRANSMITTING MESSAGE ....... MESSAGE TRANSMITTED SUCCESSFULLY The transmitted message is 1011011001 MENU 1-SUCCESSFUL TRANSMISSION 2-ERROR IN TRANSMISSION PRESS ANY OTHER KEY TO EXIT Enter your choice 2 TRANSMITTING MESSAGE ....... ERROR IN TRANSMISSION MENU 1-SUCCESSFUL TRANSMISSION 2-ERROR IN TRANSMISSION PRESS ANY OTHER KEY TO EXIT Enter your choice 4 PROGRAM ENDS */
Labels:
c plus plus,
computer networks,
cpp program,
networks
Subscribe to:
Posts (Atom)