
Dorsa R.
asked 08/31/20please write a C program to :
we have a list of
student's firstname [80]
lastname[80]
student's number[11]
grade (float)
a program to :
add a new student to the list
remove a student from the list
search a student by student number
organize the students by their grade
save the list to a file
1 Expert Answer

Patrick B. answered 09/01/20
Math and computer tutor/teacher
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STUDENT_NAME_LENGTH (80)
#define STUDENT_ID_NUM_SIZE (11)
typedef struct _TStudent
{
char firstName[STUDENT_NAME_LENGTH];
char lastName[STUDENT_NAME_LENGTH];
char studentNum[STUDENT_ID_NUM_SIZE];
float grade;
} * TStudent;
#define STUDENT_REC_SIZE (sizeof(struct _TStudent))
typedef struct _TStudentDbase
{
int numStudentRecs;
TStudent A;
} * TStudentDbase;
#define STUDENT_DBASE_SIZE ( sizeof(struct _TStudentDbase))
Student_Init( TStudent studentRec)
{
memset(studentRec,0,STUDENT_REC_SIZE);
}
//constructor
Student_Create( TStudent studentRec, char * fName, char * lName, char *id, float g)
{
Student_Init(studentRec);
strcpy( studentRec->firstName,fName);
strcpy(studentRec->lastName,lName);
strcpy(studentRec->studentNum,id);
studentRec->grade = g;
}
//copy constructor
Student_Copy( TStudent studentRecDest, TStudent studentRecSource)
{
strcpy(studentRecDest->firstName,studentRecSource->firstName);
strcpy(studentRecDest->lastName,studentRecSource->lastName);
strcpy(studentRecDest->studentNum,studentRecSource->studentNum);
studentRecDest->grade = studentRecSource->grade;
}
//accessors: setters and geters
Student_GetName( TStudent studentRec, char * fName, char * lName)
{
strcpy(fName,studentRec->firstName);
strcpy(lName,studentRec->lastName);
}
Student_GetIdNum(TStudent studentRec, char * idNum)
{
strcpy(idNum,studentRec->studentNum);
}
float Student_GetGrade(TStudent studentRec) { return(studentRec->grade); }
Student_SetName(TStudent studentRec, char * fName, char * lName)
{
strcpy(studentRec->firstName,fName);
strcpy(studentRec->lastName,lName);
}
Student_SetGrade( TStudent studentRec, float g)
{
studentRec->grade = g;
}
int CompareStudentByGrade( const void * rec1, const void * rec2)
{
TStudent studentRec1 = (TStudent) rec1;
TStudent studentRec2 = (TStudent) rec2;
int iReturn=0;
float gradeDiff = studentRec1->grade - studentRec2->grade;
if (gradeDiff!=0)
{
iReturn = (gradeDiff>0) ? 1 : -1;
}
return(iReturn);
}
int CompareStudentByName( const void * rec1, const void * rec2)
{
TStudent studentRec1 = (TStudent) rec1;
TStudent studentRec2 = (TStudent) rec2;
int iReturn=strcmp(studentRec1->lastName,studentRec2->lastName);
if (iReturn==0)
{
iReturn=strcmp(studentRec1->lastName,studentRec2->lastName);
if (iReturn==0)
{
iReturn = CompareStudentByGrade((const void*)rec1,(const void *)rec2);
}
}
return(iReturn);
}
Student_SerializeToCSV( TStudent studentRec, char * csvBuff)
{
sprintf(csvBuff,"%s,%s,%s,%6.2f",studentRec->lastName,studentRec->firstName,studentRec->studentNum,studentRec->grade);
}
Student_ParseCSV( TStudent studentRec, char * csvBuff)
{
strcpy(studentRec->lastName, strtok(csvBuff,","));
strcpy(studentRec->firstName,strtok(NULL,","));
strcpy(studentRec->studentNum,strtok(NULL,","));
studentRec->grade = atof(strtok(NULL,","));
}
ReadStudentFile(TStudentDbase studentDB)
{
FILE * fptr = fopen("E:\\student.txt","r");
if (fptr!=NULL)
{
fscanf(fptr,"%d",&studentDB->numStudentRecs);
int N = studentDB->numStudentRecs;
fclose(fptr);
fptr = fopen("E:\\student.dat","r");
if (fptr != NULL)
{
studentDB->A = (TStudent) malloc(N * STUDENT_REC_SIZE);
for (int iloop=0; iloop<N; iloop++)
{
char inbuff[STUDENT_REC_SIZE+1];
fgets(inbuff,STUDENT_REC_SIZE, fptr);
Student_ParseCSV(&studentDB->A[iloop],inbuff);
}
fclose(fptr);
}
else
{
printf(" ERROR READING STUDENT REC FILE \n");
}
}
else
{
printf(" ERROR reading student index file \n");
}
}
WriteStudentFile(TStudentDbase studentDB)
{
FILE * fptr = fopen("E:\\student.txt","w");
if (fptr!=NULL)
{
fprintf(fptr,"%d",studentDB->numStudentRecs);
int N = studentDB->numStudentRecs;
fclose(fptr);
fptr = fopen("E:\\student.dat","w");
if (fptr != NULL)
{
for (int iloop=0; iloop<N; iloop++)
{
char inbuff[STUDENT_REC_SIZE+1];
Student_SerializeToCSV(&studentDB->A[iloop],inbuff);
fputs(inbuff,fptr);
fprintf(fptr,"\n");
}
fclose(fptr);
}
else
{
printf(" ERROR READING STUDENT REC FILE \n");
}
}
else
{
printf(" ERROR reading student index file \n");
}
}
Student_InsertAddNew( TStudentDbase studentDB, TStudent newStudentRec)
{
int N = studentDB->numStudentRecs;
TStudent tempArray = (TStudent)realloc(studentDB->A,(N+1)*STUDENT_REC_SIZE);
if (tempArray!=NULL)
{
memcpy(&tempArray[N],newStudentRec,STUDENT_REC_SIZE); //new record is added here
studentDB->A = tempArray;
tempArray = NULL;
studentDB->numStudentRecs++; //updates the record counter
WriteStudentFile(studentDB); //writes them to file
}
}
Student_RemoveDelete(TStudentDbase studentDB, int iIndexPos)
{
int N = studentDB->numStudentRecs;
if ((iIndexPos>-1) && (iIndexPos<N))
{
//clobbers the target records and moves everyone over one position to the left
for (int iLoop=iIndexPos; iLoop<N-1; iLoop++)
{
memcpy(&studentDB->A[iLoop],&studentDB->A[iLoop+1],STUDENT_REC_SIZE);
}
Student_Init(&studentDB->A[N-1]); //invalidates the last record
TStudent tempArray = (TStudent)realloc(studentDB->A,(N-1)*STUDENT_REC_SIZE);
if (tempArray!=NULL)
{
studentDB->A = tempArray;
tempArray = NULL;
studentDB->numStudentRecs--;
WriteStudentFile(studentDB);
}
}
}
DumpStudentRecs(TStudentDbase studentDB)
{
int N = studentDB->numStudentRecs;
for (int iLoop=0; iLoop<N; iLoop++)
{
char outbuff[STUDENT_REC_SIZE+1];
Student_SerializeToCSV(&studentDB->A[iLoop],outbuff);
printf("%s\n",outbuff);
}
}
int main()
{
struct _TStudentDbase studentDB;
ReadStudentFile(&studentDB);
int N = studentDB.numStudentRecs;
qsort(studentDB.A,N,STUDENT_REC_SIZE,CompareStudentByGrade);
DumpStudentRecs(&studentDB);
printf("**********************************\n");
qsort(studentDB.A,N,STUDENT_REC_SIZE,CompareStudentByName);
DumpStudentRecs(&studentDB);
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Jacob C.
Hi, Dorsa. The implementation of this depends on what kind of list you're using. It sounds like your assignment probably wants you to implement a linked list and a basic sorting function to sort by grade (such as quick sort). This is a larger assignment that would really take a tutoring session to explore. Shoot me a message if you want to set up a session to go over this implementation.09/01/20