Commit 958c5224 by unknown

finish hw2

parent ecda50b8
8 5
5 4 5 0 2
0 0 0 3 3
2 1 3 4 3
5 4 3 1 2
5 5 5 1 4
4 5 3 5 4
4 4 3 4 5
5 5 5 5 99
\ No newline at end of file
10 10
5 2 2 3 5 1 1 5 1 1
4 3 0 2 2 0 3 1 2 0
4 4 1 4 5 0 4 4 4 0
1 2 5 2 2 2 2 4 1 1
4 4 3 1 4 2 3 1 2 2
3 2 3 5 1 1 2 1 5 2
1 0 2 2 5 3 1 1 1 5
5 2 1 3 3 2 2 5 2 2
3 4 1 2 1 0 3 0 4 4
2 1 4 3 4 3 3 3 3 5
......@@ -17,51 +17,115 @@ typedef struct {
float average;
} student;
void getArrayDimension(char*, int);
void getStudentGrades(student*, int, int);
/*
* Function: getArrayDimension
* Description: get the array dimension from the text file
* Arguments: output - char pointer, the output array to write to
* size - int, size of the output
* data - FILE pointer, the FILE stream pointer of the text file
* Return: void
*/
void getArrayDimension(char*, int, FILE*);
/*
* Function: getStudentGrades
* Description: get the student grades from the file stream line by line
* Arguments: studentList - student pointer, the student array output
* studentsNo - int, the number of student parsed from the file stream
* coursesNo - int, the number of courses each student have
* data - FILE pointer, the FILE stream pointer of the text file
* Return: void
*/
void getStudentGrades(student*, int, int, FILE*);
/*
* Function: displayFinalMatrix
* Description: print the matrix to standard out
* Arguments: studentList - student pointer, the list of students and their data
* studentsNo - int, the number of student parsed from the file stream
* coursesNo - int, the number of courses each student have
* Return: void
*/
void displayFinalMatrix(student*, int, int);
/*
* Function: getAverage
* Description: calculate the average score from student type struct
* Arguments: currentStudent - student, the data of the current student to calculate
* coursesNo - int, the number of courses each student have
* Return: float, the average calculated
*/
float getAverage(student, int);
/*
* Function: findBestStudent
* Description: find the best student from all the student data array
* Arguments: studentList - student pointer, the list of students and their data
* studentsNo - int, the number of student parsed from the file stream
* Return: int, the ID of the best student
*/
int findBestStudent(student*, int);
int main () {
//Open the stream to data.txt, with read only
FILE* data = fopen("data.txt", "r");
// String buffer to get the array dimension from the file stream
char arrayDimension[20];
getArrayDimension(arrayDimension, 20);
getArrayDimension(arrayDimension, 20, data);
// Get the first value from the first line in file
char* buff = strtok(arrayDimension, " ");
int studentsNo = atoi(buff);
// Get the second value from the first line in file
buff = strtok(NULL, " ");
int coursesNo = atoi(buff);
if (studentsNo > 15 || coursesNo > 15 || studentsNo < 1 || coursesNo < 1) {
puts("Both students number and courses number cannot be bigger than 15 or smaller than 1");
return 1;
}
// Create a student type array to enter their data
student studentList[studentsNo];
getStudentGrades(studentList, studentsNo, coursesNo);
getStudentGrades(studentList, studentsNo, coursesNo, data);
displayFinalMatrix(studentList, studentsNo, coursesNo);
int bestStudent = findBestStudent(studentList, studentsNo);
printf("\nBest Student: STUDENT%0.2d\n", bestStudent + 1);
fclose(data);
return 0;
}
void getArrayDimension(char* output, int size) {
FILE* data = fopen("data.txt", "r");
void getArrayDimension(char* output, int size, FILE* data) {
// Get the first line of the file, which is the array dimensions
fgets(output, sizeof(char) * size, data);
fclose(data);
}
void getStudentGrades(student* studentList, int studentsNo, int coursesNo) {
char buffer[100];
void getStudentGrades(student* studentList, int studentsNo, int coursesNo, FILE* data) {
// Buffer to get text lines from the file
int buffLength = 100;
char buffer[buffLength];
// buffer to get each grade in the buffer string
char* mark;
FILE* data = fopen("data.txt", "r");
fgets(buffer, sizeof(buffer), data);
for (int i = 0; i < studentsNo; i++) {
fgets(buffer, sizeof(buffer), data);
// Read each lines from the array
fgets(buffer, buffLength, data);
// Divide each grade separated by " "
if (strlen(buffer) < coursesNo) {
puts("The number of students is inaccurate!");
exit(1);
}
mark = strtok(buffer, " ");
// Assign the first grade to each student in the studentList array
studentList[i].grades[0] = atoi(mark);
for (int j = 1; j < coursesNo; j++) {
// Continue getting the grades and assigning them
mark = strtok(NULL, " ");
studentList[i].grades[j] = atoi(mark);
if (mark != NULL) studentList[i].grades[j] = atoi(mark);
}
}
fclose(data);
}
void displayFinalMatrix(student* studentList, int studentsNo, int coursesNo) {
......@@ -71,10 +135,12 @@ void displayFinalMatrix(student* studentList, int studentsNo, int coursesNo) {
}
printf("AVERAGE\n");
for (int i = 0; i < studentsNo; i++) {
// For each student, we print their grades array included in their struct
printf(" STUDENT%0.2d", i + 1);
for (int j = 0; j < coursesNo; j++) {
printf("\t %d", studentList[i].grades[j]);
}
// Calculate the student average based on their own grades array
studentList[i].average = getAverage(studentList[i], coursesNo);
printf("\t %0.3f\n", studentList[i].average);
}
......@@ -83,8 +149,10 @@ void displayFinalMatrix(student* studentList, int studentsNo, int coursesNo) {
float getAverage(student currentStudent, int coursesNo) {
float total = 0;
for (int i = 0; i < coursesNo; i++) {
// Add all their grades together
total += currentStudent.grades[i];
}
// Divide by the courses number to get the average
return (float)(total / coursesNo);
}
......@@ -92,10 +160,12 @@ int findBestStudent(student* studentList, int studentsNo) {
int bestStudent;
float bestScore = -1;
for (int i = 0; i < studentsNo; i++) {
// If we find a better student, bestStudent is changed
if (studentList[i].average > bestScore) {
bestStudent = i;
bestScore = studentList[i].average;
}
}
// After the loop, we can safely return this is the best student
return bestStudent;
}
hellomake: homework2.c
gcc -o homework2 homework2.c
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment