Skip to content

Instantly share code, notes, and snippets.

@joho
Created August 18, 2012 10:13
Show Gist options
  • Save joho/3385854 to your computer and use it in GitHub Desktop.
Save joho/3385854 to your computer and use it in GitHub Desktop.
An assignment from my first C class way back in 2002
/*
* John Barton
* 201165023
* jrba@deakin.edu.au
*
* The purpose of this program is to read a file, do some things with the data
* in that file, then output the results into another file.
*
* About the only thing i learnt about software development from this is that
* sitting down to do some "proper" pseudocode does me no good, when i look
* at problems, i'm already coding it in some kind of half old school BASIC
* and half C in my head, and trying to write it out another way doesn't work,
* so i haven't bothered to submit any psuedocode. The defining diagrams kind
* of helped, but i probably don't do those right either.
*
* About C i learnt that working with files is fairly simple, and the easiest
* way to keep track on your pointers and everything is to have some scratch
* paper near the computer to jot down wheter you're working with values,
* addresses or whatever, because trying to keep all that in your head
* doesn't work. Also turning on line numbers in vim really helps to recognise
* errors after gcc tells me about them.
*/
#include <stdio.h>
int read_record(FILE *, char *, int *, char *);
void write_record(FILE *, char [], int, char, char []);
void calculate_grade(int, char *);
void write_headings(FILE *);
void write_header(FILE *);
void acc_totals(int *, int *, int *, int *, int *, int, char, char *);
void write_summary(FILE *, int, int, float, float, float);
int main(void)
{
char studentID[20], gender, grade[2];
int examMark, totalFemale, totalMale, totalMaleHD, totalFemaleHD,
totalMarks, count;
float percentMaleHD, percentFemaleHD, averageMark;
FILE *student, *report;
totalFemale = 0;
totalMale = 0;
totalMaleHD = 0;
totalFemaleHD = 0;
totalMarks = 0;
count = 0;
student = fopen("student.csv", "r");
report = fopen("report.rpt", "w");
write_header(report);
write_headings(report);
while( read_record(student, studentID, &examMark, &gender) != EOF )
{
calculate_grade(examMark, grade);
gender = toupper(gender);
acc_totals(&totalFemale, &totalMale, &totalMaleHD, &totalFemaleHD, &totalMarks, examMark, gender, grade);
write_record(report, studentID, examMark, gender, grade);
count++;
}
percentMaleHD = ((float)totalMaleHD / (float)totalMale) * 100;
percentFemaleHD = ((float)totalMaleHD / (float)totalFemale) * 100;
averageMark = (float)totalMarks / (float)count;
write_summary(report, totalMale, totalFemale, percentFemaleHD, percentMaleHD, averageMark);
printf("Report written to file \"report.rpt\".\n");
fclose(student);
fclose(report);
return(0);
}
int read_record(FILE *filename, char *studentID, int *examMark, char *gender)
{
int returnVal = fscanf(filename, "%[^,]%*c%d%*c%c\n", studentID, examMark, gender);
return(returnVal);
}
void write_record(FILE *filename, char id[20], int examMark, char gender, char grade[2])
{
fprintf(filename, "%-19s%10d%12c%13s\n", id, examMark, gender, grade);
}
void calculate_grade(int examMark, char *grade)
{
if( (examMark >= 0) && (examMark < 50) )
strcpy(grade, "N");
else if( (examMark >= 50) && (examMark < 60) )
strcpy(grade, "P");
else if( (examMark >= 60) && (examMark < 70) )
strcpy(grade, "C");
else if( (examMark >= 70) && (examMark < 80) )
strcpy(grade, "D");
else if( (examMark >= 80) && (examMark <=100) )
strcpy(grade, "HD");
}
void write_header(FILE *filename)
{
fprintf(filename, "\nReport made by student_report.c about student results and gender\n\n");
}
void write_headings(FILE *filename)
{
fprintf(filename, "Student ID Exam Mark Gender Grade\n\n");
}
void acc_totals(int *totalFemale, int *totalMale, int *totalMaleHD, int *totalFemaleHD, int *totalMarks, int examMark, char gender, char *grade)
{
if(gender == 'F')
*totalFemale += 1;
else
*totalMale += 1;
if( (gender == 'F') && (*grade == 'H') )
*totalFemaleHD += 1;
else if( (gender == 'M') && (*grade == 'H') )
*totalMaleHD += 1;
*totalMarks += examMark;
}
void write_summary(FILE *filename, int totalMale, int totalFemale, float percentFemaleHD, float percentMaleHD, float averageMark)
{
fprintf(filename, "\n\nReport Summary:\n");
fprintf(filename, "Total male students: %4d\n", totalMale);
fprintf(filename, "Total female students: %4d\n\n", totalFemale);
fprintf(filename, "Percentage of males with a HD: %4.2f%\n", percentMaleHD);
fprintf(filename, "Percentage of females with a HD: %4.2f%%\n\n", percentFemaleHD);
fprintf(filename, "Average mark: %4.2f\n", averageMark);
}
@joho
Copy link
Author

joho commented Aug 18, 2012

It appears I was a big fan of not using braces if i could help it back in the day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment