Write a program which makes a grade book using file i/0.Your program Part 1 (1)
ID: 3535011 • Letter: W
Question
Write a program which makes a grade book using file i/0.Your program
Part 1
(1) Reads from an input file using file i/o 20 names(can use first names but make them distinct) and the corresponding grades(grades are between 0 and 100- make several with the same grade).
(2) finds the average
(3) finds the mode, the most common grade- use a frequency array.
(4) sorts the names alphabetically using a link list implemented via classes. Each node in the list should have the name, the grade, and a pointer to another node. You will need string comparisons. A selection sort might be the easiest.
(5) Sorts the grades numerically and prints the names and corresponding grade
Divide up the parts of the assignment into functions.
Part 2
1)Repeat part 1 but rather than using linked lists for 4 and 5, use 2 arrays- one for names and one for grades and the sorting should be done using a merge sort.
Explanation / Answer
#include<iostream.h>
#include<fstream.h>
struct hi // structure that store name and grade of a student
{
char name[30];
float grade;
hi *next;
};
void main()
{
hi mem,*ptr,*p,*temp; // pointer to store student info
fstream file;
file.open("data.txt"); //open file dat contain data file
float total=0,avg,max,,a,b,c,i=0,arr[20];
p=start;
while(file!=eof)//file will be read at the end of the file....
{
mem-file.read((*ch)&mem,sizeof(mem)); // reading from is done and size of mem is extracted from file
cout<<"Name "<<mem.name<<" grade="<<mem.grade";
total=total+grade; // total of grade is performed
i++;
arr[i]=mem.grade;
ptr=new mem;//allocation of new member
ptr=mem;
p->next=ptr;//dynamic allocation
}
avg=total/(i+1);//find avg
//sorting
p=start;
while(p->next!=null)
{
ptr=ptr->next;
if(p->grade>ptr->grade)//swaping of grade or sorting of grade
{
temp=p;
p=ptr;
ptr=temp;
}
p=p->next;
}
p=start;
//print name
while(p->next!=null) //print grade....
{
cout<<"Name is "<<p->name<<" Grade is "<<p->grade;
}
}
2.
struct hi // store data of student
{
char name[30];
float grade;
};
void main()
{
hi mem;
fstream file;
file.open("data.txt"); //open file
float i=0,arr[20];
char name[20[30];
p=start;
while(file!=eof) //file is read till the end of file
{
mem-file.read((*ch)&mem,sizeof(mem));
cout<<"Name "<<mem.name<<" grade="<<mem.grade";
total=total+grade;
i++;
arr[i]=mem.grade;
name[i]=mem.name;
}
//merge sort
int temp;
char[30];
for(i=0;i<20;i++)
{
if(arr[i]>arr[i+1])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
t=name[i];
name[i]=name[i+1];
name[i+1]=t;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.