Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Jaccard index of the two sets A and B is the size of their intersection divi

ID: 3763409 • Letter: T

Question

The Jaccard index of the two sets A and B is the size of their intersection divided by the size of their union, |A B| /|A B| . It is a statistical measure used in recommendation systems for comparing the similarity of two sample sets. For example, Amazon uses it to find the similarity between items or users and recommend new items to users. Write a program jaccard.c that asks the user for a positive integer length and then for the integer elements of two arrays of the desired length. The program prints, the double Jaccard index of the two arrays with a two-digit precision.

Explanation / Answer

Program for jaccard index:

---------------------------------

#include <stdio.h>
#include <stdlib.h>

//declaration of Set structure
typedef struct
{
   int elements[40]; //elements of set
   int len; //length of set
} Set;

//function to calculate intersection of two sets
Set calculate_intersection(int* A,int* B,int lenA, int lenB)
{
   int i,j,k=0, temp;
   Set intersectionAB;
   for(i=0;i<lenA;i++)
   {
       temp=A[i];
       for(j=0;j<lenB;j++)
       {
           if(temp==B[j])
           {
               intersectionAB.elements[k]=temp;
               k++;
               break;
           }
       }
   }
   intersectionAB.len=k;
   return intersectionAB;
}

//function to calculate union of two sets
Set calculate_union(int* A,int* B,int lenA, int lenB)
{
   int i,j,k=0, temp, found;
   Set unionAB;
   for(i=0;i<lenA;i++)
   {
       unionAB.elements[k]=A[i];
       k++;
   }
   for(j=0;j<lenB;j++)
   {
       temp=B[j];
       found=0;
       for(i=0;i<lenA;i++)
       {
           if(temp==A[i])
           {
               found=1;
               break;
           }
       }
       if(found==0)
       {
           unionAB.elements[k]=temp;
           k++;
       }
   }
   unionAB.len=k;
   return unionAB;
}

//main function
int main(void) {
   int A[20],B[20],lenA, lenB,i;
   Set intersectionAB, unionAB;
   double jaccard_index;
   printf("Enter no. of elements of set A:");
   scanf("%d",&lenA);
   printf("Enter elements of Set A:");
   for(i=0;i<lenA;i++)
   {
       scanf("%d ",&A[i]);
   }

   printf("Enter no. of elements of set B:");
   scanf("%d",&lenB);
   printf("Enter elements of Set B:");
   for(i=0;i<lenB;i++)
   {
       scanf("%d ",&B[i]);
   }

   intersectionAB=calculate_intersection(A,B,lenA,lenB);
   printf("printing intersection of A and B:");
   for(i=0;i<intersectionAB.len;i++)
   {
       printf("%d ",intersectionAB.elements[i]);
   }

   unionAB=calculate_union(A,B,lenA,lenB);
   printf("printing union of A and B:");
   for(i=0;i<unionAB.len;i++)
   {
       printf("%d ",unionAB.elements[i]);
   }

   jaccard_index=(double)intersectionAB.len/(double)unionAB.len;
   printf("Jaccard index of set A and B:%.2f",jaccard_index);
  
   return EXIT_SUCCESS;
}

----------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote