I need a modified version of the following code with the following function PROT
ID: 3547489 • Letter: I
Question
I need a modified version of the following code
with the following function PROTOYPES:
void ReadStudentData(ifstream& infile, StudentType*& student, int& numOfStudents);
StudentType* SortStudentsByName(const StudentType* student, int size);
StudentType* SortStudentsByScore(const StudentType* student, int size);
iam changing the existing prototypes with the same name to the 3 i just mentioned
The program runs great i just need to add these 3 functions and remove the old 3 functions with the same name
main program
1.Remove fixed-sized array
2. add a loop so that a user can open the students file several times, right now you can only do it once.
remember to deallocate memory at end of loop.If a file fails to open
you will need to handle this properly so that the program will allow the user to continue
the program and open another file.
3. . Adjust your code to dynamically allocate the array used by
the program. Revise your variable declarations and your function arguments to handle
the pointer to the dynamic array. Test your program on an updated input file (i.e.: the
original data file had 23 students, 23 or a number ? 23 may now appear on the first line
of the file so that all the students with valid scores get into the course).
4. In your ReadStudentData() function, you will first read the file
Explanation / Answer
/*
Here is an example of the input file:
5
ff1 ll1 16
ff2 ll2 64
ff3 ll3 99
The max size of the dynamic array is 5, total 3 students
Note, In the SortStudentsXXX methods, you cannot use const StudentType* student,
because we need to swap the students in the array, so must remove the keyword "const".
In the memory allocation, I use the extra credit: use try catch bad_alloc when memory allocate is bad.
*/
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include <new> // std::bad_alloc
using namespace std;
struct StudentType
{
string studentName;
int testScore;//Between 0 and 100
char grade;
};
void PrintNameHeader(ostream& out);
bool OpenInputFile(ifstream& inFile, string& infilename ); //OPEN input file
void Pause();// Pause
void ReadStudentData(ifstream& infile, StudentType*& student, int& numOfStudents);// Read student info including first and last name and test score
void AssignGrades(StudentType student[], int);//assign grades to each student
int HighestScore(const StudentType student[], int );//Get the highest scores
void PrintNamesWithHighestScore(const StudentType student[], int);//Print name/s with highest Scores
void DisplayAllStudents(const StudentType student[], int);//Display all students
void GetLowHighRangeValues(const StudentType student[], int, int& , int&);//for example a student types 50 100 , it will get all students within that range
void DisplayStudentsInRange(const StudentType student[], int, int, int);// display students in that range
StudentType* SortStudentsByName(StudentType* student, int size);// sort students by name
StudentType* SortStudentsByScore(StudentType* student, int size);// sort students by test score highest to lowest
void FormatNameScoreGrade(ostream& out);
void EndOfList(ostream& out);
const int NUM_STUDENTS = 20;
int main()
{
ifstream infile;
string inFilename;
StudentType* student;
int numStudents = 0,
lowRange,
highRange;
char answer;
PrintNameHeader(cout);
do {
bool hasData = false;
while (!hasData) {
if (!OpenInputFile(infile,inFilename)) {
cout << "Try again... " << endl;
}
else {
ReadStudentData(infile, student, numStudents);
if (student == NULL) {
cout << "Try again... " << endl;
infile.close();
}
else {
hasData = true;
}
}
}
infile.close();
AssignGrades(student, numStudents);
cout <<"List of All the Students in the class" << endl;
DisplayAllStudents(student, numStudents);
cout <<"There are " << numStudents <<" students in this class." << endl;
PrintNamesWithHighestScore(student, numStudents);
Pause();//Pause
do {
GetLowHighRangeValues(student, numStudents, lowRange, highRange);
DisplayStudentsInRange(student, numStudents, lowRange, highRange);
cout <<"Do you want to see another range of scores [y/n]?";
cin >> answer;
} while( answer == 'y' || answer == 'Y');
SortStudentsByName(student, numStudents);
Pause();//Pause
SortStudentsByScore(student, numStudents);
// delete memory
delete[] student;
cout << endl;
cout <<"Do you want to open another file [y/n]?";
cin >> answer;
} while (answer == 'y' || answer == 'Y');
return 0;
}
//Function definitions
void PrintNameHeader(ostream& out)
{
//Display name header on screen
}
void FormatNameScoreGrade(ostream& out)
{
out << setw(10) << " Student Name" << setw(20) <<"Test Score" << setw(10) <<"Grade" << endl;
out <<" ------------------------------------------" << endl;
}
void EndOfList(ostream & out)
{
cout <<" " <<"End of List." << endl << endl;
}
bool OpenInputFile(ifstream& inFile, string& infilename)
{
cout << "Enter the name of the .txt file that you want to open for input. ";
cout << "Do not put spaces in the file name : ";
cin >> infilename;
cout << endl;
inFile.open(infilename.c_str());
if (inFile.fail())
{
inFile.clear();
cout << "Sorry, the input file " << infilename <<" was not found"<< endl;
return false;
}
cout << "Input file " << infilename << " is open for reading. ";
return true;
}
void Pause()
{
cout << endl;
cin.ignore(80, ' ');
cout<<"Please hit the enter key to continue... ";
cin.get();
}
void ReadStudentData(ifstream& infile, StudentType*& student, int& numOfStudents)
{
string firstName, lastName;
int testScore;
int count = 0;
infile >> numOfStudents;
try
{
student = new StudentType[numOfStudents];
while(count < NUM_STUDENTS && infile >> firstName >> lastName >> testScore)
{
if( testScore >= 0 && testScore <=100)
{
student[count].studentName = lastName + ", " + firstName;
student[count].testScore = testScore;
count++;
}
}
numOfStudents = count;
} catch (bad_alloc& ba)
{
cout << "Failed to allocate memory" << endl;
numOfStudents = 0;
student = NULL;
}
}
void AssignGrades(StudentType student[], int numStudents)
{
int testscore;
char grade;
for(int i = 0; i < numStudents; i++)
{
testscore = student[i].testScore;
if(testscore >= 90 && testscore <=100)
grade = 'A';
else if(testscore >= 80 && testscore <90)
grade = 'B';
else if(testscore >= 70 && testscore <80)
grade = 'C';
else if(testscore >= 60 && testscore <70)
grade = 'D';
else
grade = 'F';
student[i].grade = grade;
}
}
int HighestScore(const StudentType student[], int numStudents)
{
int highest = student[0].testScore;
for(int i = 1; i < numStudents; i++)
{
if(highest < student[i].testScore) highest = student[i].testScore;
}
return highest;
}
void PrintNamesWithHighestScore(const StudentType student[], int numStudents)
{
int highest = HighestScore(student, numStudents);
cout << endl << "The following student(s) have the highest score" << endl << endl;
FormatNameScoreGrade(cout);
for(int i = 0; i < numStudents; i++)
{
if (student[i].testScore == highest)
{
cout << setw(20) << student[i].studentName << setw(10) << student[i].testScore << setw(10) << student[i].grade
<< endl;
}
}
cout << endl;
EndOfList(cout);
}
void DisplayAllStudents(const StudentType student[], int numStudents)
{
cout << endl;
FormatNameScoreGrade(cout);
for(int i = 0; i < numStudents; i++)
{
cout << setw(20) << student[i].studentName << setw(10) << student[i].testScore << setw(10) << student[i].grade << endl;
}
cout << endl;
EndOfList(cout);
}
void GetLowHighRangeValues(const StudentType student[], int numStudents, int& lowRange, int& highRange)
{
cout <<"Enter two values from 0-100 for the range of test scores you want to view: ";
cin >> lowRange >> highRange;
while ( (lowRange < 0 )|| (highRange < 0) || (lowRange > highRange) || (lowRange > 100) || (highRange > 100) )
{
cout <<"Please re-enter 2 values of test scores that you want to view from ";
cout <<"within the range 0-100: ";
cin >> lowRange >> highRange;
cout << endl;
}
}
void DisplayStudentsInRange(const StudentType student[], int numStudents, int lowRange, int highRange)
{
cout << endl << "List of students with scores in the range " << lowRange << " to " << highRange << endl << endl;
FormatNameScoreGrade(cout);
for(int i = 0; i < numStudents; i++)
{
if(student[i].testScore >= lowRange && student[i].testScore <= highRange)
{
cout << setw(20) << student[i].studentName << setw(10) << student[i].testScore << setw(10) << student[i].grade << endl;
}
}
cout << endl;
EndOfList(cout);
}
StudentType* SortStudentsByName(StudentType* student, int numStudents)
{
int startScan,
minIndex;
for (startScan = 0; startScan < (numStudents-1); startScan++)
{
minIndex = startScan;
for ( int index = startScan; index < numStudents; index++)
{
if( student[index].studentName < student[minIndex].studentName)
minIndex = index;
}
if(minIndex!=startScan)
{
StudentType temp = student[minIndex];
student[minIndex] = student[startScan];
student[startScan] = temp;
}
}
cout << endl;
cout << "List of Students sorted Alphabetically "<< endl;
DisplayAllStudents(student, numStudents);
return student;
}
StudentType* SortStudentsByScore(StudentType* student, int numStudents)
{
int startScan,
minIndex;
for (startScan = 0; startScan < (numStudents-1); startScan++)
{
minIndex = startScan;
for ( int index = startScan; index < numStudents; index++)
{
if( student[index].testScore>student[minIndex].testScore)
minIndex = index;
}
if(minIndex!=startScan)
{
StudentType temp = student[minIndex];
student[minIndex] = student[startScan];
student[startScan] = temp;
}
}
cout <<"List of Students sorted by Score from Highest to Lowest" << endl;
DisplayAllStudents(student, numStudents);
return student;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.