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

Requirements Write a program ( proj3.c ) that uses the Titanic data to answer us

ID: 654753 • Letter: R

Question

Requirements

Write a program ( proj3.c ) that uses the Titanic data to answer use s queries. Your program should first load the data from the Titanic.txt file and store them in a well-organized way. Your program then prompts the user to choose whether he/she wants to submit a query or quit the program. If the user types the number 0, then the program should display the closing message and quit. If the user types the number 1, then the program should further prompt the user to type in the query conditions, i.e., the cabin class, age, and gender properties that specify a certain type of passenger. The program then goes through the entire dataset and finds out the number of passengers that match the user s query conditions, and calculate the chance of survival for this type of passenger.

Besides the listed values for a property of a passenger, the user can also type any as the input for one or more property(ies). In this case, the property whose query value is specified as any is ignored when we look for passengers that match the query conditions. For example, if the user types any for the cabin class, and child for the age, and female for the gender, then the program should find out the number of passengers whose age is child and gender is female , regardless of which cabin class they were in. One more example, if the user types any for both the cabin class and the age, but female for the gender, the class and the age properties are ignored and only the gender is checked for finding the passengers who match the query. In other words, here we just want to find out how many female passengers were on the ship, and how likely a female passenger would survive the diaster. See the sample outputs at the end of this document for more examples of different types of queries.

Sample Code to start off

/******************************************************
* Programming Project #3 Sample Code:
*    Open the file "Titanic.txt"
*    Load all the lines from Titanic.txt
*    Store all the lines in an array of strings
*    Print the first 5 lines from the array
*******************************************************/

#include <stdio.h>
#include <string.h>

/* Macro definitions */
#define NUM_OF_LINES 2201
#define MAX_LINE_LEN 99

/* Function declarations */
char* readline(FILE *fp, char line[]);

int main(void) {
    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);
  
    // Print out the first 5 lines read from the file
    printf("The first 5 lines are: ");
    int i;
    for (i = 0; i < 5; i++) {
        printf("%s ", lines[i]);
    }
  
    return 0;
}

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;
    }
}

Sample Outputs <arctic:~/test >a.out

2201 lines read from file Titanic.txt.

Please choose an option (1 for query, 0 for quit): 1

You chose to query the Titanic data.

* Which cabin class (first, second, third, crew): first

* What age (adult, child): adult

* What gender (male, female): male

Query result:

* 175 passengers match your query, 57 survived.

* Chance of survival is 32.6%

Please choose an option (1 for query, 0 for quit): 1

You chose to query the Titanic data.

* Which cabin class (first, second, third, crew): crew

* What age (adult, child): adult

* What gender (male, female): female

Query result:

* 23 passengers match your query, 20 survived.

* Chance of survival is 87.0%

Please choose an option (1 for query, 0 for quit): 1

You chose to query the Titanic data.

* Which cabin class (first, second, third, crew): crew

* What age (adult, child): child

* What gender (male, female): male Invalid query.

Please choose an option (1 for query, 0 for quit): 1

You chose to query the Titanic data.

* Which cabin class (first, second, third, crew): first

* What age (adult, child): child

* What gender (male, female): any

Query result:

* 6 passengers match your query, 6 survived.

* Chance of survival is 100.0%

Please choose an option (1 for query, 0 for quit): 1

You chose to query the Titanic data.

* Which cabin class (first, second, third, crew): any

* What age (adult, child): any

* What gender (male, female): female

Query result:

* 470 passengers match your query, 344 survived.

* Chance of survival is 73.2%

Please choose an option (1 for query, 0 for quit): 0

You chose to quit. Bye!

Explanation / Answer

/******************************************************
* Programming Project #3 Sample Code:
* Open the file "Titanic.txt"
* Load all the lines from Titanic.txt
* Store all the lines in an array of strings
* Print the first 5 lines from the array
*******************************************************/
#include <ATLComTime.h>
#include <stdio.h>
#include <string.h>
#define NUM_OF_LINES 2201
#define MAX_LINE_LEN 99
#import "C:Program FilesCommon FilesSystemADOmsado15.dll" rename("EOF", "EndOfFile")

char* readline(FILE *fp, char line[]);

int main(void)
{
FILE *fp;
char fileName[] = "Titanic.txt";
fp = fopen(fileName, "r");

std::string connectStr = "Provider=sqloledb;Data Source=MyDSN;Database=MyDB;User Id=DBUser;Password=Dbpwd;";

try
{
ADODB::_ConnectionPtr pConnection;
TESTHR(pConnection.CreateInstance(__uuidof(ADODB::Connection)));
ADODB::_ConnectionPtr pConnection->Open (connectStr.c_str(),"","", NULL);


VARIANT * RecordsAffected NULL;
long Options = ADODB::adExecuteNoRecords;

ADODB::_RecordsetPtr pRec = pConnection->Execute("select * from my_table",RecordsAffected,Options);

}
catch(_com_error &e)
{
// print error message
}
  

if (fp == NULL)
{

printf("Error opening file %s. ", fileName);
return -1;
}
  

char lines[NUM_OF_LINES][MAX_LINE_LEN];

  
int n = 0;
while(1)
{
char line[MAX_LINE_LEN];
if (readline(fp, line) != NULL)
{
strcpy(lines[n], line);
n++;
}
else
{
break;
}
}
  
fclose(fp);
  
printf("%d lines read from file %s. ", n, fileName);
  
printf("The first 5 lines are: ");
int i;
for (i = 0; i < 5; i++) {
printf("%s ", lines[i]);
}
  
return 0;
}

char* readline(FILE *fp, char line[])
{


if (fgets(line, MAX_LINE_LEN, fp) != NULL)
{
int len = strlen(line);
  
while (line[len - 1] == ' ' || line[len - 1] == ' ')
{
line[len - 1] = '';
len = strlen(line);
}
  
return line;
}

else
{
  
return NULL;
}
}

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