In C please Design a program using random file for 10 students, the record for e
ID: 3766007 • Letter: I
Question
In C please
Design a program using random file for 10 students, the record for each student must have 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
Develop functions: CreateFile, Menu,
a) Capture_Data
b) Print_Records
c) Calculate average_Grade for all students and print them.
d) Consulting Monthly Grades from a range, example from 8 to 9, and print them.
e) Search by Last Name or FistName and print the record found
f) Sort record based in Last Name from Z to A or from A to Z and print them.
This is the code to store the data
#include "stdafx.h"
#include <stdio.h>
struct studentData {
unsigned int record_Number; // record number
char student_Name[15]; // student name
double first_Grade; //first grade
double second_Grade; // second grade
double average_Grade; // average grade
}; // end struct studentData
int main(void)
{
int i;
struct studentData blankStudent = { 0,"",0,0,0 };
FILE*cfPtr;
if ((cfPtr = fopen("f:\student.dat", "wb")) == NULL) {
puts("File could not be opened.");
} // end if
else {
for (i = 1; i <= 10; i++) {
fwrite(&blankStudent, sizeof(struct studentData), 1, cfPtr);
}
fclose(cfPtr);
} // end if else
printf("Random file created 10 blank records.. ");
return 0;
}
I need a program to capture and read the data and a menu similar to this but only up to 5
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.0 8.0Explanation / Answer
Answer:
The program works on the random access file where, it is tested only for the 5 data values.
Please be careful while entering the names, the first letter to be in capital letter.
The sorting is done in ascending order by name.
Program code to copy:
// StudentDataClass.c
#include <stdio.h>
using namespace std;
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(struct studentData *stud, int &count);
void printAll();
void sortName(struct studentData *student, int count);
void calculateAverage(struct studentData *student, int count);
void calculateMonthlyGrades(struct studentData *student, int count);
void print(struct studentData *student, int i);
void writeData(struct studentData *student, int count);
int main()
{
studentData *student=new studentData[10];
int choice =0;
int count=0;
do
{
choice = enterChoice();
if(choice == 1)
captureData(student, count);
else if(choice == 2)
printAll();
else if(choice ==3)
calculateAverage(student, count);
else if(choice == 4)
calculateMonthlyGrades(student, count);
else if(choice == 5)
sortName(student, count);
else if(choice == 9)
{
return 0;
}
}while(choice !=0);
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 ");
printf("Enter your choice: ");
scanf_s("%d", &choice);
return choice;
}
void captureData(struct studentData *stud, int &count)
{
struct studentData student = { 0, "", 0.0, 0.0, 0.0 };
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 ");
fscanf(stdin, "%s %f %f", student.student_Name, &student.first_Grade, &student.second_Grade);
stud[count] = student;
count++;
// enables user to input another account
printf("Enter record number <0 to exit> ");
scanf("%d", &student.record_Number);
} // end while
writeData(stud, count);
}// end function Capture_Data
void writeData(struct studentData *student, int count)
{
FILE *cfPtr;
cfPtr = fopen("student.dat", "rb+");
struct studentData stud = { 0, "", 0.0, 0.0, 0.0 };
if(cfPtr ==NULL)
{
printf(" File could not be opened ");
}
else
{
for(int i=0;i<count; i++)
{
stud = student[i];
// seek position in random file
fseek(cfPtr, (stud.record_Number - 1)*sizeof(struct studentData), SEEK_SET);
// write random file <- MOST IMPORTANT WRITE STRUCTURE
fwrite(&stud, sizeof(struct studentData), 1, cfPtr);
}
}
fclose(cfPtr);
}
void printAll()
{
struct studentData student = { 0, "", 0.0, 0.0, 0.0 }; //bank inf each client
FILE *cfPtr; //pointer to the file
cfPtr = fopen("student.dat", "rb+");
if(cfPtr == 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 calculateAverage(struct studentData *student, int count)
{
double total=0;
double average=0;
printf("The value is: %s ",student[0].student_Name);
for(int i=0;i<count;i++)
{
total = student[i].first_Grade+student[i].second_Grade;
average = total/2;
student[i].average_Grade=average;
}
printf(" After calculating the average values, the data in the student structure is: ");
for(int i=0;i<count;i++)
print(student, i);
printf(" ");
writeData(student, count);
}
void calculateMonthlyGrades(struct studentData *student, int count)
{
double min, max;
printf(" Enter minimum range: ");
scanf("%lf", &min);
printf("Enter maximum range: ");
scanf("%lf", &max);
for(int i=0;i<count;i++)
{
if(student[i].average_Grade>=min && student[i].average_Grade<=max )
print(student, i);
}
printf(" ");
}
void print(struct studentData *student, int i)
{
printf("%-10d %-10s %-10.2f %-10.2f %-10.2f ",student[i].record_Number, student[i].student_Name, student[i].first_Grade, student[i].second_Grade, student[i].average_Grade);
}
void sortName(struct studentData *student, int count)
{
studentData temp;
for(int i=0;i<count;i++)
{
for(int j=0;j<count;j++)
{
if(strcmp(student[i].student_Name,student[j].student_Name)<0)
{
temp=student[i];
student[i]=student[j];
student[j]=temp;
}
}
}
for(int i=0;i<count;i++)
print(student, i);
printf(" ");
writeData(student, count);
}
Sample Output:
Enter your choice
1 - Capture Data
2 - Print all record
3 - Calculate Average Grades
4 - Consulting Monthly Grades from a Range
5 - Sort Record Based on Name from Z to A or A to Z
6 - Update Any Record, Allowing Changes in Grades
7 - Insert New Information or Delete
8 - Exchange Information from One Record with Another Record
9 - End Program
Enter your choice: 1
Enter a record number from 1 to 10, 0 to end
1
Enter Name, first grade, second grade
Frederick 8.0 8.5
Enter record number <0 to exit>
2
Enter Name, first grade, second grade
Chase 9.5 8.5
Enter record number <0 to exit>
3
Enter Name, first grade, second grade
Melissa 10.0 9.0
Enter record number <0 to exit>
4
Enter Name, first grade, second grade
david 7.5 9.0
Enter record number <0 to exit>
5
Enter Name, first grade, second grade
Ryan 9.5 8.5
Enter record number <0 to exit>
0
Enter your choice
1 - Capture Data
2 - Print all record
3 - Calculate Average Grades
4 - Consulting Monthly Grades from a Range
5 - Sort Record Based on Name from Z to A or A to Z
6 - Update Any Record, Allowing Changes in Grades
7 - Insert New Information or Delete
8 - Exchange Information from One Record with Another Record
9 - End Program
Enter your choice: 2
RecNum Name FirstGrade SecondGrade AverageGrade
1 Frederick 8.00 8.50 0.00
2 Chase 9.50 8.50 0.00
3 Melissa 10.00 9.00 0.00
4 david 7.50 9.00 0.00
5 Ryan 9.50 8.50 0.00
Enter your choice
1 - Capture Data
2 - Print all record
3 - Calculate Average Grades
4 - Consulting Monthly Grades from a Range
5 - Sort Record Based on Name from Z to A or A to Z
6 - Update Any Record, Allowing Changes in Grades
7 - Insert New Information or Delete
8 - Exchange Information from One Record with Another Record
9 - End Program
Enter your choice: 3
The value is: Frederick
After calculating the average values, the data in the student structure is:
1 Frederick 8.00 8.50 8.25
2 Chase 9.50 8.50 9.00
3 Melissa 10.00 9.00 9.50
4 david 7.50 9.00 8.25
5 Ryan 9.50 8.50 9.00
Enter your choice
1 - Capture Data
2 - Print all record
3 - Calculate Average Grades
4 - Consulting Monthly Grades from a Range
5 - Sort Record Based on Name from Z to A or A to Z
6 - Update Any Record, Allowing Changes in Grades
7 - Insert New Information or Delete
8 - Exchange Information from One Record with Another Record
9 - End Program
Enter your choice: 4
Enter minimum range: 8
Enter maximum range: 9
1 Frederick 8.00 8.50 8.25
2 Chase 9.50 8.50 9.00
4 david 7.50 9.00 8.25
5 Ryan 9.50 8.50 9.00
Enter your choice
1 - Capture Data
2 - Print all record
3 - Calculate Average Grades
4 - Consulting Monthly Grades from a Range
5 - Sort Record Based on Name from Z to A or A to Z
6 - Update Any Record, Allowing Changes in Grades
7 - Insert New Information or Delete
8 - Exchange Information from One Record with Another Record
9 - End Program
Enter your choice: 5
2 Chase 9.50 8.50 9.00
1 Frederick 8.00 8.50 8.25
3 Melissa 10.00 9.00 9.50
5 Ryan 9.50 8.50 9.00
4 david 7.50 9.00 8.25
Enter your choice
1 - Capture Data
2 - Print all record
3 - Calculate Average Grades
4 - Consulting Monthly Grades from a Range
5 - Sort Record Based on Name from Z to A or A to Z
6 - Update Any Record, Allowing Changes in Grades
7 - Insert New Information or Delete
8 - Exchange Information from One Record with Another Record
9 - End Program
Enter your choice: