So basically here is the function I am having difficulty with...the entire code is below:
void sort(A_STUDENT TheClass[], int size)
{
for (int pass = 1; pass < size; pass++)
{
for (int j = 0; j < size-pass; j++)
{
if (TheClass[j].TOTavg < TheClass[j+1].TOTavg)
{
swap(TheClass[j].TOTavg, TheClass[j+1].TOTavg);
}
}
}
}
Like I said, TheClass[j] and TheClass[j+1] are being swapped like they should, but I need all members to be swapped (name, final, etc). I tried using the below code as well, but it did nothing at all; everything stayed in the exact same place:
//beginning of function sort goes here...
//function call
if (TheClass[j].TOTavg < TheClass[j+1].TOTavg)
{
swap_all(TheClass[j].TOTavg, TheClass[j+1].TOTavg);
}
//rest of function sort goes here...
void swap_all(A_STUDENT person1, A_STUDENT person2)
{
string temp_name;
int temp_midterm, temp_final, temp_HQavg;
double temp_TOTavg;
char temp_grade;
temp_name = person2.name;
temp_midterm = person2.midterm;
temp_final = person2.final;
temp_HQavg = person2.HQavg;
temp_TOTavg = person2.TOTavg;
temp_grade = person2.grade;
person2.name = person1.name;
person2.midterm = person1.midterm;
person2.final = person1.final;
person2.HQavg = person1.HQavg;
person2.TOTavg = person1.TOTavg;
person2.grade = person1.grade;
person1.name = temp_name;
person1.midterm = temp_midterm;
person1.final = temp_final;
person1.HQavg = temp_HQavg;
person1.TOTavg = temp_TOTavg;
person1.grade = temp_grade;
}
Here is the total program:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
//structure for a students name, hq/quiz average, midterm and final exam grades
struct A_STUDENT
{
string name;
int midterm;
int final;
int hw_quiz[12];
int HQavg;
double TOTavg;
char grade;
};
void get_data(ifstream& in_file, A_STUDENT TheClass[], int& class_size);
//reads the file data1.txt and retrieves the student's name, midterm grade, final exam grade
//and 12 quiz/hw grades. The data is stored in the array of structs A_STUDENT TheClass[i], with class_size
//returning the number of students in the class, not exceeding 10
void HQgrade(A_STUDENT TheClass[], int size);
//precondition: the 12 hw_quiz grades for each student has been stored in the array of structs TheClass[].hw_quiz
//postcondition: the 12 grades are sorted in descending order, and the total of the 10 highest grades is stored in
//TheClass[].HQavg for each student
void swap(int& x, int& y);
//values for x and y are swapped
void swap_all(A_STUDENT person1, A_STUDENT person2);
void TOTavg(A_STUDENT TheClass[], int size);
//precondition: the array of structs TheClass[] has been filled for each students midterm grade, final grade,
//and HQavg(hw/quiz average).
//postcondition: each students course average TOTavg is calculated and stored in TheClass[].TOTavg
void grade(A_STUDENT TheClass[], int size);
//precondition: each students total course average has been stored in the array of structs TheClass[].TOTavg
//postcondition: each students grade is computed based on their course average, and stored in the array of
//structs TheClass[].grade
void sort(A_STUDENT TheClass[], int size);
//the array of structs TheClass is sorted in descending order based on the students total course average (TOTavg)
void display (A_STUDENT TheClass[], int size);
int main()
{
ifstream in_file;
A_STUDENT TheClass[10];
A_STUDENT TheClass_sorted[10];
int class_size = 0;
get_data(in_file, TheClass, class_size);
HQgrade(TheClass, class_size);
TOTavg(TheClass, class_size);
grade(TheClass, class_size);
sort(TheClass, class_size);
display(TheClass, class_size);
in_file.close();
cout << endl;
return 0;
}
void get_data(ifstream& in_file, A_STUDENT TheClass[], int& class_size)
{
in_file.open("data1.txt");
if (in_file.fail())
{
cout << "Opening input file \"data1.txt\" failed\n" << endl;
exit(1);
}
int i = 0;
while(!in_file.eof())
{
in_file >> TheClass[i].name;
if (in_file.eof())
break;
in_file >> TheClass[i].midterm >> TheClass[i].final;
for (int j=0; j < 12; j++)
{
TheClass[i].hw_quiz[j] = 0;
}
for (int j=0; j < 12; j++)
{
in_file >> TheClass[i].hw_quiz[j];
}
class_size++;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
i++;
}
}
void HQgrade(A_STUDENT TheClass[], int size)
{
for (int i = 0; i < size; i++)
{
TheClass[i].HQavg = 0;
}
for (int i=0; i < size; i++)
{
for (int pass = 1; pass <= 12; pass++)
{
for (int j=0; j < 12-pass; j++)
{
if (TheClass[i].hw_quiz[j] < TheClass[i].hw_quiz[j+1])
{
swap(TheClass[i].hw_quiz[j],TheClass[i].hw_quiz[j+1]);
}
}
}
for (int k=0; k < 10; k++)
{
TheClass[i].HQavg = TheClass[i].HQavg + TheClass[i].hw_quiz[k];
}
}
}
void swap(int& x, int& y)
{
int temp;
temp = x;
x = y;
y = temp;
}
void TOTavg(A_STUDENT TheClass[], int size)
{
for (int i = 0; i < size; i++)
{
TheClass[i].TOTavg = 0;
}
for (int i = 0; i < size; i++)
{
TheClass[i].TOTavg = (.30*(TheClass[i].midterm + TheClass[i].final) + .40*(TheClass[i].HQavg));
}
}
void grade(A_STUDENT TheClass[], int size)
{
for (int i = 0; i < size; i++)
{
TheClass[i].grade = 0;
}
for (int i = 0; i < size; i++)
{
if ((TheClass[i].TOTavg <= 100) && (TheClass[i].TOTavg >= 90))
{
TheClass[i].grade = 'A';
}
else if ((TheClass[i].TOTavg <= 89) && (TheClass[i].TOTavg >= 80))
{
TheClass[i].grade = 'B';
}
else if ((TheClass[i].TOTavg <= 79) && (TheClass[i].TOTavg >= 70))
{
TheClass[i].grade = 'C';
}
else if (TheClass[i].TOTavg < 70)
{
TheClass[i].grade = 'F';
}
}
}
void sort(A_STUDENT TheClass[], int size)
{
for (int pass = 1; pass < size; pass++)
{
for (int j = 0; j < size-pass; j++)
{
if (TheClass[j].TOTavg < TheClass[j+1].TOTavg)
{
swap(TheClass[j].TOTavg, TheClass[j+1].TOTavg);
}
}
}
}
void display(A_STUDENT TheClass[], int size)
{
cout << endl << "Name\t\t\t" << "Midterm\t" << "HQAvg\t" << "Final\t" << "Avg\t" << "Grade\n\n";
for (int i = 0; i < size; i++)
{
cout << TheClass[i].name << "\t\t" << TheClass[i].midterm << "\t" << TheClass[i].HQavg << "\t"
<< TheClass[i].final << "\t" << TheClass[i].TOTavg << "\t" << TheClass[i].grade << endl;
}
}
This post has been edited by nhubred: 03 June 2009 - 01:20 PM

New Topic/Question
Reply




MultiQuote




|