Design a program using random file for 10 students, the record for each student
ID: 3765941 • Letter: D
Question
Design a program using random file for 10 students, the record for each student must has the next structure: record_Number, student_Name 15 characters, first_Grade, second _Grade and average_Grade. The numeric grades must be from 1.0 to 10.0. Data will be store in student.dat
To all Students develop functions: CreateFile, Menu, Capture_Data and Print_Records
I already created CreateFile, Menu, Capture_Data and Print_Records.
My question is:
How can sort record based in Last Name from Z to A or from A to Z and print them. I created a function void sortName(); Thank you. .
record_Number
student_Name
first_ Grade
second _Grade
average_Grade
1
Frederick
8.0
8.5
2
Chase
9.5
8.5
3
Melissa
10.0
9.0
4
David
7.5
9.0
5
Ryan
9.5
8.5
6
Erick
10.0
10.0
7
Joshua
8.0
9.5
8
Marc
9.5
10.0
9
Sadeer
8.5
9.5
10
Karen
10
8.0
See code below
// Exam3AddFunction.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
struct studentData
{
int record_Number;
char student_Name[15];
float first_Grade;
float second_Grade;
float average_Grade;
}; // end structure
unsigned int enterChoice(void);
void captureData();
void printAll();
void sortName();
//void sortStruct(Student *s, unsigned int order);
int main(void)
{
int choice= enterChoice();
if(choice == 1)
captureData();
else if(choice == 2)
printAll();
return 0;
}
unsigned int enterChoice(void){
int choice = 0;
printf("Enter your choice ");
printf("1 - Capture Data ");
printf("2 - Print all record ");
printf("3 - Calculate Average Grades ");
printf("4 - Consulting Monthly Grades from a Range ");
printf("5 - Sort Record Based on Name from Z to A or A to Z ");
printf("6 - Update Any Record, Allowing Changes in Grades ");
printf("7 - Insert New Information or Delete ");
printf("8 - Exchange Information from One Record with Another Record ");
printf("9 - End Program ?");
scanf_s("%d", &choice);
return choice;
}
void captureData(){
FILE *cfPtr;
struct studentData student = { 0, "", 0.0, 0.0, 0.0 };
if ((cfPtr = fopen("e:\student.dat", "rb+")) == NULL)
{
printf(" File could not be opened ");
} // end if
else
{
printf("Enter a record number from 1 to 10, 0 to end ?");
scanf("%d", &student.record_Number);
while (student.record_Number != 0)
{
printf("Enter Name, first grade, second grade, average grade ?");
fscanf(stdin, "%s%f%f", student.student_Name, &student.first_Grade, &student.second_Grade, &student.average_Grade);
// seek position in random file
fseek(cfPtr, (student.record_Number - 1)*sizeof(struct studentData), SEEK_SET);
// write random file <- MOST IMPORTANT WRITE STRUCTURE
fwrite(&student, sizeof(struct studentData), 1, cfPtr);
// enables user to input another account
printf("Enter record number <0 to exit> ?");
scanf("%d", &student.record_Number);
} // end while
fclose(cfPtr);
} // end else
}// end function Capture_Data
void printAll(){
struct studentData student = { 0, "", 0.0, 0.0, 0.0 }; //blank info for each client
FILE *cfPtr; //pointer to the file
if((cfPtr = fopen("e:\student.dat", "rb")) == NULL){ //rb+ is for read binary file
printf("FILE could not be opened.. ");
}
else{
printf("%-10s %-10s %-10s %-10s %-10s ","RecNum","Name", "FirstGrade","SecondGrade","AverageGrade");
while(!feof(cfPtr)){ //read all data
if (!fread(&student, sizeof(struct studentData),1,cfPtr)){
break;
}
if(student.record_Number != 0){ //Display record if account number != 0
printf("%-10d %-10s %-10.2f %-10.2f %-10.2f ",student.record_Number, student.student_Name, student.first_Grade, student.second_Grade, student.average_Grade);
}
}//end of while
fclose(cfPtr);
}//end of if else
}
record_Number
student_Name
first_ Grade
second _Grade
average_Grade
1
Frederick
8.0
8.5
2
Chase
9.5
8.5
3
Melissa
10.0
9.0
4
David
7.5
9.0
5
Ryan
9.5
8.5
6
Erick
10.0
10.0
7
Joshua
8.0
9.5
8
Marc
9.5
10.0
9
Sadeer
8.5
9.5
10
Karen
10
8.0
Explanation / Answer
Program:
// Srudentrecord.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
struct studentData
{
int record_Number;
char student_Name[15];
float first_Grade;
float second_Grade;
float average_Grade;
}; // end structure
unsigned int enterChoice(void);
void captureData();
void printAll();
void sortName(struct studentData *student);
int main()
{
studentData *student=new studentData[10];
int choice= enterChoice();
if(choice == 1)
captureData();
else if(choice == 2)
printAll();
return 0;
}
unsigned int enterChoice()
{
int choice = 0;
printf("Enter your choice ");
printf("1 - Capture Data ");
printf("2 - Print all record ");
printf("3 - Calculate Average Grades ");
printf("4 - Consulting Monthly Grades from a Range ");
printf("5 - Sort Record Based on Name from Z to A or A to Z ");
printf("6 - Update Any Record, Allowing Changes in Grades ");
printf("7 - Insert New Information or Delete ");
printf("8 - Exchange Information from One Record with Another Record ");
printf("9 - End Program ");
scanf_s("%d", &choice);
return choice;
}
void captureData()
{
FILE *cfPtr;
struct studentData student = { 0, "", 0.0, 0.0, 0.0 };
if ((cfPtr = fopen("student.dat", "rb+")) == NULL)
{
printf(" File could not be opened ");
} // end if
else
{
printf("Enter a record number from 1 to 10, 0 to end ");
scanf("%d", &student.record_Number);
while (student.record_Number != 0)
{
printf("Enter Name, first grade, second grade, average grade ");
fscanf(stdin, "%s %f %f", student.student_Name, &student.first_Grade, &student.second_Grade, &student.average_Grade);
// seek position in random file
fseek(cfPtr, (student.record_Number - 1)*sizeof(struct studentData), SEEK_SET);
// write random file <- MOST IMPORTANT WRITE STRUCTURE
fwrite(&student, sizeof(struct studentData), 1, cfPtr);
// enables user to input another account
printf("Enter record number <0 to exit> ");
scanf("%d", &student.record_Number);
} // end while
fclose(cfPtr);
} // end else
}// end function Capture_Data
void printAll(){
struct studentData student = { 0, "", 0.0, 0.0, 0.0 }; //blank info for each client
FILE *cfPtr; //pointer to the file
if((cfPtr = fopen("student.dat", "rb+")) == NULL){ //rb+ is for read binary file
printf("FILE could not be opened.. ");
}
else{
printf("%-10s %-10s %-10s %-10s %-10s ","RecNum","Name", "FirstGrade","SecondGrade","AverageGrade");
while(!feof(cfPtr)){ //read all data
if (!fread(&student, sizeof(struct studentData),1,cfPtr)){
break;
}
if(student.record_Number != 0){ //Display record if account number != 0
printf("%-10d %-10s %-10.2f %-10.2f %-10.2f ",student.record_Number, student.student_Name, student.first_Grade, student.second_Grade, student.average_Grade);
}
}//end of while
fclose(cfPtr);
}//end of if else
}
void sortName(struct studentData *student)
{
studentData temp;
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(strcmp(student[i].student_Name,student[j].student_Name)<0)
{
temp=student[i];
student[i]=student[j];
student[j]=temp;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.