I have to write a program in OOP format that reads in student names/grades from a user provided file, outputs the raw data, sorts & outputs the data by alphabetical order, and sorts/outputs the data by final average.
My professor provided me with the skeleton of the program, posted below. I have no idea how to sort the student data alphabetically. In the assignment instructions, it said that the class contains overloads and that the < (less than) operator should be used to compare student names.
Can one of you please show me some sample code of how this is supposed to work? I have no clue whatsoever how to do the alphabet sort. So far, I have a general idea of how to knock out the other parts of the program.
#include <iostream>
#include <fstream>
#include <iomanip>
usingnamespace std;
#include "Student.h"
#define ARRAYSIZE 12
/*****************************************************************************/
/* Function Prototypes */
/* Description: Informs compiler about functions to be used in program. */
/*****************************************************************************/
// read the data file, store in arrays
int readDatafile(Student[], int);
// output the formatted "raw data"
void dumpData(Student[]);
// output the names, average, grade data (formatted)
void dumpSorted(Student[]);
// sorting routines - use a bubble sort
void sortAscend(Student[]); // set this up to sort by name
void sortDescend(Student[]); // set this up to sort by average
void swap( ... ); // used by the bubble sort
/*****************************************************************************/
/* Function name: MAIN */
/* Description: Triggers all of the functions in the order listed */
/*****************************************************************************/
main()
{
Student slist[ARRAYSIZE];
int readDatafile();
dumpData();
sortAscend();
sortDescend();
dumpSorted();
return 0;
}
/*****************************************************************************/
/* Function name: readDatafile */
/* Description: Opens & reads the datafile, then returns # of entries read */
/*****************************************************************************/
int readDatafile(Student list[], int max)
{
string fname;
ifstream ifs;
int num=0;
cout << "Enter file Name: ";
cin >> fname;
ifs.open(fname.c_str());
if(ifs.fail())
{
cout << "Error opening file: " << fname << endl;
return 0;
}
// assumes each data line is well formatted
while(num < max && ifs >> list[num])
{
num++;
}
ifs.close();
return num;
}
/*****************************************************************************/
/* Function name: dumpData */
/* Description: Output the data that was read in */
/*****************************************************************************/
void dumpData(Student list[])
{
cout << endl;
cout << "Raw Grades:"<< endl;
cout << "----------" << endl;
for(int i = 0; i < max; i ++)
{
cout << setw(2) << list[i].getName() << " / "
cout << setw(2) << list[i].getTests() << " / ";
cout << setw(2) << list[i].getProgs() << " / ";
cout << list[i].getFinal();
cout << endl;
}
}
/*****************************************************************************/
/* Function name: sortAscend */
/* Description: Sort student data in alphabetical order */
/*****************************************************************************/
void sortAscend(Student list[])
{
}
/*****************************************************************************/
/* Function name: sortDescend */
/* Description: Sort student data by average from highest to lowest */
/*****************************************************************************/
void sortDescend(Student list[])
{
// flag to determine if a swap has occurred
// flag is initialized to true to start first pass
bool flag= true;
for(int i = 1; (i <= length) && flag; i++)
{
flag = false;
for(int j=0; j < (length -1); j++)
{
if (list[j+1] > list[j])
{
// swap elements
swap(list[j], list[j+1]);
flag = true; // indicates that a swap occurred.
}
}
}
}
/*****************************************************************************/
/* Function name: dumpSorted */
/* Description: Output the final sorted information */
/*****************************************************************************/
void dumpSorted(Student list[])
{
cout << endl;
cout << "Grades:" << endl;
cout << "------" << endl;
for(int i=0; i<max; i++)
{
cout << list[i] << endl;;
}
}
void swap()
{
}
I’m sure you have already solved your exercise, anyway:
Your code:
1 2 3 4
// greater than - compares grade "average"
booloperator>(const Student &) const; // greater than
// less than - compares student names
booloperator<(const Student &) const; // less than