Sorting an Array of Structs

Sorting an Array of Structs using Bubble Sort

Page 1 of 1

2 Replies - 6925 Views - Last Post: 03 June 2009 - 02:15 PM Rate Topic: -----

#1 nhubred  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-June 09

Sorting an Array of Structs

Posted 03 June 2009 - 01:10 PM

Hi, I need to sort an array of structs using Bubble Sort or Selection Sort. I am in a beginners class, and the way we have to write our code is very specific. I have everything completed, but when I am in the function sort and I call the swap function, it only swaps the two variables in the array of structs, rather than the entire array of structs. So the TOTavg is printing in descending order like it should, but the rest of the information (name, midterm, final, etc) is staying in its original position. Any ideas? Thanks in advance for all help!

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


Is This A Good Question/Topic? 0
  • +

Replies To: Sorting an Array of Structs

#2 ajwsurfer  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 373
  • Joined: 24-October 06

Re: Sorting an Array of Structs

Posted 03 June 2009 - 01:47 PM

The problem is in the "swap" function

When you call it it needs to swap "A_STUDENT" instead of an "int"
so call it using:
 swap(TheClass[j], TheClass[j+1]);


and write it using:
void swap(A_STUDENT& x, A_STUDENT& y)
{
	A_STUDENT temp;
	temp = x;
	x = y;
	y = temp;
}



;)
Was This Post Helpful? 1
  • +
  • -

#3 nhubred  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-June 09

Re: Sorting an Array of Structs

Posted 03 June 2009 - 02:15 PM

View Postajwsurfer, on 3 Jun, 2009 - 12:47 PM, said:

The problem is in the "swap" function

When you call it it needs to swap "A_STUDENT" instead of an "int"
so call it using:
 swap(TheClass[j], TheClass[j+1]);


and write it using:
void swap(A_STUDENT& x, A_STUDENT& y)
{
	A_STUDENT temp;
	temp = x;
	x = y;
	y = temp;
}



;)


Awesome! Thank you so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1