/* TMA02 q3d */
/*author: Chan Chi Ming, s93504843, dtae of authoring: 29/1/2001,
this program is used to print out the analysis of the data about the
assignmnent submission pattern of MT258 students.*/
#include <condefs.h>
#pragma hdrstop
#include <stdio.h>
#include <stdlib.h>
#define SIZE 2048
//---------------------------------------------------------------------------
#pragma argsused
struct TMARecord { /* A structure for holding student records*/
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 */
void LateAndEarlyGroups(struct TMARecord records[], int size){
/*function body of LateAndEarlyGroups*/
int i; /*index*/
int gpnumber=0; /*group number*/
int Latecount[24]; /*array stores the number of late student*/
int maxLate=0; /*variable used to store the maximum late day*/
int maxgp=0; /*variable used to store the group number of largest late day*/
float sumLate [24]; /*array stores the sum of late submission day*/
float avgLate[24]; /*array use to calculae the average late day*/
for (i = 0; i < 24; i++){ /*initiate the value of arrays*/
Latecount[i] = 0;
sumLate[i] = 0.0;
}
for (i = 0; i < recordCount; i++) {
gpnumber = records[i].group; /*match the group number*/
if (records[i].daysLate > 0) {
Latecount[gpnumber-1] = Latecount[gpnumber-1] + 1; /*increment no of late*/
sumLate[gpnumber-1] = sumLate[gpnumber-1] + records[i].daysLate; /*increment no of late day*/
}
}
for (i = 0; i<24; i++) {
if (Latecount[i] > 0) {
avgLate[i] = sumLate[i] / Latecount[i]; } /*calculate the average late day*/
else {
avgLate[i] = 0.0; }
if (Latecount[i] > maxLate) { /*find out the larest late count and its group*/
maxLate = Latecount[i];
maxgp = i; }
}
/*print out result*/
printf("| Group No | No of Late submission | Average No of days late |\n");
for (i = 0; i < 9; i++){
printf("| %2d | %2d | %4.1f |\n",
i+1, Latecount[i], avgLate[i]);
}
printf("\n");
printf("Group %d has the largest number of late submission students.\n", maxgp);
}
void AnalyseDiscussion(struct TMARecord records[], int size) {
/*function body of AnalyseDiscussion*/
int IDmaxAR; /*variable stores index of student with maximum articles read*/
int IDmaxAP; /*variable stores index of student with maximum articles posted*/
int i, maxAR = 0, maxAP = 0, sumAR = 0, sumAP = 0;
float averAR, averAP; /*variables store average articles read and average articles posted*/
for (i=0; i<recordCount; i++){
if (records[i].articlesRead > maxAR){ /*find out the student with
largest articlesRead*/
maxAR = records[i].articlesRead;
IDmaxAR = i; }
if (records[i].articlesPosted > maxAP){ /*find out the student with
largest articlesPosted*/
maxAP = records[i].articlesPosted;
IDmaxAP = i; }
sumAR = sumAR + records[i].articlesRead; /*find out the sum of articlesRead*/
sumAP = sumAP + records[i].articlesPosted; /*find out the sum of articlesPosted*/
}
averAR = sumAR / recordCount; /*find out the average of articlesRead*/
averAP = sumAP / recordCount; /*find out the average of articlesPosted*/
/*print out result*/
printf("The StudentID with largest number of articles read: %s\n", records[IDmaxAR].studentid);
printf("The StudentID with largest number of articles posted: %s\n", records[IDmaxAP].studentid);
printf("The sum of articles read of all records: %d\n", sumAR);
printf("The sum of articles posted of all records: %d\n", sumAP);
printf("The average number of articles read per student: %.1f\n", averAR);
printf("The average number of articles posted per student: %.1f\n", averAP);
}
void AnalyseAdvanced(struct TMARecord records[], int size) {
/*Function body of AnalyseAdvanced*/
int i, j; /*index*/
int gpnumber; /*group number*/
bool result; /*flag to check the theory testing result*/
int Readcount[8][4]; /*array hold the number of article read*/
float StudentCount[8][4]; /*array holds the number of student*/
float AvgRead[9][4]; /*array holds the average number of article read*/
for(i=0; i<=7; i++) { /*initiate the value of arrays*/
for(j=0; j<=3; j++) {
Readcount[i][j]=0;
StudentCount[i][j]=0.0; }}
for(i=0; i<=8; i++) {
for(j=0; j<=3; j++) {
AvgRead[i][j] =0.0;}}
for(i=0; i<recordCount; i++) {
gpnumber = records[i].group; /*match the group number*/
if (records[i].daysLate <= 0) {
/*increment articles Read of student with no late submission*/
Readcount[gpnumber-1][0] = Readcount[gpnumber-1][0] + records[i].articlesRead;
StudentCount[gpnumber-1][0] = StudentCount[gpnumber-1][0] + 1;
}
if ((records[i].daysLate > 0) && (records[i].daysLate <= 7)){
/*increment articles Read of students with 1-7 late days*/
Readcount[gpnumber-1][1] = Readcount[gpnumber-1][1] + records[i].articlesRead;
StudentCount[gpnumber-1][1] = StudentCount[gpnumber-1][1] + 1;
}
if ((records[i].daysLate > 7) && (records[i].daysLate <= 14)){
/*increment articles Read of students with 8-14 late days*/
Readcount[gpnumber-1][2] = Readcount[gpnumber-1][2] + records[i].articlesRead;
StudentCount[gpnumber-1][2] = StudentCount[gpnumber-1][2] + 1;
}
if (records[i].daysLate > 14){
/*increment articles Read of students with more than 14 late days*/
Readcount[gpnumber-1][3] = Readcount[gpnumber-1][3] + records[i].articlesRead;
StudentCount[gpnumber-1][3] = StudentCount[gpnumber-1][3] + 1;
}
}
for(i=0; i<=7; i++) { /*fill the table with average article read for each group & late day group*/
for(j=0; j<=3; j++) {
AvgRead[i][j] = Readcount[i][j] / StudentCount[i][j];
}}
for(j=0; j<=3; j++) { /*find out the overall average article read*/
for(i=0; i<7; i++) {
AvgRead[8][j] = AvgRead[8][j] + AvgRead[i][j];
}
}
if ((AvgRead[8][3]<AvgRead[8][2])&&(AvgRead[8][2]<AvgRead[8][1])&&(AvgRead[8][1]<AvgRead[8][0])){
result = true; } /* check whether the theory is true or not by compare the ovarage average*/
else {
result = false; }
/*print out the result*/
printf("Table of analysis of articlesRead and daysLate: \n");
printf("|days Late| Gp1 | Gp2 | Gp3 | Gp4 | Gp5 | Gp6 | Gp7 | Gp8 |Overall|");
printf("\n| < 0 |");
for (i=0; i<=8; i++){
printf("%5.1f |",AvgRead[i][0]); }
printf("\n| 1-7 |");
for (i=0; i<=8; i++){
printf("%5.1f |",AvgRead[i][1]); }
printf("\n| 8-14 |");
for (i=0; i<=8; i++){
printf("%5.1f |",AvgRead[i][2]); }
printf("\n| >14 |");
for (i=0; i<=8; i++){
printf("%5.1f |",AvgRead[i][3]); }
printf("\n");
printf("By analyse the overall result, the theory that reading more articles \n");
printf("can reduce the extent of late submisison is ");
if (result == true){
printf("TRUE.\n"); }
else {
printf("FALSE.\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(int argc, char **argv)
{
int choose;
loadRecordFile("q3ddata.txt");
printf("Choose the funtion number: \n"); /*show the menu*/
printf("1. print out all record\n");
printf("2. Late and Early Group\n");
printf("3. Analyse Discussion\n");
printf("4. Analyse Advanced\n");
scanf("%d", &choose);
switch (choose) /*call suitable function according to user choose*/
{
case 1: printRecords();
break;
case 2: LateAndEarlyGroups(records, recordCount);
break;
case 3: AnalyseDiscussion(records, recordCount);
break;
case 4: AnalyseAdvanced(records, recordCount);
break;
}
fflush(stdin);
getchar();
return 0;
}