Sunday, October 18, 2015

C Program to find the Hamming Distance of all consecutive decimal numbers in an array, find the maximum hamming distance and print the index of these two number.

/* =====================================================================================
 * *
 * *       Filename:  hamdist.c
 * *        Created:  Tuesday 3 February 2015
 * *       Compiler:  gcc
 * *         Author:  Manoj Kumar Patra,manojpatra.sit@gmail.com
 * *   Organization:  SCIS,University of Hyderabad
 * *
 * * =====================================================================================*/

#include<stdio.h>
int decimal_binary(int ar, int n1[]);
int hamming_distance(int ar[], int ar1[],int p,int q);
int main()
{
int k=0,ni,c1,c2,hd,maxhd=0,minhd=1000;
int no1,no2,maxi1,maxi2,mini1,mini2;
int arr[100],i=1,j;
printf("\n--------------------------------------------------------\n");
printf("How many integer you wanna insert:");
scanf("%d",&ni);
printf("Enter the elements in to the array:\n");
for(j=0; j<ni; j++)
{
scanf("%d",&arr[j]);
}
printf("\nThe elements of the array are:\n");
for(j=0; j<ni; j++)
{
printf("%d\t",arr[j]);
}
printf("\n--------------------------------------------------------\n");
for(j=0; j<ni-1; j++)
{
int n1[100],n2[100];
c1=decimal_binary(arr[j],n1);
c2=decimal_binary(arr[j+1], n2);
printf("The binary value of %d is:",arr[j]);
for(k=0; k<c1; k++)
{
printf("%d",n1[k]);
}
printf("\nThe binary value of %d is:",arr[j+1]);
for(k=0; k<c2; k++)
{
printf("%d",n2[k]);
}
hd=hamming_distance(n1,n2,c1,c2);
printf("\nHamming distance between above two binary number is:%d",hd);
if(hd > maxhd)
{
maxhd=hd;
no1=arr[j];
no2=arr[j+1];
maxi1=j;
maxi2=j+1;
}
else if(hd < minhd)
{
minhd=hd;
no1=arr[j];
no2=arr[j+1];
mini1=j;
mini2=j+1;
}
else{}
printf("\n--------------------------------------------------------\n");
}
printf("The maximum hamming distance is betweeen %d and %d=%d\n",arr[maxi1],arr[maxi2],maxhd);
printf("The index of the two numbers are: %d and %d\n",maxi1,maxi2);
printf("The minimum hamming distance is betweeen %d and %d=%d\n",arr[mini1],arr[mini2],minhd);
printf("The index of the two numbers are: %d and %d\n",mini1,mini2);
printf("\n");
return 0;
}
int decimal_binary(int dn, int ar[])
{
int k=0,quo = dn;
int i=1,j;
int binaryNumber[100];
while(quo!=0)
{
binaryNumber[i++]= quo % 2;
quo = quo / 2;
}
for(j = i -1 ;j> 0;j--)
{
ar[k]=binaryNumber[j];
k++;
}
return k;
}
int hamming_distance(int ar[], int ar1[],int p,int q)
{
int i,j,count=0;
if(p == q)
{
for(i=0; i<p; i++)
{
if(ar[i] != ar1[i])
{
count++;
}
}
return count;
}
else if(p > q)
{
i=p-1;
for(j=q-1; j>=0; j--)
{
if(ar1[j] != ar[i])
{
count++;
}
i--;
}
for(i=p-q-1; i>=0; i--)
{
if(ar[i] == 1)
{
count++;
}
}
return count;
}
else
{
j=q-1;
for(i=p-1; i>=0; i--)
{
if(ar[i] != ar1[j])
{
count++;
}
j--;
}
for(j=q-p-1; j>=0; j--)
{
if(ar1[j] == 1)
{
count++;
}
}
return count;

}
}

OutPut:


1 comment:

  1. Hi Dear,

    I Like Your Blog Very Much. I see Daily Your Blog, is A Very Useful For me.

    You can also Find Maxi2 herbal male enhancement pills Buy Maxi2 herbal male enhancement pills at only

    $24/pack. Buy original Maxi2 to boost your sexual performance in bed. Solve your erectile dysfunction Now.

    Visit Now:- https://www.sexpills.tips/maxi2pills

    ReplyDelete