Code:
.
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <conio.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;
age_record_t fandm;
fandm females[100];
fandm males[100];
runner_t person;
FILE *infile;
FILE *outfile;
// connecting the file pointers to the actual files.
infile = fopen("marathon_txt.txt","r");
outfile = fopen("copy.txt","w");
/* 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);
printf("%d%s\n",icount,line);
//getch();
fprintf(outfile,"%d%s\n",icount,line);
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("The program read %d lines \n",icount);
printf ("There are %d runners \n", icount);
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;
}
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.*/
paren_ptr = strchr (line, paren);
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);
//printf("%.2lf", person.off_time);
return person;
}
.
In this program , the creat record function, returns the age time and gender of every runner. The main fuction reads lines of text from a file, as the data is returned from the data-extractin function, the results are supposed to be added to an array of different structs , arrayed by age. This array shd be of type age-record tight?
Am I declaring the arrays right? I don't think I am since I get an error message:
missising ; before identifier females ...and males.
Also once I get these numbers, how do I compute the standard deviation of their times... for all the ages? ..Go through the array twice... making sure that there is valid data(N>=2)?
In the end I'll print the age, gender , mean and standard deviation to the screen.