Question: Complete the following using the C programming language Write to prove that your code can handle millions of records. Extend the student record program so
Complete the following using the C programming language
Write to prove that your code can handle millions of records. Extend the student record program so that:
(1) All arrays are dynamically allocated (specifically, even the sr_list array is dynamically allocated)
(2) The user can add new student records to the list
(3) The user can save the list of student records
(4) The user can load a previously saved list of student records
#include
#include
#include
typedef struct StudentRecordTag
{
int ID;
char* first_name;
char* last_name;
float* grades;
int num_grades;
int days_absent;
char* memo;
} StudentRecord;
StudentRecord make_student_record(
int ID,
char const* first_name,
char const* last_name,
float const* grades,
int num_grades,
int days_absent,
char* memo
)
{
StudentRecord sr;
sr.ID = ID;
sr.days_absent = days_absent;
if (first_name != NULL)
{
sr.first_name = malloc(strlen(first_name) + 1);
if (sr.first_name != NULL)
{
strcpy(sr.first_name, first_name);
}
}
if (last_name != NULL)
{
sr.last_name = malloc(strlen(last_name) + 1);
if (sr.last_name != NULL)
{
strcpy(sr.last_name, last_name);
}
}
if (grades != NULL)
{
sr.grades = malloc(num_grades*sizeof(float));
if (sr.grades != NULL)
{
memcpy(sr.grades, grades, num_grades*sizeof(float));
}
}
if (memo != NULL)
{
sr.memo = malloc(strlen(memo) + 1);
if (sr.memo != NULL)
{
strcpy(sr.memo, memo);
}
}
return sr;
}
void destroy_student_record(StudentRecord* p_sr)
{
free(p_sr->first_name);
free(p_sr->last_name);
free(p_sr->grades);
free(p_sr->memo);
p_sr->ID = 0;
p_sr->first_name = NULL;
p_sr->last_name = NULL;
p_sr->grades = NULL;
p_sr->num_grades = 0;
p_sr->days_absent = 0;
p_sr->memo = NULL;
}
int main()
{
printf("%lld ", sizeof(StudentRecord));
int const num_students = 2000;
StudentRecord sr_list[num_students];
for (int student_idx = 0; student_idx < num_students;
++student_idx)
{
int const num_grades = 3;
float const grades[3] = {50, 75.5, 32.1};
sr_list[student_idx] = make_student_record(
student_idx,
"Johnny",
"Doeberman",
grades,
num_grades,
12,
"This student never came to class!"
);
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ");
printf("ID: %d ", sr_list[student_idx].ID);
printf("First Name: %s ", sr_list[student_idx].first_name);
printf("Last Name: %s ", sr_list[student_idx].last_name);
printf("Grades: ");
for (int grade_idx = 0; grade_idx < num_grades; ++grade_idx)
{
printf("(%d) %3.1f", grade_idx+1,
sr_list[student_idx].grades[grade_idx]);
if (grade_idx < num_grades-1)
{
printf(", ");
}
else
{
printf(" ");
}
}
printf("Days Absent: %d ",
sr_list[student_idx].days_absent);
printf("Memo: %s ", sr_list[student_idx].memo);
printf(" ");
destroy_student_record(&sr_list[student_idx]);
}
printf("bye! ");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
