Chessboard distance or Minkowski distance

Consider two objects and describe by 10 features and write a c-program to compute Lr distance by the varying the “r-value”.

The C-program automatically reads the 10 random number and the value of r going to increment automatically up to 50. Our infinity value is set to 50, can be changed to the desired value. the r-value is 1 then it is called the city-block distance, if the value of r is 2 then it is called Euclidean distance and if the value of r is infinity then it is called as a chessboard or Lr distance, supremum distance (also referred to as Lmax, L norm and as the Chebyshev distance) is a generalization of the Minkowski distance for L → ∞.

The C-program automatically reads the 10 random number and the value of r going to increment automatically up to 50. Our infinity value is set to 50, can be changed to the desired value.

The c-program is as follows:



#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<float.h>
int main()
{
    long int i,j,r,max,a[10],b[10];
    double c[10],sum;
    float k;

    printf("reading\n\n    object1\t object2\n");

    for (i=0;i<10;i++)
    {
        a[i] = rand() % 10 + 1;
        b[i] = rand() % 15 + 3;
        printf("\t%d\t %d\n ",a[i],b[i]);
    }

    c[0]=abs(a[0]-b[0]);
    max = c[0];
//    printf("max is %d\n",c[0]);

    printf("the absolute values of each differences \n");

    for (i=0;i<10;i++)
    {
        c[i]=abs(a[i]-b[i]);
        printf("%.f\n",c[i]);
        if (c[i]>max)
            max=c[i];
    }
    printf( "\nmaximum value is %d\n\n\n" , max );

    for ( r=1;r<=50;r++)
    {
        sum = 0;
        for (i=0;i<10;i++)
        {
            sum += pow(c[i],r);
        }
        k = (float) 1 / (float)r;
        c[i] =  pow(sum,k);
        printf("%.2f\t%.f\t%d %f\n", k,sum,r,c[i]);
    }
    c[r+1]=max;
    printf("\n%ld\t%f\n",r,c[r+1]);
    return 0;
}


 Output:

Fig: Output up to first 16 values
Fig: Output values from 17 to 51

Academic students can ask me their queries related to projects and assignments using the comment section.