Code:
.
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
typedef struct {
int age;
double off_time;
char gender;
} runner_t;
typedef struct {
int N;
double sumt;
double sumt2;
double mean;
double SD;
} age_record_t;
/*typedef struct {
int age[60];
} male_t;*/
/* Declaring Prototypes.*/
int not_blank (char * line);
runner_t creat_record(char line[], int icount);
int main()
{
char line[200];
int icount = 0;
int age = 0;
runner_t person;
age_record_t females[100];
age_record_t males[100];
FILE *infile;
FILE *outfile;
// connecting the file pointers to the actual files.
infile = fopen("marathon_txt.txt","r");
outfile = fopen("copy.txt","w");
/* This sets all the parts of the struct in the array to zero.*/
for ( age=0; age < 100; age++)
{
males[age].N = 0;
males[age].mean = 0.0;
males[age].SD = 0.0;
males[age].sumt = 0.0;
males[age].sumt2 = 0.0;
females[age].N = 0;
females[age].mean = 0.0;
females[age].SD = 0.0;
females[age].sumt = 0.0;
females[age].sumt2 = 0.0;
}
/* Read the entire data file
fgets reads a line up to and including \n, returns NULL if error
or it encountered the end-of-file. */
fgets(line,199,infile);
fgets(line,199,infile);
while (fgets(line,199,infile) != NULL)
{
if (not_blank(line) == 1)
{
icount++;
person = creat_record(line,icount);
if (person.gender =='F')
{
females[person.age].sumt+= person.off_time;
females[person.age].sumt2+= person.off_time * person.off_time;
females[person.age].N++;
}
if (person.gender == 'M')
{
males[person.age].sumt+= person.off_time;
males[person.age].sumt2+= person.off_time * person.off_time;
males[person.age].N++;
}
}
}
printf("\nThe program read %d lines \n",icount);
printf ("\nThere are %d runners \n", icount);
/* This part of the program calculates the mean and standard deviation using the extracted */
/* times.*/
for ( age=16; age < 100; age++)
{
if ((males[age]).N >= 2)
{
males[age].mean = (males[age].sumt)/(males[age].N);
males[age].SD = sqrt((males[age].sumt2 - (males[age].mean * males[age].mean))/ males[age].N);
}
else if (females[age].N >= 2)
{
females[age].mean = (females[age].sumt)/(females[age].N);
females[age].SD = (sqrt((females[age].sumt2 - (females[age].mean * females[age].mean)) / females[age].N))/females[age].N - 1;
}
}
/* The last part of this main fuction, prints out the results( age, gender, mean and standard
deviation for any sample with more than two runners.*/
for ( age=16; age < 100; age++)
{
if (males[age].N >= 2)
{
printf("%d\n ", person.age);
printf("%c\n ", person.gender);
printf("%lf\n ", males[age].mean);
printf("%lf\n\n", males[age].SD);
fprintf(outfile," %d\n",person.age);
fprintf(outfile," %c\n",person.gender);
fprintf(outfile," %lf\n",males[age].mean);
fprintf(outfile," %lf\n\n",males[age].SD);
}
}
for ( age=16; age < 100; age++)
{
if (females[age].N >= 2)
{
printf("%d\n ", person.age);
printf("%c\n ", person.gender);
printf("%lf\n ", females[age].mean);
printf("%lf\n\n", females[age].SD);
fprintf(outfile," %d\n",person.age);
fprintf(outfile," %c\n",person.gender);
fprintf(outfile," %lf\n",females[age].mean);
fprintf(outfile," %lf\n\n",females[age].SD);
}
}
fclose(infile);
fclose(outfile);
return 0;
}
//Not_Blank function takes out the spaces as the file is being read.
int not_blank(char * line)
{
while (*line != '\0')
{
if (*line !=' ' && *line != '\t' && *line != '\n') return 1;
++line;
}
return 0;
}
/* The creat_record fuction extracts the gender, time and age of the runners and places them */
/* in their respective structs.*/
runner_t creat_record(char line[], int icount)
{
runner_t person;
double hours = 0.0;
double mins = 0.0;
double secs = 0.0;
char *paren_ptr = '\0';
char paren ='(';
int fcount = 0;
int mcount = 0;
if(line[6] == 'F')
{
person.gender = 'F';
fcount ++;
}
else
{
person.gender = 'M';
mcount ++;
}
/* This part of the fuction puts the age into the runner_t struct.*/
if ( paren_ptr != NULL )
{
sscanf ( paren_ptr+1, "%d", &person.age);
}
else
{
fprintf( stderr, "Panic!!\n" );
exit( 1 );
}
/*This part of the fuction puts the calls a fuction that converts the time and puts into the
struct as minutes*/
sscanf (line + 60,"%lf:%lf:%lf", &hours, &mins, &secs);
person.off_time = (60.0 * hours + secs/60.0 + mins);
return person;
}
.