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

Please help me to write this C Code . Here is the prompt. A group of sociology s

ID: 3891480 • Letter: P

Question

Please help me to write this C Code. Here is the prompt.

A group of sociology students have had the idea of creating a dating application for UC Davis, and even found a name: UCupiD. However, none of them know how to program and they need help implementing their idea.

A file database has an entry for each individual who has subscribed to the service. Each entry contains the individual’s SID number (9 digits), first name (< 20 characters), sex (M or F), sexual orientation (M or F), age, political leaning (from 0 for very conservative, up to 10 for very liberal), major hobby (< 20 characters), minor hobby (< 20 characters), height (in inches) and weight (in pounds). Entries are ordered by SID numbers.

Two individuals match if their respective sex and sexual orientation are compatible (e.g. a female looking for males would be compatible with a male looking for females, or a male looking for males would be compatible another male looking for males, etc.) and if one of the following matching criteria is triggered:

Social identity: the two individuals are within 10 years of each other and have a political leaning within 2 points of each other.

Personality: the two individuals share both their major and minor hobbies.

Appearance: the two individuals are within 10% of each other’s height and weight.

Write program UCupidD.c which receives as program arguments the name of the database file and an individual’s SID, and generates an output file containing the list of possible matches for this individual, ordered by their SID, as illustrated in the following example.

Here are a list of requirements, assumptions and hints:

The program should receive two command-line arguments, the name of the database file and an individual’s SID. If less than two arguments are provided, the program should display an error message and return with error code 1.

Other errors include: database file cannot be opened, individual cannot be found in database, output file cannot be created and opened.

The name of the output file must be generated based on the individual’s SID for which the program is trying to find matches: match_.txt.

The database file is formatted in csv, which means that each individual’s entry is described on one line, and the information within each line are separated by commas. The format for an entry is:

For reading the file, it is recommended to use fgets() to read an entire line (we assume that the maximum amount of characters per line is 256) and sscanf() to parse the entry and retrieve each of the individual’s information.

Because commas are not considered as a word delimiter by sscanf() when using %s, we need to use string specifier %[^,]. This specifier will match all the characters until encountering a comma. Here an example code:

When reading the database file, you have to create a linked list to represent individuals in your program.

IMPORTANT: Not using a linked list will result in getting a straight zero for the Implementation score on Gradescope.

Here is the suggested order of implementation for this program:

1. Define a structure that represents each individual.

2. Write a function that reads each entry from the database file, and creates a structure object that is inserted in a linked list of individuals.

3. Write a function that finds an individual in the linked list based on their SID.

4. Write a function that finds all the matches between an individual and all of the individuals represented in the linked list, and outputs the matches in a file.

5. Write a function that destroy the linked list.

6. Write the main() function which receives the command-line arguments and then calls all the functions above in the appropriate order.

List of some important libc functions that are used in the reference program: fopen(), fclose(), fgets(), sscanf(), malloc(), free(), strcmp(), sprintf(), abs().

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/**Define a structure that represents each individual.**/
struct list
{
char SID[11];
char Firstname[20];
char Sex[2];
char Orientation[2];
int Age;
int PoliticalLeaning;
char Major[20];
char Minor[20];
int Height;
int Weight;
struct list *next;
};
struct list *head;

/**Write a function that reads each entry from the database file, and creates a structure object that is inserted in a linked list of individuals.**/
int readAndFeed(char filename[])
{
FILE *fp;
head=NULL;
int count=0;
char str[256];
char *token;
struct list *new1;
char temp[30];

fp=fopen(filename,"r");

if(fp!=NULL)
{
  
  while(fgets(str,256,fp)!=NULL)
  {
   new1=(struct list *)malloc(sizeof(struct list));
   count++;
   
   token = strtok(str, ",");
//   printf("token=%s ",token);
   strcpy(new1->SID,token);
   
   token = strtok(NULL, ",");
//   printf("token=%s ",token);
   strcpy(new1->Firstname,token);
   
   token = strtok(NULL, ",");
//   printf("token=%s ",token);
   strcpy(new1->Sex,token);
   
   token = strtok(NULL, ",");
//   printf("token=%s ",token);
   strcpy(new1->Orientation,token);
   
   token = strtok(NULL, ",");
//   printf("token=%s ",token);
   new1->Age=atoi(token);
   
   token = strtok(NULL, ",");
//   printf("token=%s ",token);
   new1->PoliticalLeaning=atoi(token);
   
   token = strtok(NULL, ",");
//   printf("token=%s ",token);
   strcpy(new1->Major,token);
   
   token = strtok(NULL, ",");
//   printf("token=%s ",token);
   strcpy(new1->Minor,token);
   
   token = strtok(NULL, ",");
//   printf("token=%s ",token);
   new1->Height=atoi(token);
   
   token = strtok(NULL, ",");
//   printf("token=%s ",token);
   new1->Weight=atoi(token);
   
   new1->next=head;
   head=new1;
   
   
  }
}
else
{
  fclose(fp);
  return -1;
}
fclose(fp);
return count;
}

/**Write a function that finds an individual in the linked list based on their SID.**/
struct list *findOne(char id[])
{
struct list *trav=head;

struct list *individual;
if(trav==NULL)
{
  printf(" No data in local");
}

while(NULL!=trav)
{
//  printf("SID=%s",trav->SID);
  if((strcmp(id,trav->SID))==0)
  {
   strcpy(individual->SID,trav->SID);
//   printf(" sid ");
   strcpy(individual->Firstname,trav->Firstname);
//   printf(" fname ");
   strcpy(individual->Sex,trav->Sex);
//   printf(" sex ");
   strcpy(individual->Orientation,trav->Orientation);
//   printf(" orientation ");
   individual->Age=trav->Age;
//   printf(" age ");
   individual->PoliticalLeaning=trav->PoliticalLeaning;
//   printf(" polytical ");
   strcpy(individual->Major,trav->Major);
//   printf(" major ");
   strcpy(individual->Minor,trav->Minor);
//   printf(" minor ");
   individual->Height=trav->Height;
//   printf(" height ");
   individual->Weight=trav->Weight;
//   printf(" weigth ");
   individual->next=NULL;
   return individual;
  }
  trav=trav->next;
}

return NULL;
}
/**Write a function that finds all the matches between an individual and all of the individuals represented in the linked list, and outputs the matches in a file.**/
int findAndFeed(struct list *individual)
{
int count=0;
char filename[256];
char putData[256];
FILE *fp;
struct list *trav=head;
int flag=0;

strcat(filename,"./match_");
strcat(filename,individual->SID);
strcat(filename,".txt");


while(trav!=NULL)
{
  flag=0;
  if((strcmp(trav->Sex,individual->Orientation)==0)&&(strcmp(trav->Orientation,individual->Sex)==0))
  {
   if((trav->Age-individual->Age)<=10&&(trav->Age-individual->Age)>=-10)
   {
    flag=1;
   }
   if(flag==0)
   {
    if((strcmp(strlwr(trav->Major),strlwr(individual->Major))==0)&&(strcmp(strlwr(trav->Minor),strlwr(individual->Minor))==0))
    {
     
     flag=1;
    }
   }
   if(flag==0)
   {
    //for Appearance
   }
  }
  fp=fopen(filename,"w");
  if(flag==1)
  {
   count++;
   if(count==1)
   {
    strcpy(putData,"Matches for user ");
    strcat(putData,individual->SID);
    strcat(putData," (");
    strcat(putData,individual->Firstname);
    strcat(putData,") ");
    fputs(putData,fp);
   }
   strcat(putData,"- ");
   strcat(putData,trav->SID);
   strcat(putData," (");
   strcat(putData,trav->Firstname);
   strcat(putData,") ");
   fputs(putData,fp);
  }
  trav=trav->next;
}
fclose(fp);
return count;
}
/**Write a function that destroy the linked list.**/
void destroy()//struct list *head)
{
struct list *trav=head;
struct list *toDestroy;
printf(" *********************************");
printf(" * Destroying Local Data....!!! *");
printf(" *********************************");
while(trav!=NULL)
{
  toDestroy=trav;
  trav=trav->next;
  free(toDestroy);
}
head=NULL;
}
void print_list()//struct list * head)
{
struct list *new1=head;

printf("Print : ");
if(NULL==head)
{
  printf("No Value in head ");
  exit(1);
}
else{
  
  while(new1!=NULL)
  {
   printf("%s ",new1->SID);
      printf("%s ",new1->Firstname);
      printf("%s ",new1->Sex);
      printf("%s ",new1->Orientation);
      printf("%d ",new1->Age);
      printf("%d ",new1->PoliticalLeaning);
      printf("%s ",new1->Major);
      printf("%s ",new1->Minor);
      printf("%d ",new1->Height);
      printf("%d ",new1->Weight);
      printf(" ........................................... ");
   new1=new1->next;
  }
}
}
/**Write the main() function which receives the command-line arguments and then calls all the functions above in the appropriate order.**/
int main()
{
//struct list *head=NULL;
char filename[20],SID_[11];
struct list *individual=NULL;
strcpy(filename,"filename");
int status=readAndFeed(filename);
if(status<0)
{
  printf(" No File With name %s found.",filename);
  exit(1);
}
else
{
  if(status==0)
  {
   printf(" No data read from file ");
  }
  else
  {
   printf(" %d Data read and stored in local storage",status);
   printf(" Enter the SID:");
   scanf("%s",SID_);
   individual=findOne(SID_);
   if(individual!=NULL)
   {
    printf(" Coresponding Person found ");
    status=findAndFeed(individual);
    if(status==0)
    {
     printf(" No Match Found ");
    }
    else
    {
     printf("%d match stored in -match_%s",status,SID_);
    }
    
   }
   else
   {
    printf(" No individual found with SID=%s",SID_);
   }
  }
  destroy();
}
return 0;
}