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

Can someone pls help ,I need 2 functions for highest and smallest score in class

ID: 3572859 • Letter: C

Question

Can someone pls help ,I need 2 functions for highest and smallest score in class,and number of students for this program.

#include <string.h>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int const MAXSIZE = 100; // maximum number of records in total
int const MAXLENGTH = 31; // maximum string length
int const MAXGRADE = 100; // highest possible grade
int const LOWGRADE = 0; // lowest possible grade
int const GROUP = 10; // group amount
int const HISTOGRAMSIZE = (MAXGRADE - LOWGRADE) / GROUP + 1;// grouped by GROUP

struct StudentType { // information of one student
   int grade; // the grade of the student
   char last[MAXLENGTH]; // last name (MAXLENGTH-1 at most)
   char first[MAXLENGTH]; // first name (MAXLENGTH-1 at most)
};

// prototypes go here

bool sortInput(ifstream& argFile, StudentType studentsList[], int &argSize);
void displayList(const StudentType sortedList[], const int size);
int* setHistogram(int histogram[], const StudentType sortedList[],
                                                       const int sortedListSize);
void displayHistogram(const int histogram[]);
int findAverage(const StudentType sortedList[], const int size);
void sortInputSelectionSort(int& argSize, StudentType studentsList[],
                                                       StudentType tempStudent);
//-----------------------------------------------------------------------------
int main() {
   StudentType students[MAXSIZE]; // list of MAXSIZE number of students
   int size = 0; // total number of students
   int histogram[HISTOGRAMSIZE]; // grades grouped by GROUP
   int average = 0; // average exam score, truncated

   // creates file object and opens the data file
   ifstream infile("data1.txt");
   if (!infile) {
       cout << "File could not be opened." << endl;
       return 1;
   }
  
   // read and sort input by last then first name
   bool successfulRead = sortInput(infile, students, size);
   infile.close(); // Close the file stream

   // display list, histogram, and class average
   if (successfulRead) {
       displayList(students, size); //Display the sorted student list to console
       //assign values to the histogram array
       setHistogram(histogram, students, size);
       displayHistogram(histogram); //Display the histogram to console
       //Compute the average of the grades
       average = findAverage(students, size);
       cout << "Average grade: " << average << endl << endl;
   }
  
   return 0;
}
bool sortInput(ifstream& argFile, StudentType studentsList[], int &argSize) {
   //returns true if the file to be read from has been opened
   if (argFile.is_open()) {
       StudentType tempStudent; //Current StudentType student read from the file
      
       while (argFile >> tempStudent.last >> tempStudent.first >>
                                                               tempStudent.grade) {
           if (argSize == 0) {
               //Loop through file until first valid studentType is found
               for (;;) {
                   //Check for invalid grade; if false, skips over the current
                   //tempStudent to be overridden by the next.
                   if ((tempStudent.grade >= LOWGRADE &&
                               tempStudent.grade <= MAXGRADE)) {
                       studentsList[0] = tempStudent;
                       argSize++;
                       break;
                   }
                   argFile >> tempStudent.last >> tempStudent.first >>
                                                                   tempStudent.grade;
               }
           }
           else if (argSize < MAXSIZE) {// While array is still in bounds
               /*arrCounter represents the currently pointed index in
               studentsList while running the selection sort */
               int arrCounter = argSize - 1;
               if ((tempStudent.grade >= LOWGRADE &&
                                               tempStudent.grade <= MAXGRADE)) {
                   /*Place the current tempStudent into its sorted position
                   in studentsList*/
                   sortInputSelectionSort(argSize, studentsList, tempStudent);
               }
           }
           else {
               cout << "Reached Max Size of Array" << endl;
               break;
           }
       }
       return true;
   }
   else { return false; }
}

// ----------------------------------------------------------------------------
//displayList
//Prints the contents of the sorted StudentType list
void displayList(const StudentType sortedList[], const int size) {
   cout << "List of names sorted:" << endl;
   for (int i = 0; i < size; i++) {
       cout << sortedList[i].grade << " " << sortedList[i].last << " " <<
                                                           sortedList[i].first << endl;
   }
   cout << endl;
}

// ----------------------------------------------------------------------------
//setHistogram
//Initializes the values for the StudentType grades histogram
int* setHistogram(int histogram[], const StudentType sortedList[],
   const int sortedListSize) {
   for (int i = 0; i < HISTOGRAMSIZE; i++) {
       int sortedListCount = 0;
       histogram[i] = 0; //Initializes the int at i to 0.
       while(sortedListCount < sortedListSize) {
           if ( (sortedList[sortedListCount].grade >= (i * 10)) &&
               (sortedList[sortedListCount].grade <= ((i * 10) + 9)) ) {
               histogram[i] += 1;
               sortedListCount++;
           }
           else {
               sortedListCount++;
           }
       }
   }
   return histogram;
}

// ----------------------------------------------------------------------------
//displayHistogram
//Prints the histogram representing the distribution of
//StudentType grades in the studentsList.
void displayHistogram(const int histogram[]) {
   cout << "Histogram of grades:" << endl;
   for (int i = 0; i < HISTOGRAMSIZE; i++) {
       int histCount = histogram[i];
       if (i < 10) {
           cout << setw(3) << (i * 10) << "-->" <<
                       setw(4) << (i * 10) + 9 << " ";
           while (histCount > 0) {
               cout << "*";
               histCount--;
           }
           cout << endl;
       }
       else {
           cout << setw(3) << 100 << "-->" << setw(4) << 100 << " ";
           while (histCount > 0) {
               cout << "*";
               histCount--;
           }
           cout << endl << endl;
       }
   }
}

// ----------------------------------------------------------------------------
//findAverage
//Computes the average grade of all StudentTypes in the sorted StudentType list
int findAverage(const StudentType sortedList[], const int size) {
   int sum = 0;
   for (int i = 0; i < size; i++) {
       sum += sortedList[i].grade;
   }
   return sum / size;
}

// ----------------------------------------------------------------------------
// sortInputAlgorithm
//Helper method for sortInput that places argument tempStudent in its
//sorted position in studentsList
void sortInputSelectionSort(int& argSize, StudentType studentsList[],
                                                       StudentType tempStudent) {
   /*arrCounter represents the currently pointed index in
   studentsList while running the selection sort */
   int arrCounter = argSize - 1;
   if ((tempStudent.grade >= LOWGRADE && tempStudent.grade <= MAXGRADE)) {
       //Still elements in studentsList to compare to tempStudent
       while (arrCounter >= 0) {
           /*If the last name of the current tempStudent is alphabetically
           lower than that of the currently pointed StudentType in studentsList*/
           if (strcmp(tempStudent.last, studentsList[arrCounter].last) < 0) {
               /*Move the currently pointed StudentType in studentsList up one
               space in the array*/
               studentsList[arrCounter + 1] = studentsList[arrCounter];
           }
           /*If the last names are the same, but the current tempStudent's first
           name is lower than the current StudentType in studentsList, */
           else if (strcmp(tempStudent.last, studentsList[arrCounter].last) == 0
               && strcmp(tempStudent.first, studentsList[arrCounter].first) < 0) {
               /*Move the currently pointed StudentType in studentsList up one
               space in the array*/
               studentsList[arrCounter + 1] = studentsList[arrCounter];
           }
           /*If both names are completely identical,
           stop looping; the correct position is found*/
           else { break; }
           arrCounter--;
       }
       studentsList[arrCounter + 1] = tempStudent;
       argSize++;
   }
   else {
       /*Do nothing; skip over the current tempStudent
       so it can be overridden by the next*/
   }
}

Explanation / Answer

#include <string.h>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int const MAXSIZE = 100; // maximum number of records in total
int const MAXLENGTH = 31; // maximum string length
int const MAXGRADE = 100; // highest possible grade
int const LOWGRADE = 0; // lowest possible grade
int const GROUP = 10; // group amount
int const HISTOGRAMSIZE = (MAXGRADE - LOWGRADE) / GROUP + 1;// grouped by GROUP
struct StudentType { // information of one student
int grade; // the grade of the student
char last[MAXLENGTH]; // last name (MAXLENGTH-1 at most)
char first[MAXLENGTH]; // first name (MAXLENGTH-1 at most)
};
// prototypes go here
bool sortInput(ifstream& argFile, StudentType studentsList[], int &argSize);
void displayList(const StudentType sortedList[], const int size);
int* setHistogram(int histogram[], const StudentType sortedList[],
const int sortedListSize);
void displayHistogram(const int histogram[]);
int findAverage(const StudentType sortedList[], const int size);
void sortInputSelectionSort(int& argSize, StudentType studentsList[],
StudentType tempStudent);
int findHighest(const StudentType studentsList[], const int size);
int findLowest(const StudentType studentsList[], const int size);

//-----------------------------------------------------------------------------
int main() {
StudentType students[MAXSIZE]; // list of MAXSIZE number of students
int size = 0; // total number of students
int histogram[HISTOGRAMSIZE]; // grades grouped by GROUP
int average = 0; // average exam score, truncated
// creates file object and opens the data file
ifstream infile("data1.txt");
if (!infile) {
cout << "File could not be opened." << endl;
return 1;
}
  
// read and sort input by last then first name
bool successfulRead = sortInput(infile, students, size);
infile.close(); // Close the file stream
// display list, histogram, and class average
if (successfulRead) {
displayList(students, size); //Display the sorted student list to console

//print max & min
cout<<" Highest grade: "<<findHighest(students, size)<<endl;
cout<<" Lowest grade: "<<findLowest(students, size)<<endl;

//assign values to the histogram array
setHistogram(histogram, students, size);
displayHistogram(histogram); //Display the histogram to console
//Compute the average of the grades
average = findAverage(students, size);
cout << "Average grade: " << average << endl << endl;
}
  
return 0;
}
bool sortInput(ifstream& argFile, StudentType studentsList[], int &argSize) {
//returns true if the file to be read from has been opened
if (argFile.is_open()) {
StudentType tempStudent; //Current StudentType student read from the file
  
while (argFile >> tempStudent.last >> tempStudent.first >>
tempStudent.grade) {
if (argSize == 0) {
//Loop through file until first valid studentType is found
for (;;) {
//Check for invalid grade; if false, skips over the current
//tempStudent to be overridden by the next.
if ((tempStudent.grade >= LOWGRADE &&
tempStudent.grade <= MAXGRADE)) {
studentsList[0] = tempStudent;
argSize++;
break;
}
argFile >> tempStudent.last >> tempStudent.first >>
tempStudent.grade;
}
}
else if (argSize < MAXSIZE) {// While array is still in bounds
/*arrCounter represents the currently pointed index in
studentsList while running the selection sort */
int arrCounter = argSize - 1;
if ((tempStudent.grade >= LOWGRADE &&
tempStudent.grade <= MAXGRADE)) {
/*Place the current tempStudent into its sorted position
in studentsList*/
sortInputSelectionSort(argSize, studentsList, tempStudent);
}
}
else {
cout << "Reached Max Size of Array" << endl;
break;
}
}
return true;
}
else { return false; }
}
// ----------------------------------------------------------------------------
//displayList
//Prints the contents of the sorted StudentType list
void displayList(const StudentType sortedList[], const int size) {
cout << "List of names sorted:" << endl;
for (int i = 0; i < size; i++) {
cout << sortedList[i].grade << " " << sortedList[i].last << " " <<
sortedList[i].first << endl;
}
cout << endl;
}
// ----------------------------------------------------------------------------
//setHistogram
//Initializes the values for the StudentType grades histogram
int* setHistogram(int histogram[], const StudentType sortedList[],
const int sortedListSize) {
for (int i = 0; i < HISTOGRAMSIZE; i++) {
int sortedListCount = 0;
histogram[i] = 0; //Initializes the int at i to 0.
while(sortedListCount < sortedListSize) {
if ( (sortedList[sortedListCount].grade >= (i * 10)) &&
(sortedList[sortedListCount].grade <= ((i * 10) + 9)) ) {
histogram[i] += 1;
sortedListCount++;
}
else {
sortedListCount++;
}
}
}
return histogram;
}
// ----------------------------------------------------------------------------
//displayHistogram
//Prints the histogram representing the distribution of
//StudentType grades in the studentsList.
void displayHistogram(const int histogram[]) {
cout << "Histogram of grades:" << endl;
for (int i = 0; i < HISTOGRAMSIZE; i++) {
int histCount = histogram[i];
if (i < 10) {
cout << setw(3) << (i * 10) << "-->" <<
setw(4) << (i * 10) + 9 << " ";
while (histCount > 0) {
cout << "*";
histCount--;
}
cout << endl;
}
else {
cout << setw(3) << 100 << "-->" << setw(4) << 100 << " ";
while (histCount > 0) {
cout << "*";
histCount--;
}
cout << endl << endl;
}
}
}
// ----------------------------------------------------------------------------
//findAverage
//Computes the average grade of all StudentTypes in the sorted StudentType list
int findAverage(const StudentType sortedList[], const int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += sortedList[i].grade;
}
return sum / size;
}
// ----------------------------------------------------------------------------
// sortInputAlgorithm
//Helper method for sortInput that places argument tempStudent in its
//sorted position in studentsList
void sortInputSelectionSort(int& argSize, StudentType studentsList[],
StudentType tempStudent) {
/*arrCounter represents the currently pointed index in
studentsList while running the selection sort */
int arrCounter = argSize - 1;
if ((tempStudent.grade >= LOWGRADE && tempStudent.grade <= MAXGRADE)) {
//Still elements in studentsList to compare to tempStudent
while (arrCounter >= 0) {
/*If the last name of the current tempStudent is alphabetically
lower than that of the currently pointed StudentType in studentsList*/
if (strcmp(tempStudent.last, studentsList[arrCounter].last) < 0) {
/*Move the currently pointed StudentType in studentsList up one
space in the array*/
studentsList[arrCounter + 1] = studentsList[arrCounter];
}
/*If the last names are the same, but the current tempStudent's first
name is lower than the current StudentType in studentsList, */
else if (strcmp(tempStudent.last, studentsList[arrCounter].last) == 0
&& strcmp(tempStudent.first, studentsList[arrCounter].first) < 0) {
/*Move the currently pointed StudentType in studentsList up one
space in the array*/
studentsList[arrCounter + 1] = studentsList[arrCounter];
}
/*If both names are completely identical,
stop looping; the correct position is found*/
else { break; }
arrCounter--;
}
studentsList[arrCounter + 1] = tempStudent;
argSize++;
}
else {
/*Do nothing; skip over the current tempStudent
so it can be overridden by the next*/
}
}

int findHighest(const StudentType studentList[], const int size){
   int mx = studentList[0];
   for(int i=0;i<size;i++){
       //check if even larger available
       if(mx < studentList[i])
           mx = studentList[i];
   }
   return mx;
}

int findLowest(const StudentType studentList[], const int size){
   int mn = studentList[0];
   for(int i=0;i<size;i++){
       //check if even small available
       if(mn > studentList[i])
           mn = studentList[i];
   }
   return mn;
}

-----------------------------

Hastebin link in case formatting gets lost: http://www.hastebin.com/tawapemuvi.cpp