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

Explain line by line what is going on in this code? function purpose? #include <

ID: 3771970 • Letter: E

Question

Explain line by line what is going on in this code? function purpose?
#include <stdio.h>
#include <string.h>
typedef struct {
char class[10];
char age[10];
char gender[10];
char survived[10];
} Passenger;
/* Macro definitions */
#define NUM_OF_LINES 2201
#define MAX_LINE_LEN 99

/* Function declarations */
char* readline(FILE *fp, char line[]);
void linetostruct(char line[]);
void printInfo(Passenger p);
int findchar(char str[], int size, int start, char ch) ;
void substrcpy(char str1[], int start, int end, char str2[]);
Passenger user;
Passenger p;

int main(void) {

   int q;
   float percentage;
   /*Ask the user for their query or if they want to quit*/
   start:printf("Please choose an option (1 for query,0 for quit):");
   scanf("%d", &q);
   if (q == 1){
       printf("You chose to query the Titanic data. ");
       printf("Which cabin class? (first,second,third,or crew):");
       scanf("%s", &user.class);
       printf("What age? (adult or child):");
       scanf("%s", &user.age);
       printf("What gender? (male or female):");
       scanf("%s", &user.gender);
      
   }
   if (q == 0){
       printf("You chose to quit.Bye! ");
       return 0;
   }
   if (q != 0 && q != 1){
       printf("You didn't choose the right option ");
       goto start;
   }
      
FILE *fp; // A file pointer
char fileName[] = "Titanic.txt";
fp = fopen(fileName, "r"); // Open the file for reading
if (fp == NULL) {
// Fail to open the file, quit the program
printf("Error opening file %s. ", fileName);
return -1;
}
  
// Use a 2-D array to store all lines,
// each line is stored in one row of the array
char lines[NUM_OF_LINES][MAX_LINE_LEN];

// Read and store each line from file,
// stop if fail to read a line or reach the end of file
int n = 0;
while(1) {
char line[MAX_LINE_LEN];
if (readline(fp, line) != NULL) {
strcpy(lines[n], line);
n++;
}
else {
break;
}
}
  
fclose(fp); // Close the file after reading
  
printf("%d lines read from file %s. ", n, fileName);
  
// Find out how many passenger matched with the query given by the user and how many of those survived.
  
   int i, match = 0,survive=0;
   char yes[5] = "yes";

for (i = 0; i < NUM_OF_LINES; i++) {


linetostruct(lines[i]);
   if (cmpstruct(p, user) == 0){
       match++;
       if (strcmp(p.survived, yes) == 0){
           survive++;
       }
   }


   }
   char percent[5] = "%";
   if (match== 0){
       printf("Invalid query ");
       goto start;
      
   }
   else{
       printf("Query result : ");
       printf("*%d passengers match your query,%d survived ", match, survive);
       percentage = (float)survive / match * 100;
       printf("*Chance of survival is %.1f%s ", percentage, percent);


       goto start;
   }
}

char* readline(FILE *fp, char line[]) {
/* This function reads one line from an opened file stream */
/* It uses the fgets (from stdio.h) function */
/* fp: a pointer to the opened file stream */
/* line: a char array to store the read line */
/* Return the same char pointer pointing to line if reading succeeds */
/* Return NULL pointer if reading fails */

// Read one line using fgets
if (fgets(line, MAX_LINE_LEN, fp) != NULL) {
int len = strlen(line);
// Remove all the ' ' and ' ' characters at the end of line
while (line[len - 1] == ' ' || line[len - 1] == ' ') {
line[len - 1] = '';
len = strlen(line);
}
// Reading succeeded
return line;
}
else {
// Reading failed
return NULL;
}
}

void linetostruct(char str[]){

  
int i1, i2;
int len = strlen(str);
  
i1 = findchar(str, len, 0, '=');
i2 = findchar(str, len, i1, ',');
substrcpy(str, i1+1, i2-1, p.class);
  
i1 = findchar(str, len, i2, '=');
i2 = findchar(str, len, i1, ',');
substrcpy(str, i1+1, i2-1, p.age);
  
i1 = findchar(str, len, i2, '=');
i2 = findchar(str, len, i1, ',');
substrcpy(str, i1+1, i2-1, p.gender);
  
i1 = findchar(str, len, i2, '=');
substrcpy(str, i1+1, len-1, p.survived);

}


int findchar(char str[], int size, int start, char ch) {
int i;
for(i = start; i < size; i++) {
if(str[i] == ch) {
return i;
}
}
return -1;
}

void substrcpy(char str1[], int start, int end, char str2[]) {
int i, j;
for(i = start, j = 0; i <= end; i++, j++) {
str2[j] = str1[i];
}
str2[j] = '';
}

//This functions compare the given query with each and every passenger in the list and returns 0 if match or else returns 1
int cmpstruct(Passenger p1, Passenger p2){
   char any[5] = "any";
   if ((strcmp(p1.class, p2.class) == 0) || (strcmp(p2.class, any) == 0)){
       if ((strcmp(p1.age, p2.age) == 0) || (strcmp(p2.age, any) == 0)){
           if ((strcmp(p1.gender, p2.gender) == 0) || (strcmp(p2.gender, any) == 0)){
               return 0;

           }
           else return 1;
       }
       else return 1;
   }
   else return 1;

}

Explanation / Answer

Added commments for your understanding purpose....

#include <stdio.h>
#include <string.h>
//above tw line are directives..

//declaring structure..
typedef struct {
//char type class,age,gender,survived.
char class[10];
char age[10];
char gender[10];
char survived[10];
} Passenger;

//structure name Passenger.

/* Macro definitions */
#define NUM_OF_LINES 2201
#define MAX_LINE_LEN 99
/* Function declarations */
//below function reads lines from a file.
char* readline(FILE *fp, char line[]);

//linetostruct.
void linetostruct(char line[]);
//prints the info..
void printInfo(Passenger p);
//finds the character..
int findchar(char str[], int size, int start, char ch) ;
//substring copy function.
void substrcpy(char str1[], int start, int end, char str2[]);
Passenger user;
Passenger p;

//main function..
int main(void) {
int q;
float percentage;
/*Ask the user for their query or if they want to quit*/
start:printf("Please choose an option (1 for query,0 for quit):");
scanf("%d", &q);
if (q == 1){
printf("You chose to query the Titanic data. ");
printf("Which cabin class? (first,second,third,or crew):");
scanf("%s", &user.class);
printf("What age? (adult or child):");
scanf("%s", &user.age);
printf("What gender? (male or female):");
scanf("%s", &user.gender);
  
}
if (q == 0){
printf("You chose to quit.Bye! ");
return 0;
}
if (q != 0 && q != 1){
printf("You didn't choose the right option ");
goto start;
}
  
FILE *fp; // A file pointer
char fileName[] = "Titanic.txt";
fp = fopen(fileName, "r"); // Open the file for reading
if (fp == NULL) {
// Fail to open the file, quit the program
printf("Error opening file %s. ", fileName);
return -1;
}
  
// Use a 2-D array to store all lines,
// each line is stored in one row of the array
char lines[NUM_OF_LINES][MAX_LINE_LEN];
// Read and store each line from file,
// stop if fail to read a line or reach the end of file
int n = 0;
while(1) {
char line[MAX_LINE_LEN];
if (readline(fp, line) != NULL) {
strcpy(lines[n], line);
n++;
}
else {
break;
}
}
  
fclose(fp); // Close the file after reading
  
printf("%d lines read from file %s. ", n, fileName);
  
// Find out how many passenger matched with the query given by the user and how many of those survived.
  
int i, match = 0,survive=0;
char yes[5] = "yes";
for (i = 0; i < NUM_OF_LINES; i++) {

linetostruct(lines[i]);
if (cmpstruct(p, user) == 0){
match++;
if (strcmp(p.survived, yes) == 0){
survive++;
}
}

}
char percent[5] = "%";
if (match== 0){
printf("Invalid query ");
goto start;
  
}
else{
printf("Query result : ");
printf("*%d passengers match your query,%d survived ", match, survive);
percentage = (float)survive / match * 100;
printf("*Chance of survival is %.1f%s ", percentage, percent);

goto start;
}
}
char* readline(FILE *fp, char line[]) {
/* This function reads one line from an opened file stream */
/* It uses the fgets (from stdio.h) function */
/* fp: a pointer to the opened file stream */
/* line: a char array to store the read line */
/* Return the same char pointer pointing to line if reading succeeds */
/* Return NULL pointer if reading fails */
// Read one line using fgets
if (fgets(line, MAX_LINE_LEN, fp) != NULL) {
int len = strlen(line);
// Remove all the ' ' and ' ' characters at the end of line
while (line[len - 1] == ' ' || line[len - 1] == ' ') {
line[len - 1] = '';
len = strlen(line);
}
// Reading succeeded
return line;
}
else {
// Reading failed
return NULL;
}
}
void linetostruct(char str[]){
  
int i1, i2;
int len = strlen(str);
  
i1 = findchar(str, len, 0, '=');
i2 = findchar(str, len, i1, ',');
substrcpy(str, i1+1, i2-1, p.class);
  
i1 = findchar(str, len, i2, '=');
i2 = findchar(str, len, i1, ',');
substrcpy(str, i1+1, i2-1, p.age);
  
i1 = findchar(str, len, i2, '=');
i2 = findchar(str, len, i1, ',');
substrcpy(str, i1+1, i2-1, p.gender);
  
i1 = findchar(str, len, i2, '=');
substrcpy(str, i1+1, len-1, p.survived);

}

int findchar(char str[], int size, int start, char ch) {
int i;
for(i = start; i < size; i++) {
if(str[i] == ch) {
return i;
}
}
return -1;
}
void substrcpy(char str1[], int start, int end, char str2[]) {
int i, j;
for(i = start, j = 0; i <= end; i++, j++) {
str2[j] = str1[i];
}
str2[j] = '';
}
//This functions compare the given query with each and every passenger in the list and returns 0 if match or else returns 1
int cmpstruct(Passenger p1, Passenger p2){
char any[5] = "any";
if ((strcmp(p1.class, p2.class) == 0) || (strcmp(p2.class, any) == 0)){
if ((strcmp(p1.age, p2.age) == 0) || (strcmp(p2.age, any) == 0)){
if ((strcmp(p1.gender, p2.gender) == 0) || (strcmp(p2.gender, any) == 0)){
return 0;
}
else return 1;
}
else return 1;
}
else return 1;
}

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