Thread: Wrong output.

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    16

    Wrong output.

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

    There are no errors with this program, however the output is not quite right. Instead of printing out the age, gender, mean, and standard deviation for all the ages , it prints out only for age 48 . It does this for males only.

    What am I doing wrong? How can I correct it?
    Thanks
    Last edited by doampofo; 03-12-2003 at 09:53 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>infile = fopen("marathon_txt.txt", "r");
    >>outfile = fopen("copy.txt", "w");
    You didn't check either of these two worked before using them. This probably isn't your real problem, but with a sample of the input I can't run your program. Can you post an example of the input?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    It is not recommended to use exit(1) in function creat_record(). Better would be to return to main(). Function main() should then close the open files and perhaps do some other things related to error-handling.

    Also paren_ptr is used to store data, but no memory is allocated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrong Output!
    By kolliash in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2008, 07:55 AM
  2. Something Wrong with my function in Linux!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 04-30-2008, 10:00 PM
  3. Getting wrong output from a class
    By orikon in forum C++ Programming
    Replies: 11
    Last Post: 11-18-2005, 07:58 PM
  4. Why is the output of this wrong?
    By Tokimasa in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2004, 01:58 PM
  5. Leap year program prints wrong output
    By Guti14 in forum C Programming
    Replies: 8
    Last Post: 08-24-2004, 11:56 AM