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

Q1. Given a data file \"candidatesdata.txt\", write a C program that collects th

ID: 3605125 • Letter: Q

Question

Q1. Given a data file "candidatesdata.txt", write a C program that collects the information listed below: (100 points) The "candidatesdata.txt"file have the following format: Name,Gender. Height, Weight Tanner, M,71.8,180.25 (1) Create a struct for each data record (2) Output the numbers and percentages of female and male candidates 3) Output the average height for male candidates; 4) Output the average height for female candidates; (5) Output the overall average height 6) Output the average weight for male candidates; (7 Output the average weight for female candidates (8) Output the overall average weight (9) List the candidates (Name and Gender) who have the least weight and the most weight; (10) List the candidates (Name and Gender) who is the shortest and the tallest.

Explanation / Answer


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LINE 250
#define MAX_NAME 100
typedef struct STRUCT_CANDIDATE
{
char szName[MAX_NAME];
char chGender;
double dHeight;
double dWeight;
}Candidate;
int main()
{
int i;
char c;
FILE *fp;
int iIndex;
int iLineCount = 0; // Line counter (result)
char szLine[MAX_LINE];
Candidate *pCandidate;
int iNoOfMale = 0;
int iNoOfFemale = 0;
double dTotalHeight = 0;
double dMaleTotalHeight = 0;
double dFemaleTotalHeight = 0;
double dTotalWeight = 0;
double dMaleTotalWeight = 0;
double dFemaleTotalWeight = 0;
double dMinWeight = 0;
double dMaxWeight = 0;
int iMinWeightIndex = 0;
int iMaxWeightIndex = 0;
double dMinHeight = 0;
double dMaxHeight = 0;
int iMinHeightIndex = 0;
int iMaxHeightIndex = 0;
//open CandidateData.txt file.
fp = fopen("CandidateData.txt", "r");
if (NULL == fp)
{
printf("file not found");
return -1;
}
// Count no of records in file.
// Extract characters from file and store in character c
for (c = getc(fp); c != EOF; c = getc(fp))
{
if (c == ' ') // Increment count if this character is newline
{
iLineCount = iLineCount + 1;
}
}
//allocate memory for all records
printf(" No of records: %d ", iLineCount);
pCandidate = (Candidate *)malloc(sizeof (Candidate)*iLineCount);
if (NULL == pCandidate)
{
printf("Insufficent memory");
return -1;
}
iIndex = 0;
fseek(fp, 0, SEEK_SET);
while (fgets(szLine, MAX_LINE, fp) && iIndex < iLineCount)
{
//printf("No: %d ", iIndex);
char* token1 = strtok(szLine, ",");
strcpy(pCandidate[iIndex].szName, token1);
//printf("Name: %s ", token1);
char* token2 = strtok(NULL, ",");
pCandidate[iIndex].chGender = *token2;
//printf("Gender: %s ", token2);
char* token3 = strtok(NULL, ",");
pCandidate[iIndex].dHeight = atof(token3);
//printf("Height: %s ", token3);
char* token4 = strtok(NULL, ",");
pCandidate[iIndex].dWeight = atof(token4);
//printf("Weight: %s ", token4);
iIndex++;
}
fclose(fp);
//count male and female canidiates.
for (i = 0; i < iLineCount; i++)
{
if (pCandidate[i].chGender == 'M' || pCandidate[i].chGender == 'm')
{
iNoOfMale++;
}
if (pCandidate[i].chGender == 'F' || pCandidate[i].chGender == 'f')
{
iNoOfFemale++;
}
}
printf("No of Male %d No of Female %d ", iNoOfMale, iNoOfFemale);
//count and display male and female canidiates percentage.
printf("Male percentage %f, female percentage %f ", ((float)iNoOfMale / iLineCount) * 100, ((float)iNoOfFemale / iLineCount) * 100);
//calculate average heght, avg male height, avg female height.
//count male and female canidiates and total height,weight.
for (i = 0; i < iLineCount; i++)
{
if (pCandidate[i].chGender == 'M' || pCandidate[i].chGender == 'm')
{
dMaleTotalHeight += pCandidate[i].dHeight;
dMaleTotalWeight += pCandidate[i].dWeight;
}
if (pCandidate[i].chGender == 'F' || pCandidate[i].chGender == 'f')
{
dFemaleTotalHeight += pCandidate[i].dHeight;
dFemaleTotalWeight += pCandidate[i].dWeight;
}
dTotalHeight += pCandidate[i].dHeight;
dTotalWeight += pCandidate[i].dWeight;
}
printf("Average height of male candidate %f ", dMaleTotalHeight/iNoOfMale);
printf("Average height of female candidate %f ", dFemaleTotalHeight / iNoOfFemale);
printf("Average height of overall candidate %f ", dTotalHeight / iLineCount);
printf("Average weight of male candidate %f ", dMaleTotalWeight / iNoOfMale);
printf("Average weight of female candidate %f ", dFemaleTotalWeight / iNoOfFemale);
printf("Average weight of overall candidate %f ", dTotalWeight / iLineCount);
//find min and max height and width record.
for (i = 0; i < iLineCount; i++)
{
if (0 == dMinHeight)
{
dMinHeight = pCandidate[i].dHeight;
iMinHeightIndex = i;
}
else
{
if (dMinHeight > pCandidate[i].dHeight)
{
dMinHeight = pCandidate[i].dHeight;
iMinHeightIndex = i;
}
}
if (0 == dMaxHeight)
{
dMaxHeight = pCandidate[i].dHeight;
iMaxHeightIndex = i;
}
else
{
if (dMaxHeight < pCandidate[i].dHeight)
{
dMaxHeight = pCandidate[i].dHeight;
iMaxHeightIndex = i;
}
}
if (0 == dMinWeight)
{
dMinWeight = pCandidate[i].dWeight;
iMinWeightIndex = i;
}
else
{
if (dMinWeight > pCandidate[i].dWeight)
{
dMinWeight = pCandidate[i].dWeight;
iMinWeightIndex = i;
}
}
if (0 == dMaxWeight)
{
dMaxWeight = pCandidate[i].dWeight;
iMaxWeightIndex = i;
}
else
{
if (dMaxWeight < pCandidate[i].dWeight)
{
dMaxWeight = pCandidate[i].dWeight;
iMaxWeightIndex = i;
}
}
}
printf("Candidate with least weight Name: %s Gender: %c ", pCandidate[iMinWeightIndex].szName, pCandidate[iMinWeightIndex].chGender);
printf("Candidate with most weight Name: %s Gender: %c ", pCandidate[iMaxWeightIndex].szName, pCandidate[iMaxWeightIndex].chGender);
printf("Candidate with least height Name: %s Gender: %c ", pCandidate[iMinHeightIndex].szName, pCandidate[iMinHeightIndex].chGender);
printf("Candidate with most height Name: %s Gender: %c ", pCandidate[iMaxHeightIndex].szName, pCandidate[iMaxHeightIndex].chGender);
free(pCandidate);
return 0;
}
/*
OutPut:
No of records: 9
No of Male 2 No of Female 7
Male percentage 22.222223, female percentage 77.777779
Average height of male candidate 41.800000
Average height of female candidate 163.371429
Average height of overall candidate 136.355556
Average weight of male candidate 180.250000
Average weight of female candidate 391.392857
Average weight of overall candidate 344.472222
Candidate with least weight Name: Tanner10 Gender: F
Candidate with most weight Name: Tanner9 Gender: F
Candidate with least height Name: Tanner1 Gender: M
Candidate with most height Name: Tanner2 Gender: F
Press any key to continue . . .
*/

/*CandidateData.txt*/
/*
Tanner1,M,11.8,180.25
Tanner2,F,712.8,180.25
Tanner3,F,71.8,180.25
Tanner4,M,71.8,180.25
Tanner6,F,71.8,180.25
Tanner7,F,71.8,180.25
Tanner8,F,71.8,180.25
Tanner9,F,71.8,1820.25
Tanner10,F,71.8,18.25
*/