Assume we have the following data shown below data is also a

Assume we have the following data shown below: (data is also available in candidatedata.txt).

Name,Gender,Height,Weight

Tanner,M,71.8,180.25

John,M,70.75,185.3

Parker,F,65.25,120.3

Meeks,M,57.25,210.2

Big,M,57.5,150.1

Jackson,F,52.1,163.4

Tillis,M,78.75,157.75

Boop,F,54.5,108.7

Kay,F,71.25,153.6

Monroe,F,68.5,145.4

Thomas,M,66.0,182.7

Ferris,M,73.6,153.1

Appleworth,F,65.75,154.9

Prince,F,57.1,121.3

Schultz,F,67.25,144.6

Brooks,M,68.75,168.5

Loris,F,66.75,136.0

Martin,M,72.0,172.8

Smithe,M,75.25,230.7

Brians,F,72.75,102.6

(There are no spaces between the lines, the prompt box just makes them appear that way.)

Write a program that: a) Create a struct for each data record. b) Create structure array, dynamically allocate memory and read in all records from the TXT file into the structure array. c) Output the numbers and percentages of female and male candidates. d) Output the average height for male candidates. e) Output the average height for female candidates. f) Output the overall average weight. g) List the candidates (Name and Gender) who have the least weight and the most weight. h) List the candidates (Name and Gender) who is the shortest and the tallest. i) Free the memory and exit the program.

Im struggling really hard with this question. Help would be greatly appreciated. I cant find a good example in my book on how to do this.

Solution

Here is the program

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

struct candidate
{
char name[30];
char gender[2];
float height;
float weight;
};

int findMin(float listofValues)
{
int size = listofValues.length;
float minimum = listofValues[0];
int location = 0;
for ( c = 1 ; c < size ; c++ )
{
if ( listofValues[c] < minimum )
{
minimum = listofValues[c];
location = c;
}
}
return location;
}

int findMax(float listofValues)
{
int size = listofValues.length;
float maximum = listofValues[0];
int location = 0;
for ( c = 1 ; c < size ; c++ )
{
if ( listofValues[c] > maximum )
{
maximum = listofValues[c];
location = c;
}
}
return location;
}

int main()
{
static const char filename[] = \"candidatedata.txt\";
FILE *file = fopen ( filename, \"r\" );
char *pt;
struct candidate record[2];
int counter = 0;
//reading from file
if(file != NULL)
{
char line[128];
while(fgets(line, sizeof line, file) != NULL)
{
pt = strtok(line, \",\");
strcpy(record[counter].name, pt);
pt = strtok(NULL,\",\");
strcpy(record[counter].gender, pt)
pt = strtok(NULL,\",\");
record[counter].height = atoi(pt);
pt = strtok(NULL,\",\");
record[counter].weight = atoi(pt);
counter++;
}
}
// Manipulating the data
int numberofCandidates = record.length();
int numOfFemaleCandidates = 0;
int numOfMaleCandidates = 0;
float totalheightOfMale = 0.0F;
float totalheightoffemale = 0.0F;
float overallWeight = 0.0F;
for(int i=0; i<numberofCandidates; i++)
{
if(record[i].gender == \"M\")
{
numOfMaleCandidates++;
totalheightOfMale = totalheightOfMale + record[i].height;
overallWeight = overallWeight + record[i].weight;
}
else
{
numOfFemaleCandidates++;
totalheightoffemale = totalheightoffemale + record[i].height;
overallWeight = overallWeight + record[i].weight;
}
}


printf(\"Percentage of Male Candidates: %d\ \", numOfMaleCandidates/numberofCandidates*100);
printf(\"Percentage of Female Candidates: %d\ \", numOfFemaleCandidates/numberofCandidates*100);
printf(\"Average height of Male Candidates: %f\ \", totalheightOfMale/numOfMaleCandidates);
printf(\"Average height of Female Candidates: %f\ \", totalheightoffemale/numOfFemaleCandidates);
printf(\"Overall average weight of Candidates: %f\ \", overallWeight/numberofCandidates);

// creating array for height and weight separately
float heights[numberofCandidates];
float weights[numberofCandidates];
for(int x=0 ;x<numberofCandidates; x++)
{
heights[x] = record[x].height;
weights[x] = record[x].weight;
}
int minWeight = findMin(weights);
int maxWeight = findMax(weights);
int minheight = findMax(heights);
int maxheight = findMax(heights);
printf(\"Candidate with least weight: %s %s\ \", record[minWeight].name, record[minWeight].gender);
printf(\"Candidate with most weight: %s %s\ \", record[maxWeight].name, record[maxWeight].gender);
printf(\"Candidate who is shortest: %s %s\ \", record[minheight].name, record[minheight].gender);
printf(\"Candidate who is tallest: %s %s\ \", record[maxheight].name, record[maxheight].gender);


return 0;
}

The output is :
Input Text File contains the following data
Tanner,M,71.8,180.25
John,M,70.75,185.3
Parker,F,65.25,120.3
Meeks,M,57.25,210.2
Big,M,57.5,150.1
Jackson,F,52.1,163.4
Tillis,M,78.75,157.75

Percentage of Male Candidates: 71
Percentage of Female Candidates: 29
Average height of Male Candidates: 67.21
Average height of Female Candidates: 58.675
Overall average weight of Candidates: 166.76
Candidate with least weight: Parker F
Candidate with most weight: Meeks M
Candidate who is shortest: Jackson F
Candidate who is tallest: Tillis M

Assume we have the following data shown below: (data is also available in candidatedata.txt). Name,Gender,Height,Weight Tanner,M,71.8,180.25 John,M,70.75,185.3
Assume we have the following data shown below: (data is also available in candidatedata.txt). Name,Gender,Height,Weight Tanner,M,71.8,180.25 John,M,70.75,185.3
Assume we have the following data shown below: (data is also available in candidatedata.txt). Name,Gender,Height,Weight Tanner,M,71.8,180.25 John,M,70.75,185.3
Assume we have the following data shown below: (data is also available in candidatedata.txt). Name,Gender,Height,Weight Tanner,M,71.8,180.25 John,M,70.75,185.3

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site