write a code in c++ • Create a structure called Student with the following data
ID: 3717259 • Letter: W
Question
write a code in c++
• Create a structure called Student with the following data elements: (5 points) – name - string – house - string – year - integer – grades - array of doubles of size 5 – average - double
• Create a structure called Course with the following data elements: (5 points) – subject - string – year avg - array of doubles of size 7
• Open the file students.txt. The first line of this file is an integer that will tell you the number of students in the gradebook. Read in size from the file. (2 points) 1
• Create a dynamic array of Student structures with “size” number of elements. (5 points)
• Create an array of Course structures of size 5. This one should be local to main, and not dynamic. (4 points)
• Continue reading from students.txt into the array of Student structures. Each following line of the file will be of the format name * house * year Please make sure you watch out for the newlines while reading. (10 points)
• Open the file grades.txt. This file will have one less line than the student file. Each line of this file will have 5 doubles. The grades on line k will be the grades for the student on line k+1 of the student file. The grades will always be in the order - Charms Defense Herbology Potions Transfiguration (space separated) (3 points)
• Read data from the grade file into each student’s record. Calculate each student’s individual average and store it in the structure. (15 points)
• Calculate the count of students in each year. (6 points)
• Iterate through the array of students and use this data to calculate the average for each subject for each of the 7 years at Hogwarts. (12 points)
• Open a file called report.txt. Use the student array to print the name of the student and their grade average into this file. (10 points)
• Once all the student information has been printed, print the average of each subject by year into the file. (15 points)
• Delete the dynamic array of students. (3 points)
• You can use the sample files given here to test your code.
• You need not check for any errors. You can assume that the data in the files will be exactly as expected.
• Please include comments wherever appropriate. (5 points)
• Write a function called sort, that would take in 3 parameters - the array of Students, its size and an integer called choice. If the choice were odd, sort the students by name. If the choice were even, sort them by house. The order in the same house is immaterial. Call this function before printing to the file. You can pass in 1 or 0 for the third parameter to test the odd or even case. You can use the insertion sort algorithm given in Homework 5. If you do decide to complete this part, the first part (student name and average) of your output will be different from the above output. It will be the same values, ordered differently (50 points).
You need to use dynamic memory allocation. Allocating a regular array for the Student records will result in loss of points.
Please make sure that you’re conforming to specifications (program name, print statements, expected inputs and outputs etc.).
No global variables (variables outside of main() )
All input and output must be done with streams, using the library iostream
You may only use the iostream, fstream, and the cstring. string and cctype libraries (you do not need any others for these tasks)
NO C style printing is permitted. (Aka, dont use printf).
Transfiguration Year 1: 51 students.tx Herbology Year 1: 91 Charms Year 1: 52 Year 2: 63.25 Year 2: 80.5 Year 2: 50.5 Year 3: 47 Year 4: 58 Year 5: 60. Year 5: 89 Year 6: 36 Year 7: 84.5 Year 7: 33.5 Year 7: 64.5 grades.txt report.txt Bronwen Bennett 66.4 12 Bronen BennettHufflepuff3 52 51 91 31 51 Edward Kerley 55.2 Edward Kerley * Slytherin * 1 Linda Fawcett *Hufflepuff * 6 Reginald Shipuay * Gryffindor * 4 75 66 37 89.5 67Ann Trevithick 66.9 Ann Trevithick Ravenclaw*7 Michael Hennessey * Gryffindor * 2 32 73 31.5 85 35 Stephen Perkins 51.3 Stephen Perkins * Slytherin *Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <cctype>
using namespace std;
struct Student
{
string name, house;
int year;
double grades[5];
double average;
};
struct Course
{
string subject;
double year_avg[7];
};
int main()
{
int size;
Student *students;
//Course *course;
ifstream in, IN;
in.open("students.txt");
if (!in)
cout << "error opening students.txt file";
else
{
in >> size;
students = new Student[size];
for (int i = 0; i < size; i++)
{
char star;
in.ignore();
getline(in, students[i].name, '*');
getline(in, students[i].house, '*');
in >> students[i].year;
}
IN.open("grades.txt");
if (!IN)
cout << "error opening grades.txt file";
else
{
double avg = 0.0,vals;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < 5; j++)
{
IN >> vals;
students[i].grades[j] = vals;
avg += vals;
}
avg = avg / 5;
students[i].average = avg;
}
}
/* int count1=0, count2=0, count3=0, count4=0, count5=0, count6=0, count7=0;
for (int i = 0; i < size; i++)
{
if (students[i].year == 1)
count1++;
if (students[i].year == 2)
count2++;
if (students[i].year == 3)
count3++;
if (students[i].year == 4)
count4++;
if (students[i].year == 5)
count5++;
if (students[i].year == 6)
count6++;
if (students[i].year == 7)
count7++;
}
*/
int count[7]; //count array for each year
for(int i=0;i<7;i++)
{
count[i]=0; //initialize
}
for (int i = 0; i < size; i++)
{
if (students[i].year == 1)
count[0]++;
if (students[i].year == 2)
count[1]++;
if (students[i].year == 3)
count[2]++;
if (students[i].year == 4)
count[3]++;
if (students[i].year == 5)
count[4]++;
if (students[i].year == 6)
count[5]++;
if (students[i].year == 7)
count[6]++;
}
//set course names; set average by year =0
Course courselist[5]={{"Charms",0,0,0,0,0,0,0} ,{"Defense",0,0,0,0,0,0,0}, {"Herbology",0,0,0,0,0,0,0},{"Potions",0,0,0,0,0,0,0},{"Transfiguration",0,0,0,0,0,0,0}};
//Part 10:
//To calculate average of each subject by year
// Processing
for(int i=0;i<size;i++) //to access each student
{
for(int j=0;j<5;j++) // to access each year
{ if(students[i].year==1)
courselist[j].year_avg[0]+=students[i].grades[j];
else if(students[i].year==2)
courselist[j].year_avg[1]+=students[i].grades[j];
else if(students[i].year==3)
courselist[j].year_avg[2]+=students[i].grades[j];
else if(students[i].year==4)
courselist[j].year_avg[3]+=students[i].grades[j];
else if(students[i].year==5)
courselist[j].year_avg[4]+=students[i].grades[j];
else if(students[i].year==6)
courselist[j].year_avg[5]+=students[i].grades[j];
else if(students[i].year==7)
courselist[j].year_avg[6]+=students[i].grades[j];
}
}
for(int i=0;i<5;i++) //average value
{
for(int j=0;j<7;j++)
{
courselist[i].year_avg[j]/=count[j];
}
}
//part 12:
/* print the average of each subject by year into the file.*/
FILE* ofile=fopen("SubjectAverageByYear.txt","w");
{
// fprintf(ofile,"%s ",courselist[i].subject); // to print subject name
for(int j=0;j<7;j++) //year
{
fprintf(ofile,"Year %d : %.2f ",(j+1), courselist[i].year_avg[j]);
}
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.