Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using arrays of structure data type (C++) NO POINTS AWARDED FOR PARTIAL/INCOMPLE

ID: 3569065 • Letter: U

Question

Using arrays of structure data type (C++)

NO POINTS AWARDED FOR PARTIAL/INCOMPLETE ANSWER

DATA FILE: http://pastebin.com/3HAxsBXg

Problem: You have been asked by a friend of yours who is a TA for a history course to write a program to help him manage the grades for the course. Your friend can provide you with a file History101.dat (do not change the file name) that contains student IDs in the first column, the scores for exam 1 in the second column, the scores for exam 2 in the third column, and the scores for exam 3 in the fourth column. All the values for exam scores are whole numbers. The TA does not know how many students are in the class, just that it is less than 300, so your program will also need to count the number of students.

You should create a structure definition with 4 properties (attributes or fields) for a student. These properties are: a 4-digit ID, and integer array for the 3 exam scores, a total score, and a letter grade (may contain + or -). You will create an array of this structure data type and store all the information for each student as a separate element in this array. Your program must include and use the following functions. (You may create and use other functions of your choosing as long as your program utilizes the functions listed.)

Function to input information into an array of structure variables: this function should employ a loop to read the data of ID and 3 exam scores from the file, to calculate the total of the exam scores, and to assign the letter grade for the student by calling the function to determine a letter grade. This function should return the number of students in the class the function call.

Function to determine and return the index of the element with the maximum test score for a specified exam (exam 1, exam2 or exam 3). This function should accept the array of structure variables and the number corresponding to the specified exam (the number should be used to determine which column of the array is to be examined).

Function to output all the information for a single student to a file. (This function should accept as structure variable as input parameter.)

Function to output all the information of an array of students to a file. (This function should repeatedly call the function to output the information for a single structure variable.)

Function to determine and send back to the function call the letter grade based on the following schedule for total score. (This function should accept the total score as input)

            276 and above                      A

            270 to 275                            A-

            264 to 269                            B+

            246 to 263                            B

            240 to 245                            B-

            234 to 239                            C+

            210 to 233                            C

            180 to 209                            D

            Below 180                            F

Function to sort the students ascending by total score

Function to sort the students descending by total score.

Function to sort the students by ID (you may choose ascending or descending)

Function to count how many students earned each letter grade. These numbers should be sent back to the function call. Do not use an array or arrays to store these values.

In main, you should declare your array of structure variables and call the function to fill the array from the file. The user should be prompted to input the name of the file to be read either in main or in the function to fill the array. The main function should output the number of students in the class to the screen. Main should also contain a loop that will continue to run as long as the user wants to perform one of the following utilities you should employ a switch to: output to the screen all the information for the student who had the highest score in exam 1; output to the screen all the information for the student who had the highest score in exam 2; output to the screen all the information for the student who had the highest score in exam 3; sort the array by ID and output the result to a file; sort the array ascending by total score and output the result to a file; sort the array descending by total score and output the result to a file; determine how many students earned each grade and output these values to the screen; or quit. The output of students of information for students receiving the maximum score on a specified exam should be to the screen and should look similar to

        Student ____ had the high score of ___ on exam 1

and earned a ___ on exam 2 and ___ on exam 3.

Student ____ had the high score of ____ on exam 2

and earned a ___ on exam 1 and ____ on exam 3.

Student ____ had the high score of ____ on exam 3

and earned a ___ on exam 1 and ___ on exam 2

In your sorting functions you should interchange an entire structure variable at one time, not individual properties (attributes or fields). Also you should use each of the sorting algorithms (insertion, bubble, selection) once.

Carefully consider the design of all your functions. Do not pass more parameters than needed. Do not use reference parameters unnecessarily.

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string>
using namespace std;


struct student
{
char ID[4];
int exam[3];
  
int total;
char letterGrade[3];
};
int noOfStudents;

int getInput(struct student *students)
{
ifstream file;
file.open("History101.dat");
  
int i =0;
  
while(!file.eof())
{
file>>students[i].ID;
file>>students[i].exam[0];
file>>students[i].exam[1];
file>>students[i].exam[2];
  
students[i].total = students[i].exam[0] + students[i].exam[1] + students[i].exam[2] ;
  
if(students[i].total >= 276)
strcpy(students[i].letterGrade , "A");
else if(students[i].total >= 270)
strcpy(students[i].letterGrade , "A-");
else if(students[i].total >= 264)
strcpy(students[i].letterGrade , "B+");
else if(students[i].total >= 246)
strcpy(students[i].letterGrade , "B");
else if(students[i].total >= 240)
strcpy(students[i].letterGrade , "B-");
else if(students[i].total >= 234)
strcpy(students[i].letterGrade , "C+");
else if(students[i].total >= 210)
strcpy(students[i].letterGrade , "C");
else if(students[i].total >= 180)
strcpy(students[i].letterGrade , "D");
else
strcpy(students[i].letterGrade , "F");
  
i++;
}
  
  
return i;
}

int maxTestScore(struct student *students, int e)
{
int maxIndex = 0;
  
e=e-1;
  
for(int i=1; i<noOfStudents; i++)
{
if(students[maxIndex].exam[e] < students[i].exam[e])
maxIndex = i;
}
  
return maxIndex;
}

void storeSingleStudent(struct student s)
{
ofstream file;
file.open("output.txt");
  
file<<"Id:"<<s.ID<<" ";
file<<"Exam 1: "<<s.exam[0]<<" ";
file<<"Exam 2: "<<s.exam[1]<<"> ";
file<<"Exam 3: "<<s.exam[2]<<"> ";
file<<"Total: "<<s.total<<"> ";
file<<"Grade: "<<s.letterGrade<<"> ";
}

void storeAllStudents(struct student *students)
{
ofstream file;
file.open("output.txt");
  
for(int i=0; i<noOfStudents; i++)
{
file<<"Id:"<<students[i].ID<<" ";
file<<"Exam 1: "<<students[i].exam[0]<<" ";
file<<"Exam 2: "<<students[i].exam[1]<<"> ";
file<<"Exam 3: "<<students[i].exam[2]<<"> ";
file<<"Total: "<<students[i].total<<"> ";
file<<"Grade: "<<students[i].letterGrade<<"> ";
}
  
  
}

void sortById(struct student *students)
{
for (int i = 0 ; i < ( noOfStudents - 1 ); i++)
{
for (int j = 0 ; j < noOfStudents - i - 1; j++)
{
if (strcmp(students[j].ID , students[j+1].ID) == 1) /* For decreasing order use < */
{
struct student swap = students[j];
students[j] = students[j+1];
students[j+1] = swap;
}
}
}
  
storeAllStudents(students);
}

void sortByTotalAscending(struct student *students)
{
for (int i = 0 ; i < ( noOfStudents - 1 ); i++)
{
for (int j = 0 ; j < noOfStudents - i - 1; j++)
{
if (students[j].total > students[j+1].total) /* For decreasing order use < */
{
struct student swap = students[j];
students[j] = students[j+1];
students[j+1] = swap;
}
}
}
  
storeAllStudents(students);
}

void sortByTotalDescending(struct student *students)
{
for (int i = 0 ; i < ( noOfStudents - 1 ); i++)
{
for (int j = 0 ; j < noOfStudents - i - 1; j++)
{
if (students[j].total < students[j+1].total) /* For decreasing order use < */
{
struct student swap = students[j];
students[j] = students[j+1];
students[j+1] = swap;
}
}
}
  
storeAllStudents(students);
}

void detail(struct student *students)
{
int detail[9] = {0, 0, 0,0,0,0,0,0,0};
for(int i=0; i<noOfStudents; i++)
{
if(strcmp(students[i].letterGrade, "A") == 0)
detail[0]++;
  
if(strcmp(students[i].letterGrade, "A-") == 0)
detail[1]++;
  
if(strcmp(students[i].letterGrade, "B+") == 0)
detail[2]++;
  
if(strcmp(students[i].letterGrade, "B") == 0)
detail[3]++;
  
if(strcmp(students[i].letterGrade, "B-") == 0)
detail[4]++;
  
if(strcmp(students[i].letterGrade, "C+") == 0)
detail[5]++;
  
if(strcmp(students[i].letterGrade, "C") == 0)
detail[6]++;
  
if(strcmp(students[i].letterGrade, "D") == 0)
detail[7]++;
  
if(strcmp(students[i].letterGrade, "F") == 0)
detail[8]++;
}
  
cout<<" Student grade detail: ";
  
cout<<"A grade:"<<detail[0]<<" ";
cout<<"A- grade:"<<detail[1]<<" ";
cout<<"B+ grade:"<<detail[2]<<" ";
cout<<"B grade:"<<detail[3]<<" ";
cout<<"B- grade:"<<detail[4]<<" ";
cout<<"C+ grade:"<<detail[5]<<" ";
cout<<"C grade:"<<detail[6]<<" ";
cout<<"D grade:"<<detail[7]<<" ";
cout<<"F grade:"<<detail[8]<<" ";
}
int main(int argc, char** argv) {

struct student *students = new student[300];
  
  
noOfStudents = getInput(students);
  
cout<<"Number of students: "<<noOfStudents<<" ";
  
  
while(true)
{
int choice;
cout<<" ";
cout<<"1. Show highest score in exam 1 ";
cout<<"2. Show highest score in exam 2 ";
cout<<"3. Show highest score in exam 3 ";
cout<<"4. Sort by Id and store in output.txt ";
cout<<"5. Sort by total in ascending order and store in output.txt ";
cout<<"6. Sort by total in descending order and store in output.txt ";
cout<<"7. Detail of number of students with similar grades ";
cout<<"Enter your choice:";
  
cin>>choice;
cout<<" ";
  
switch(choice){
case 1:
cout<<"Student "<<students[maxTestScore(students, 1)].ID<<"had the high score of ";
cout<<students[maxTestScore(students, 1)].exam[0]<<" on exam 1 ";
cout<<"and earned a "<<students[maxTestScore(students, 1)].exam[1]<<" on exam 2 and ";
cout<<students[maxTestScore(students, 1)].exam[2]<<" on exam 3. ";
break;
  
case 2:
cout<<"Student "<<students[maxTestScore(students, 2)].ID<<"had the high score of ";
cout<<students[maxTestScore(students, 2)].exam[1]<<" on exam 2 ";
cout<<"and earned a "<<students[maxTestScore(students, 2)].exam[0]<<" on exam 1 and ";
cout<<students[maxTestScore(students, 2)].exam[2]<<" on exam 3. ";
break;
  
case 3:
cout<<"Student "<<students[maxTestScore(students, 1)].ID<<"had the high score of ";
cout<<students[maxTestScore(students, 1)].exam[2]<<" on exam 3 ";
cout<<"and earned a "<<students[maxTestScore(students, 1)].exam[0]<<" on exam 1 and ";
cout<<students[maxTestScore(students, 1)].exam[1]<<" on exam 2. ";
break;
  
case 4:
sortById(students);
break;
  
case 5:
sortByTotalAscending(students);
break;
  
case 6:
sortByTotalDescending(students);
break;
  
case 7:
detail(students);
break;
  
case 8:
return 0;
  
default:
cout<<" ";
break;
}
}
  
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote