Compare commits

...

2 Commits

Author SHA1 Message Date
f8749e3427 Fixed search the student 2020-08-03 11:34:26 +07:00
7bffb64752 Completed find failed students and fixed 2020-08-03 11:24:31 +07:00
2 changed files with 67 additions and 15 deletions

View File

@ -18,3 +18,17 @@ CS78
2 2
50 50
20 20
SMC1
7890
CGS
4
40
50
60
70
CHS10
7823
CHS2
2
19
90

View File

@ -45,6 +45,7 @@ void release(STUDENT *data);
int find_min_in_array(int *array); int find_min_in_array(int *array);
STUDENT *search_student_by_name_or_id(char search_key[NAME_SIZE]); STUDENT *search_student_by_name_or_id(char search_key[NAME_SIZE]);
STUDENT *find_maximum_avg(); STUDENT *find_maximum_avg();
STUDENT *add_last_student(STUDENT *nodes, STUDENT *element);
// core functions // core functions
void display_students(); void display_students();
@ -124,6 +125,7 @@ void display_students()
void print_student(STUDENT *student) void print_student(STUDENT *student)
{ {
int records_count = 0;
printf("\n================ Student Details ================\n"); printf("\n================ Student Details ================\n");
while (student != NULL) while (student != NULL)
{ {
@ -141,8 +143,12 @@ void print_student(STUDENT *student)
printf("]\n"); printf("]\n");
printf("Avg: %.2f\n", student->course_info.avg); printf("Avg: %.2f\n", student->course_info.avg);
student = student->next; student = student->next;
records_count++;
} }
// output the records count
printf("\nRecords: %d has been loaded successfully!\n", records_count);
// clean up the memory // clean up the memory
// after output the data // after output the data
// because we don;t need it anymore after output // because we don;t need it anymore after output
@ -235,7 +241,7 @@ void search_student()
STUDENT *found_student = search_student_by_name_or_id(search_key); STUDENT *found_student = search_student_by_name_or_id(search_key);
if (found_student == NULL) if (found_student == NULL)
{ {
printf("No student found!"); printf("\nNo student found!\n");
return; return;
} }
@ -254,6 +260,13 @@ STUDENT *search_student_by_name_or_id(char search_key[NAME_SIZE])
if (strcmp(temp->student_info.name, search_key) == 0 || strcmp(temp->student_info.id, search_key) == 0) if (strcmp(temp->student_info.name, search_key) == 0 || strcmp(temp->student_info.id, search_key) == 0)
{ {
printf("\nSearch found with key: %s\n", search_key); printf("\nSearch found with key: %s\n", search_key);
// set the temp next to null
// and free up the memory
temp->next = NULL;
free(temp->next);
// return the current temp
// that found in current search
return temp; return temp;
} }
temp = temp->next; temp = temp->next;
@ -314,6 +327,35 @@ int find_min_in_array(int *array)
return min; return min;
} }
STUDENT *add_last_student(STUDENT *nodes, STUDENT *element)
{
STUDENT *temp = nodes, *node;
node = (struct student_tag *)malloc(sizeof(struct student_tag));
node->student_info = element->student_info;
node->course_info = element->course_info;
node->next = NULL;
if (nodes == NULL)
{
nodes = node;
return nodes;
}
// store current as tempo
// ref for nodes
temp = nodes;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = node;
return nodes;
}
// ISSUE: can't find all failed students // ISSUE: can't find all failed students
// It's just return one record back // It's just return one record back
STUDENT *find_failed_mark(int upper_mark) STUDENT *find_failed_mark(int upper_mark)
@ -328,28 +370,24 @@ STUDENT *find_failed_mark(int upper_mark)
int count = count_elements(temp); int count = count_elements(temp);
printf("Temp count elements: %d", count); printf("Temp count elements: %d", count);
int run_count = 0;
while (temp != NULL) while (temp != NULL)
{ {
run_count++;
int min = find_min_in_array(temp->course_info.marks); int min = find_min_in_array(temp->course_info.marks);
if (min < upper_mark) if (min < upper_mark)
{ {
if (failed_students == NULL) printf("\nRun min: %d\n", min);
{ failed_students = add_last_student(failed_students, temp);
failed_students = temp;
failed_students->next = NULL;
}
else
{
while (failed_students->next != NULL)
{
failed_students = failed_students->next;
}
failed_students->next = temp;
}
} }
temp = temp->next; temp = temp->next;
} }
printf("\nRun count: %d", run_count);
return failed_students; return failed_students;
} }