/* TMA02 Q3d */ /* Program by Selina Sze Min Na */ /*************************************************** Skeleton for MT258 TMA02 Oct 2000 Question 3 (d) and (e) */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 2048 /* A structure for holding student records */ struct TMARecord { char studentid[8]; int group; int daysLate; int articlesRead; int articlesPosted; }; struct TMARecord records[SIZE]; /* the actual store */ int recordCount = 0; /* number of records currently in the store */ /* Implement this for Question 3 (d) */ void LateAndEarlyGroups(struct TMARecord records[], int size){ int lateCount[25]; int totalLate[25]; int max = 0; int maxGroupNo = 0; printf("\t\t>>> LateAndEarlyGroups <<<\n"); printf("\n"); for (int i = 1; i <= 24; i++) { lateCount[i] = 0; totalLate[i] = 0; } for (int i = 0; i < size; i++) { if (records[i].daysLate > 0) { lateCount[records[i].group]++; totalLate[records[i].group] += records[i].daysLate; } } for (int i = 1; i <= 24; i++) { if (lateCount[i] > 0) { printf("Group %d has %d late submission students.\n", i, lateCount[i]); printf("Group %d has %0.1f average number of days late.\n", i, (float)totalLate[i]/(float)lateCount[i]); } } for (int i = 1; i <= 24; i++) { if (lateCount[i] > max) { max = lateCount[i]; maxGroupNo = i; } } printf("\nGroup number %d has the largest number of late submission students.\n\n", maxGroupNo); } /* Implement this for Question 3 (d) */ void AnalyseDiscussion(struct TMARecord records[], int size) { printf("\t\t>>> AnalyseDiscussion <<<\n\n"); int largestArticlesRead = 0, largestArticlesPosted = 0; char studentid1[8], studentid2[8]; int sumOfArticlesRead = 0, sumOfArticlesPosted = 0; for (int i = 0; i < size; i++) { if (records[i].articlesRead > largestArticlesRead) { largestArticlesRead = records[i].articlesRead; strcpy(studentid1, records[i].studentid); } if (records[i].articlesPosted > largestArticlesPosted) { largestArticlesPosted = records[i].articlesPosted; strcpy(studentid2, records[i].studentid); } if (records[i].articlesRead > 0) { sumOfArticlesRead += records[i].articlesRead; } if (records[i].articlesPosted > 0) { sumOfArticlesPosted += records[i].articlesPosted; } } printf("Student ID %s has the largest Articles Read.\n", studentid1); printf("Student ID %s has the largest Articles Posted.\n", studentid2); printf("The sum of articles read of all records is %d.\n", sumOfArticlesRead); printf("The sum of articles posted of all records is %d.\n", sumOfArticlesPosted); printf("The average number of articles read per student is %0.1f.\n", (float)sumOfArticlesRead/(float)size); printf("The average number of articles posted per student is %0.1f.\n", (float)sumOfArticlesPosted/(float)size); } /* Implement this for Question 4 */ void AnalyseAdvanced(struct TMARecord records[], int size) { printf("\n\t\t>>> A-Challenge Question <<<\n"); printf("\t>>> AnalyseAdvanced not implementd. <<<\n\n"); } /* Prints all the records currently stored in the array records */ void printRecords() { for (int i=0; i<recordCount; i++) { printf("%s %d %d %d %d\n", records[i].studentid, records[i].group, records[i].daysLate, records[i].articlesRead, records[i].articlesPosted); } } /* Loads records from a file and stores in the array records */ void loadRecordFile(char* f) { FILE* infile; char buffer[256]; /* open the file first and report errors if necessary */ if ((infile = fopen(f, "r")) == NULL) { fprintf(stderr, "Error in loading '%s'.\n", f); getchar(); exit(1); } /* read the file line by line and add every record to the array */ while (fgets(buffer, 256, infile) != NULL) { if (sscanf(buffer, "%s %d %d %d %d", records[recordCount].studentid, &(records[recordCount].group), &(records[recordCount].daysLate), &(records[recordCount].articlesRead), &(records[recordCount].articlesPosted)) != 5) { fprintf(stderr, "Error in reading file at record line %d.\n", recordCount); getchar(); exit(1); } recordCount++; } fclose(infile); } /* main driving program */ int main() { loadRecordFile("q3ddata.txt"); /* printRecords(); */ LateAndEarlyGroups(records, recordCount); AnalyseDiscussion(records, recordCount); AnalyseAdvanced(records, recordCount); getchar(); return 0; }