Need some guidance.

This is a discussion on Need some guidance. within the C Programming forums, part of the General Programming Boards category; I'm doing a grading report and I have trouble with my average function. This is what I have so far. ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    10

    Need some guidance.

    I'm doing a grading report and I have trouble with my average function. This is what I have so far.

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    
    
    typedef struct GRADEREPORT
    {
    	int id;
    	char name[30];
    	double homework1;
    	double homework2;
    	double homework3;
    	double exam;
    	double final;
    
    } Student;
    
    int data(Student s[]);
    double studentAverage(Student s[], int count);
    double classAverage(Student s[], int count);
    double homeworkAverage(Student s[], int count);
    void output_Data(Student s[], int count,double aveHomework, double aveStu, double aveClass);
    
    
    int main()
    {
    	int count;
    	double aveHomework, aveStudent, aveClass;
    	Student nam[30];
    
    	count = data(nam);
    	aveStudent = studentAverage(nam, count);
    	aveClass = classAverage(nam, count);
    	aveHomework = homeworkAverage(nam,count);
    
    	output_Data(nam, count,aveHomework, aveStudent, aveClass);
    
    	return 0;
    }
    
    int data(Student s[])
    {
    	int c, i = 0;
    	FILE *infile;
    
    	infile = fopen("p4.dat", "r");
    	if (infile == NULL)
    	{
    		printf("Problem opening file \"p4.dat\" \n");
    		exit(1);
    	}
    
    	c = fscanf(infile, "%d %s %lf %lf %lf %lf %lf", &s[i].id, &s[i].name, &s[i].homework1, 
    		&s[i].homework2,&s[i].homework3, &s[i].exam, &s[i].final);
    
    	while (c != EOF)
    	{
    		i++;
    		c = fscanf(infile, "%d %s %lf %lf %lf %lf %lf", &s[i].id, &s[i].name, &s[i].homework1, 
    		&s[i].homework2,&s[i].homework3, &s[i].exam, &s[i].final);
    	}
    
    	fclose(infile);
    	return i;
    }
    double homeworkAverage(Student s[], int count)
    {
    	double ave, sum = 0;
    	int i,j;
    	j= 0;
    
    	for(i=0; i < count; i++)
    	{
    		sum = sum + s[i].homework1 + s[i].homework2 + s[i].homework3;
    		count = i+ 1;
    		ave = (sum/100) * 100;
    	}
    
    	return ave;
    }
    
    double studentAverage(Student s[], int count)
    {
    	double ave, sum = 0;
    	int i;
    	
    	for (i=0; i < count; i++)
    	{
    		sum = sum + s[i].homework1 + s[i].homework2 + s[i].homework3 + s[i].exam + s[i].final;
    		count = i+ 1;
    		ave = (sum/300) * 100;
    	}
    	
    	return ave;	
    }
    
    double classAverage(Student s[], int count)
    {
    	double ave, sum = 0;
    	int i;
    
    	for (i= 0; i < count; i++)
    	{
    		sum = sum + s[i].homework1 + s[i].homework2 + s[i].homework3 + s[i].exam + s[i].final;
    		count = i+ 1;
    		ave = sum/count;
    	}	
    
    	return ave;
    }
    
    void output_Data(Student s[], int count, double aveHomework, double aveStu, double aveClass)
    {
    	int i = 0;
    	double average,homeworkAverage;
    
    	FILE *outfile;
    
    	outfile = fopen("StudentReport.txt", "w");
    	if (outfile == NULL)
    	{
    		printf("Problem opening file \"StudentReport.txt\" \n");
    		exit(1);
    	}	
    
    	fprintf(outfile,"                         CSCI 110 Grade Report\n\n");
    	fprintf(outfile, "ID:     Name                Homework  Exam      Final     Average Grade\n");
    	while(i < count)
    	{
    		average = aveStu;
    		homeworkAverage = aveHomework;
    
    		fprintf(outfile, "%d %-20s %6.1lf %9.1lf %9.1lf %9.1lf\n", s[i].id , s[i].name, homeworkAverage, s[i].exam , 
    			s[i].final, average);
    		i = i + 1;
    
    	}
    
    }
    At the moment it prints the first record of the file correctly if i'm not wrong, then it prints that same record repeatably for the remaining records. I was looking at my averageHomework function, the others have the same problem. And i'm not sure if i'm in the right path but i was thinking i need an array there, but then i have to return it and i'm not sure how to do that. So hopefully someone can give me some guidance on what approach I can use. Sorry for the messy code, I was trying different things so you might notice unused variables.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You're only printing the same numbers every time, given that you print the passed-in parameters for every student. You should presumably only print one student in your output routine, so that it can go in the loop with everything else. Either that, or your Student structure is incomplete -- if you want to store all the homework averages in one go, then your Students need to have a field for that average so you can store it with the student.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    There's an issue here:

    Quote Originally Posted by Kinto View Post
    Code:
    	for(i=0; i < count; i++)
    	{
    		sum = sum + s[i].homework1 + s[i].homework2 + s[i].homework3;
    		count = i+ 1;
    		ave = (sum/100) * 100;
    	}
    "count" in the for() loop gets evaluated at every pass, meaning this loop either never ends, or (more likely) can only happen once.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MK27 View Post
    There's an issue here:



    "count" in the for() loop gets evaluated at every pass, meaning this loop either never ends, or (more likely) can only happen once.
    Ooh, good catch. I was wondering why his averages looked right, since (at first glance) it looked like he was adding everybody's scores together. Indeed, the i++ at the end of the loop means the loop will end there.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    10
    Thanks for the help, this is my current code, took sometime to reply, doing homework and watching boxing may have clouded my thoughts. lol
    Code:
    # include <stdio.h>
    # include <stdlib.h>
    
    
    typedef struct GRADEREPORT
    {
    	int id;
    	char name[30];
    	double homework1;
    	double homework2;
    	double homework3;
    	double exam;
    	double final;
    	double aveH;
    	double aveS;
    
    } Student;
    
    int data(Student s[]);
    double studentAverage(Student s[], int count);
    double classAverage(Student s[], int count);
    double homeworkAverage(Student s[], int count);
    void output_Data(Student s[], int count,double aveHomework, double aveStu, double aveClass);
    
    
    int main()
    {
    	int count;
    	double aveHomework, aveStudent, aveClass;
    	Student nam[30];
    
    	count = data(nam);
    	aveStudent = studentAverage(nam, count);
    	aveClass = classAverage(nam, count);
    	aveHomework = homeworkAverage(nam,count);
    
    	output_Data(nam, count,aveHomework, aveStudent, aveClass);
    
    	return 0;
    }
    
    int data(Student s[])
    {
    	int c, i = 0;
    	FILE *infile;
    
    	infile = fopen("p4.dat", "r");
    	if (infile == NULL)
    	{
    		printf("Problem opening file \"p4.dat\" \n");
    		exit(1);
    	}
    
    	c = fscanf(infile, "%d %s %lf %lf %lf %lf %lf", &s[i].id, &s[i].name, &s[i].homework1, 
    		&s[i].homework2,&s[i].homework3, &s[i].exam, &s[i].final);
    
    	while (c != EOF)
    	{
    		i++;
    		c = fscanf(infile, "%d %s %lf %lf %lf %lf %lf", &s[i].id, &s[i].name, &s[i].homework1, 
    		&s[i].homework2,&s[i].homework3, &s[i].exam, &s[i].final);
    	}
    
    	fclose(infile);
    	return i;
    }
    double homeworkAverage(Student s[], int count)
    {
    	double ave, sum = 0;
    	int i,j;
    	j= 0;
    
    	for(i=0; i < count; i++)
    	{
    		sum = s[i].homework1 + s[i].homework2 + s[i].homework3;
    		ave = (sum/100) * 100;
    		s[i].aveH = ave;
    	}
    
    	return ave;
    
    }
    
    double studentAverage(Student s[], int count)
    {
    	double ave, sum = 0;
    	int i,j;
    	j=0;
    	
    	for (i=0; i < count; i++)
    	{
    		sum = s[i].homework1 + s[i].homework2 + s[i].homework3 + s[i].exam + s[i].final;
    		ave = (sum/300) * 100;
    		s[i].aveS = ave;
    	}
    	
    
    	return ave;	
    }
    
    double classAverage(Student s[], int count)
    {
    	double ave, sum = 0;
    	int i;
    
    	for (i= 0; i < count; i++)
    	{
    		sum = s[i].homework1 + s[i].homework2 + s[i].homework3 + s[i].exam + s[i].final;
    		ave = sum/count;	
    	}
    	
    
    	return ave;
    
    }
    
    void output_Data(Student s[], int count, double aveHomework, double aveStu, double aveClass)
    {
    	int i = 0;
    	double average,homeworkAverage;
    
    	FILE *outfile;
    
    	outfile = fopen("StudentReport.txt", "w");
    	if (outfile == NULL)
    	{
    		printf("Problem opening file \"StudentReport.txt\" \n");
    		exit(1);
    	}
    
    	
    
    	fprintf(outfile,"                         CSCI 110 Grade Report\n\n");
    	fprintf(outfile, "ID:     Name                Homework  Exam      Final     Average Grade\n");
    
    
    
    	while(i < count)
    	{
    
    		fprintf(outfile, "%d %-20s %6.1lf %9.1lf %9.1lf %9.1lf\n", s[i].id , s[i].name, s[i].aveH, s[i].exam , 
    			s[i].final, s[i].aveS);
    		i = i + 1;
    
    	}
    
    }
    Seems to be working now, i'll go back and verify the answers. thanks again for the help, and also since I use the struct to store the ave I dont need the functions to return anything right?
    Last edited by Kinto; 05-30-2009 at 10:13 PM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Kinto View Post
    also since I use the struct to store the ave I dont need the functions to return anything right?
    You can smatter printf() statements thru-out your program to verify the state of variables, including the variables inside structs. Just remember to remove them once you are sure of what is going on.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    10
    Might as well ask now, if I want to sort the struct by student average in descending order what can i do? I tried doing something like this but didn't work.

    Code:
    void sort(Student s[], int count)
    {
    	int i,j, flag = 1;
    	double temp;
    	
    	for(i=1; (i<=count-1) && flag; i++)
    	{
    		flag = 0;
    		for (j=0; j<count-1; j++)
    		{
    			if (s[j+1].aveS > s[i].aveS)
    			{
    				temp = s[j].aveS;
    				s[j].aveS = s[j+1].aveS;
    				s[j+1].aveS = temp;
    				flag = 1;
    			}
    		}
    	}
    }

  8. #8
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Kinto View Post
    Code:
    	for(i=1; (i<=count-1) && flag; i++)
    i <= count - 1
    is the same thing as
    i < count
    which is a lot easier to read

    your problem probably occurred when you tried to do
    Code:
    for (j = 0; j < count - 1; j++)
    it might of been cause by your use of flag though, not too sure what you want that to do
    actually, that's probably it

    also note that you are comparing s[j+1].aveS with s[i].aveS which will skip s[0] (and you probably made the > count - 1 comparison because it would blow your array (if count represents the size of your array))
    Last edited by ಠ_ಠ; 05-30-2009 at 11:20 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    10
    Quote Originally Posted by ಠ_ಠ View Post
    i <= count - 1
    is the same thing as
    i < count
    which is a lot easier to read

    your problem probably occurred when you tried to do
    Code:
    for (j = 0; j < count - 1; j++)
    it might of been cause by your use of flag though, not too sure what you want that to do
    actually, that's probably it

    also note that you are comparing s[j+1].aveS with s[i].aveS which will skip s[0] (and you probably made the > count - 1 comparison because it would blow your array (if count represents the size of your array))
    Fixed that and sorted the average, but what do i need to sort the struct? now the students have the wrong average.

  10. #10
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Kinto View Post
    Fixed that and sorted the average, but what do i need to sort the struct? now the students have the wrong average.
    swap all of the members of the struct, not just the averages
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    10
    Getting late, I guess I'll resume tomorrow. Thanks for all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Guidance, please.
    By mattagrimonti in forum C Programming
    Replies: 2
    Last Post: 11-26-2008, 07:50 AM
  2. need guidance to connect to serial port
    By gnychis in forum Linux Programming
    Replies: 1
    Last Post: 06-02-2005, 10:10 AM
  3. Guidance Councilor (rant...sort of)
    By dP munky in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-03-2003, 03:03 PM
  4. Audio guidance.
    By Sebastiani in forum Windows Programming
    Replies: 6
    Last Post: 12-22-2002, 08:14 AM
  5. advice and possibly guidance
    By nazri81 in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2002, 09:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21