Thread: finding the max/min/average

  1. #1
    Hopeless
    Join Date
    Jul 2005
    Location
    Bay Area, CA
    Posts
    25

    finding the max/min/average

    Hi,

    Need some help on this one. I am writing a program using structures and arrays for students grades. I am getting the input from a file and I am suppossed to print out the data, then print out the max score, min score, and average score of each quiz taken. Here is the code I have made so far.
    Code:
    #include <stdio.h>
    
    /* Global Declarations */
    
    typedef struct
    	{
    	char nameFirst[20];
    	char nameLast[20];
    	int id;
    	int quiz[4];
    	int exam;
    	} STUDENT;
    
    int main (void)
    {
    	
    	STUDENT stuAry[50];
    	STUDENT highest;
    	STUDENT lowest;
    	STUDENT average;
    	FILE *fpData;
    	int i = 0;
    	
    
    	if ((fpData = fopen("lab5.txt", "r")) == NULL) // <-- OPENS lab5.txt FILE
    	{
    	printf("\a*ERROR OPENING lab5.txt\n");
    	return (100);
    	} /* IF OPEN */
    	
    	printf("--------------------------------------------------------------------\n");
    	printf("\t\t\t\t DATA\n");
    	printf("--------------------------------------------------------------------\n");
    	printf("\nNAME\t\t\tID\tQUIZ1\tQUIZ2\tQUIZ3\tQUIZ4\tEXAM\n\n");
    
    	while ((fscanf (fpData, "%s%s%d%d%d%d%d%d", &stuAry[i].nameFirst, &stuAry[i].nameLast, &stuAry[i].id, &stuAry[i].quiz[0], &stuAry[i].quiz[1], &stuAry[i].quiz[2], &stuAry[i].quiz[3], &stuAry[i].exam))>0){
    
    			
    			printf("%-10s ", stuAry[i].nameFirst);
    			printf("%-10s", stuAry[i].nameLast);
    			printf("\t%d\t", stuAry[i].id);
    			printf("%d\t", stuAry[i].quiz[0]);
    			printf("%d\t", stuAry[i].quiz[1]);
    			printf("%d\t", stuAry[i].quiz[2]);
    			printf("%d\t", stuAry[i].quiz[3]);
    			printf("%d\n", stuAry[i].exam);
    
    			i++;
    	}
    		
    
    	printf("\n--------------------------------------------------------------------\n");
    	printf("\t\t\t\t STATISTICS\n");
    	printf("--------------------------------------------------------------------\n");
    
    				
    	
    	if (fclose(fpData == EOF) // <--CLOSES lab5.txt FILE
    	{
    	printf("\aERROR OPENING lab5.txt\n");
    	return (102);
    	}
    
    	return 0;
    }
    Any help to start me off in the right direction would be great. Thanks in advance.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Crank the warning/error level your compiler emits to the max.
    Fix the warnings/errors.
    Post specific warnings/errors that you don't understand and can't fix.
    [edit]Post a sample of the input file (we don't read minds).
    Last edited by Dave_Sinkula; 08-03-2005 at 09:51 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Hopeless
    Join Date
    Jul 2005
    Location
    Bay Area, CA
    Posts
    25
    whoa.. took me almost all night, but I think.. well, I guess I know that i got it since it works. Thanks for the help. Here is the last part of the code that I came up with.


    Code:
    printf("\n--------------------------------------------------------------------\n");
    	printf("\t\t\t\t STATISTICS\n");
    	printf("--------------------------------------------------------------------\n");
    	printf("Quiz\tLow\tHigh\tAvg\n");
    	
    	
    	for (j = 0; j < 4; j++)
    	{
    		printStat(stuAry, i, j);
    	}	 				
    	
    	if (fclose(fpData) == EOF) // <--CLOSES lab5.txt FILE
    	{
    	printf("\aERROR OPENING lab5.txt\n");
    	return (102);
    	}
    
    	return 0;
    }
    
    void printStat(STUDENT a[], int tot, int qnum)
    {
    	int low, high;
    	float ave = 0;
    
    	int k;
    
    	low = a[0].quiz[qnum];
    	high = low;
    	
    	for(k = 1; k < tot; k++)
    	{
    		if (low > a[k].quiz[qnum])
    			low = a[k].quiz[qnum];
    
    		if (high < a[k].quiz[qnum])
    			high = a[k].quiz[qnum];
    	
    		ave += a[k].quiz[qnum];
    	}
    
    	ave = ave/tot;
    		
    	printf("Quiz %d: %d\t%d\t%.2f\n", qnum, low, high,ave);	
    	return;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Finding primes
    By scwizzo in forum C++ Programming
    Replies: 11
    Last Post: 09-10-2008, 06:15 PM
  3. Finding primes
    By starripper in forum C++ Programming
    Replies: 19
    Last Post: 01-14-2006, 04:17 PM
  4. Finding URLS
    By johnchain in forum C Programming
    Replies: 5
    Last Post: 10-07-2005, 07:56 AM
  5. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 09:06 AM