Search This Blog

Showing posts with label computernetworks. Show all posts
Showing posts with label computernetworks. Show all posts

Monday, 4 April 2016

Distance Vector Processing

Explained Logic here! You can set it with input from user and here is the problem
we have to set new routing table for j !




#include<stdio.h>

int main()
{
int A[12]={0,12,25,40,14,23,18,17,21,9,24,29};
int B[12]={24,36,18,27,7,20,31,20,0,11,22,33};
int C[12]={20,31,19,8,30,19,6,0,14,7,22,9};
int D[12]={21,28,36,24,22,40,31,19,22,10,0,9};

char str[12];
//void sum(,;
int q,t[4],z,s,zz=0;
int j[13];
int a=8,i=10,h=12,k=6;
int min;
for(z=0;z<12;z++)
{
j[9]=0;
t[0]=A[z]+a;
t[1]=B[z]+i;
t[2]=C[z]+h;
t[3]=D[z]+k;
min=t[0];
for(q=0;q<4;q++)
{
if(t[q]<min){
min=t[q];
s=q;
}
}
if(s==0)
{
str[zz]='A';
zz++;
}else if(s==1)
{
str[zz]='I';
zz++;
}else if(s==2)
{
str[zz]='H';
zz++;
}else if(s==3)
{
str[z]='K';
zz++;
}
str[9]='-';
j[z]=min;
}
for(q=0;q<12;q++)
{
printf("%d  %c\n",j[q],str[q]);
}
return 0;
}

Monday, 29 February 2016

CRC Error Detection (Dynamic)

#include<stdio.h>
int main()
{
int a[10],d[10],k[10],z[10],i,n,q,x=1,s,m,j;
printf("Enter Total Data Bits");
scanf("%d",&n);
printf("Enter Total Divisor Bits");
scanf("%d",&q);
for(i=0;i<n;i++)
{
    printf("Enter Data");
    scanf("%d",&a[i]);
}
for(i=0;i<q;i++)
{
    printf("Enter divisor");
    scanf("%d",&d[i]);
}
for(i=0;i<q;i++)
{
    printf("Enter 0's for zero detection");
    scanf("%d",&z[i]);
}
printf("\nData Before Entering temp 0's\n");
for(i=0;i<n;i++)
{
    printf("%d",a[i]);
}
s=n+(q-1);
printf("\n");
for(i=n;i<s;i++)
{
    printf("Enter 0's ");
    scanf("%d",&a[i]);
}
printf("\nData After Entering temp 0's\n");
for(i=0;i<s;i++)
{
    printf("%d",a[i]);
}
i=0;
if(a[i]==1)
{
    for(i=0;i<q;i++)
    {
    if(a[i]==d[i])
    {
        k[i]=0;
    }
    else
    {
        k[i]=1;
    }
    }
    k[i]=a[q];
    for(j=0;j<q;j++)
    {
        k[j]=k[j+1];
    }
}
else
{
    for(i=0;i<q;i++)
    {
    if(a[i]==z[i])
    {
        k[i]=0;
    }
    else
    {
        k[i]=1;
    }
    }
    k[i]=a[q];
    for(j=0;j<q;j++)
    {
        k[j]=k[j+1];
    }
}
printf("\nK:");
for(i=0;i<q;i++)
{
    printf("%d",k[i]);
}
m=q;
do
{
x=x+1;
i=0;
if(k[i]==1)
{
    for(i=0;i<q;i++)
    {
    if(k[i]==d[i])
    {
        k[i]=0;
    }
    else
    {
        k[i]=1;
    }
    }
    m++;
    k[i]=a[m];
    for(j=0;j<q;j++)
    {
        k[j]=k[j+1];
    }
}
else
{
    for(i=0;i<q;i++)
    {
    if(k[i]==z[i])
    {
        k[i]=0;
    }
    else
    {
        k[i]=1;
    }
    }
    m++;
    k[i]=a[m];
    for(j=0;j<q;j++)
    {
        k[j]=k[j+1];
    }
}
printf("\nK:");
for(i=0;i<q;i++)
{
    printf("%d",k[i]);
}
}
while(x<n);
printf("\nCRC = ");
if(x==n)
{
    for(i=0;i<q-1;i++)
    {
    printf("%d",k[i]);
    }
}
}


Sunday, 28 February 2016

Checksum error detecction

Note : lets say if we have sum of number in array is 36 then we convert into binary then if the number is >4 then we have to divide it into 4 digits and do a binary summation of that two parts so this code is only for who's binary number is less or equal then 8. then we have to do a complement of that number thus in case of 36 ans will be 1001 now this is binary of 9. so again if you add 9  into again that data then it will be 45 now again will do the same procedure so the answer will be 0. it means there is no error else error in data!

#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,a[10],i,sum=0;
clrscr();
cout<<"Enter size of digits:";
cin>>n;

for(i=0;i<n;i++)
{
cin>>a[i];
sum = sum + a[i];
}
cout<<"sum of digit is:"<<sum;

n=sum;
int bin[10];
int r;
int p=0,j;
while(n!=0)
{
r=n%2;
n=n/2;
bin[p]=r;
p++;
}
int c=p-1;
cout<<"\nBinary is:";
for(j=c;j>=0;j--)
{
cout<<bin[j];
}
//parting two division of max 8 digits of that sum of digit
int x1[15],x2[15],q;

if(c==7)
{
for(j=c-4,q=0;j>=0;j--,q++)
{
x1[q]=bin[j];
}
for(j=c,q=0;j>=4;j--,q++)
{

x2[q]=bin[j];
}
}
else if(c==6)
{
for(j=c-3,q=0;j>=0;j--,q++)
{
x1[q]=bin[j];
}
x2[0]=0;
for(j=c,q=1;j>=4;j--,q++)
{

x2[q]=bin[j];
}
}
else if(c==5)
{
for(j=c-2,q=0;j>=0;j--,q++)
{
x1[q]=bin[j];
}
x2[0]=0;
x2[1]=0;
for(j=c,q=2;j>=4;j--,q++)
{

x2[q]=bin[j];
}
}
else if(c==4)
{
for(j=c-1,q=0;j>=0;j--,q++)
{
x1[q]=bin[j];
}
x2[0]=0;
x2[1]=0;
x2[2]=0;
for(j=c,q=3;j>=4;j--,q++)
{

x2[q]=bin[j];
}
}
else
{
//4 bits binary
for(j=c,q=0;j>=0;j--,q++)
{
x1[q]=bin[j];
}
x2[0]=0;
x2[1]=0;
x2[2]=0;
x2[3]=0;

}
int s=0;
for(q=0;q<4;q++)
{
      s=10*s+x1[q];
}

int t=0;
int mul=1;
for(q=0;q<4;q++)
{
      t=10*t+x2[q];
}

int deci1=0,deci2=0,rem,z=0;
while(t!=0)
{
rem=t%10;
t/=10;
deci1+=rem*pow(2,z);
z++;
}
z=0;
while(s!=0)
{
rem=s%10;
s/=10;
deci2+=rem*pow(2,z);
z++;
}
int deci = deci1+deci2;
int fibin[10];
z=0;
p=0;
while(deci!=0)
{
rem=deci%2;
deci/=2;
fibin[z]=rem;
z++;
}


if(z<4)
{
fibin[z]=0;
for(int k=z;k>=0;k--)
{
if(fibin[k]==0)
{
fibin[k]=1;
}
else
{
fibin[k]=0;
}
}
cout<<"\nfinal sum with complement is:"<<endl;
for(k=z;k>=0;k--)
{
cout<<fibin[k];
}

}
else
{
for(int k=z-1;k>=0;k--)
{
if(fibin[k]==0)
{
fibin[k]=1;
}
else
{
fibin[k]=0;
}
}
cout<<"\nfinal sum with complement is:"<<endl;
for(k=z-1;k>=0;k--)
{
cout<<fibin[k];
}
}

int ans=0;
if(z==3)
{
for(q=z;q>=0;q--)
{
      ans=10*ans+fibin[q];
}
}
else
{
for(q=z-1;q>=0;q--)
{
ans=10*ans+fibin[q];
}
}
int check=0;
q=0;
while(ans!=0)
{
rem=ans%10;
ans/=10;
check+=rem*pow(2,q);
q++;
}
cout<<"\nans of complement is "<<check;

getch();
return 0;
}


Enter n
1
enter data members
36

checksum is 9

Hamming code

#include<stdio.h>
int main()
{
int n,i,a[4],b[4],r[7],s[3],j;
printf("Enter datawrd of 4 bits");
for(i=0,j=3;i<4;i++,j--)
{
scanf("%d",&r[i]);
a[j]=r[i];
}
r[6]=(a[0]+a[1]+a[2])%2;
r[5]=(a[1]+a[2]+a[3])%2;
r[4]=(a[0]+a[1]+a[3])%2;

printf("\nWhat to be send is ->");
for(i=0;i<7;i++)
{
printf("%d",r[i]);
}

printf("\nEnter 7 bit which received\n");
for(i=3;i>=0;i--)
{
scanf("%d",&b[i]);
}
for(i=4;i<7;i++)
{
scanf("%d",&b[i]);
}

s[2]=(b[1]+b[3]+b[0]+b[4])%2;
s[1]=(b[1]+b[3]+b[2]+b[5])%2;
s[0]=(b[0]+b[2]+b[1]+b[6])%2;
n=s[2]+s[1]+s[0];
if(n==0)
{
printf("\n No error ! codeword is: ");
for(i=0;i<4;i++)
{
printf("%d",r[i]);
}
}
else if(n==1)
{
printf("\nError is at redundant bit");
if(s[0]==1)
{
printf("\nError is at bit b[6]");
s[0]=!s[0];
printf("%d",s[0]);
}
else if(s[1]==1)
{
printf("\nError is at bit b[5]");
s[1]=!s[1];
}
else
{
printf("\nError is at bit b[4]");
s[2]=!s[2];
}
printf("\n The data word is :");
for(i=0;i<4;i++)
{
printf("%d",b[i]);
}
}
else if(n==2)
{
if(s[0]==1 && s[1]==1)
{
printf("\nError is at bit b[1]");
b[2]=!b[2];
}
else if(s[1]==1 && s[2]==1)
{
printf("\nError is at bit b[0]");
b[3]=!b[3];
}
else
{
printf("\nError is at bit b[3]");
b[0]=!b[0];
}
printf("\n The data word is:");
for(i=0;i<4;i++)
{
printf("%d",r[i]);
}
}
else if(n==3)
{
b[1]=!b[1];
printf("\nError is at bit b[2]");
printf("\nThe data word is:");

for(i=0;i<4;i++)
{
printf("%d",r[i]);
}
}
return 0;
}


Enter dataword :
0 0 0 0

enter received bit
1 0 0 0 0 0 0

error in b[0]

Saturday, 27 February 2016

CRC error detection (4 bit only)!

Cyclic redundancy Check


Note : This is for only 4 bit !!!

Example : dataword : 1 0 0 1
                  divisor : 1 0 1 1
                  crc : 1 1 0

now if you add 1 1 0 in below example instead of array of zero's then the final ans should be 0 0 0 it means no error otherwise there is error!


CODE:

#include<stdio.h>

int main()
{
    int i,j,a[15],b[10],c[10],k,z,zero[4];
    zero[0]=0;
    zero[1]=0;
    zero[2]=0;
    printf("\nEnter dataword");
    for(i=0;i<4;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("\nEnter divisor");
    for(j=0;j<4;j++)
    {
        scanf("%d",&b[j]);
    }
   
    if(a[0] == 1)
    {
        for(k=0;k<4;k++)
        {
            if(a[k]==b[k])   
            {
                c[k]=0;
            }
            else
            {
                c[k]=1;
            }
        }
        c[k]=0;   
        for(z=0;z<4;z++)
        {
        c[z]=c[z+1];
        }
    }else

    {
        for(k=0;k<4;k++)
        {
            if(a[k] == zero[k])   
            {
                c[k]=0;
            }
            else
            {
                c[k]=1;
            }
        }
   
        c[k]=0;
        for(z=0;z<4;z++)
        {
        c[z]=c[z+1];
   
        }
    }
   
    if(c[0]==1)
    {
            for(k=0;k<4;k++)
            {
            if(c[k]==b[k])   
            {
                c[k]=0;
            }
            else
            {
                c[k]=1;
            }
        }
        c[k]=0;
   
    for(z=0;z<4;z++)
    {
        c[z]=c[z+1];
   
    }
       
    }else{
            for(k=0;k<4;k++)
        {
            if(c[k] == zero[k])   
            {
                c[k]=0;
            }
            else
            {
                c[k]=1;
            }
        }
   
    c[k]=0;
    for(z=0;z<4;z++)
    {
        c[z]=c[z+1];
   
    }

    }
   
    if(c[0]==1)
    {
            for(k=0;k<4;k++)
            {
            if(c[k]==b[k])   
            {
                c[k]=0;
            }
            else
            {
                c[k]=1;
            }
        }
        c[k]=0;
   
    for(z=0;z<4;z++)
    {
        c[z]=c[z+1];
   
    }
       
    }else{
            for(k=0;k<4;k++)
        {
            if(c[k] == zero[k])   
            {
                c[k]=0;
            }
            else
            {
                c[k]=1;
            }
        }
   
    c[k]=0;
    for(z=0;z<4;z++)
    {
        c[z]=c[z+1];
   
    }
        }
   
    for(z=0;z<4;z++)
    {
        c[z]=c[z+1];
   
    }
   
    for(z=0;z<3;z++)
    {
        printf("%d",c[z]);
    }   
   
    return 0;
}


enter dataword:
1 0 0 1

enter divisor :
1 0 1 1

crc is : 1 1 0

Tuesday, 2 February 2016

Rail-Fence Cipher Program

#include<iostream>
#include<string.h>
#include<math.h>

using namespace std;

const int size=1000;

class Matrix
{
int d,l;
char a[size][size];
public:
Matrix(char[]);
void display();
};

Matrix::Matrix(char x[])
{
l=strlen(x);
int flag=1;
cout << "\t\tEnter depth of the text:";
cin >> d;
int j=0;
for(int i=1;i<=l;i++)
{
int flag2=0;
for(int k=0;k<d;k++)
{
if(j==k && flag2==0)
{
a[j][i-1]=x[i-1];
if(i%d==0)
flag=flag*-1;
j+=flag;
if(j<0)
j=1;
flag2=1;
}
else
a[k][i-1]='$';
}
}
}

void Matrix::display()
{
for(int i=0;i<d;i++)
{
for(int j=0;j<l;j++)
{
if(a[i][j]!='$')
cout << a[i][j];
}
}
}

int main()
{
char a[1000];
cout << "\t\tEnter string:";
cin >> a;
Matrix c(a);
c.display();
return 0;
}

enter string :
abcdef

a        e
  b   d    f
    c
enter depth :
3

aebdfc

Trans positional cipher program

#include<iostream>
#include<string.h>
#include<math.h>

using namespace std;

const int size=10;

class Matrix
{
float n;
char a[size][size];
int key[size];
public:
Matrix(char[]);
void display();
};

Matrix::Matrix(char x[])
{
int u=strlen(x);
n=ceil(sqrt(u));
int k=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(k<u)
a[i][j]=x[k++];
else
a[i][j]='$';
}
}
}

void Matrix::display()
{
cout << "\t\tEnter key using space";
for(int i=0;i<n;i++)
cin >> key[i];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[j][key[i]-1]!='$')
cout << a[j][key[i]-1];
}
}
}

int main()
{
char a[1000];
cout << "\t\tEnter string:";
cin >> a;
Matrix c(a);
c.display();
return 0;
}


input 

1 2 3
4 5 6
7 8 9
enter key: 3 2 1
output : 
369258147

Monday, 18 January 2016

Cipher text example with file handling

 Simple Cipher text with file Handling
Input : hello karan gajjar
key : 3
Output : khoor ndudq jdmmdu

As we know encryption and decryption. I have kept input in encode.txt file and aftyer entering key value the decrypted text will be stored in decode.txt file!

CODE:

 #include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    int i,key,k;
    char str[100],str2[100];
    FILE *fp,*fq;
    fp=fopen("encode.txt","r");
    fgets(str,sizeof(str),fp);
    puts(str);
    fq=fopen("decode.txt","a");
    clrscr();
    printf("Enter Key:");
    scanf("%d",&key);
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]>=97 && str[i]<120)
        {
            str[i]=str[i]+key;
            str2[i]=str[i];
        }
        else if(str[i]==32)
        {
            str2[i]=str[i];
        }
        else
        {
            str[i]=str[i]-26+key;
            str2[i]=str[i];
        }
    }
    str2[i]='\0';
    puts(str2);
    fprintf(fq,"\n");
    fprintf(fq,str2);
    fclose(fq);
    fclose(fp);
    getch();
}


NOTE: You can add more elseif condition for "," , "!" , "?" this type of ascii value i have added for "space".
              and yes in this example you can't add encoded message with every new line!