2 Replies - 558 Views - Last Post: 05 December 2012 - 05:46 PM Rate Topic: -----

#1 slappy5star  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-November 12

Sorting an array of structs

Posted 05 December 2012 - 02:32 PM

I need to sort an array of structs. I am sorting using the int paramater in the struct. The problem I have is that there is also a string value and when printing the array, they stay in the same order they were entered. I want them to stay with the appropriate int. Any tips to help make this work?

Here is the code:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

//struct for scores and names
struct Scores 
{
	string name;
	int score;
};

//function prototypes
void showArray (Scores *, int);
void avgScores (double *, int);
void sortScores (Scores *, int);

/***********Function Main***********/
int main ()
{
	int numScores = 0;
	double *ptr = NULL; //pointer for dynamic array size

	//get number of test scores
	cout << "How many test scores will you enter? ";
	cin >> numScores;
	cout << endl;

	Scores *ptrScores; //creates a pointer to an array of structures		
	
	ptrScores = new Scores [numScores]; //creates an array of structers

	for (int score = 0; score < numScores; score++)
	{
		cout << "Enter a student name: ";
		cin >> ptrScores[score].name;
		cout << "Enter " << ptrScores[score].name << "'s grade: ";
		cin >> ptrScores[score].score;

		if (ptrScores[score].score < 0)
		{
			cout << "Please only enter positive numbers.";
			cout << "Please enter the grade again: ";
			cin >> ptrScores[score].score;
		}
	}

	sortScores(ptrScores, numScores);
	showArray(ptrScores, numScores);

	return 0;
}

/***************sortScores******************/
//This function sorts test scores in an array
void sortScores(Scores *array, int size)
{
	int temp;
	bool swap;
	
	//sort array in ascending order
	do
	{	swap = false;

		for (int count = 0; count < (size-1); count++)
		{
			if (array[count].score > array[count + 1].score)
			{
				temp = array[count].score;
				array[count].score = array[count + 1].score;
				array[count + 1].score = temp;
				swap = true;
			}
		}
	 }while(swap);
}

/***************avgScores******************/
//This function averages test scores in an array
void avgScores(double *array, int size)
{

}

/***************showArray******************/
//This function prints the values in an array
void showArray (Scores *array, int size)
{
	cout << "The test scores you entered are: \n";

	//prints each array element
	for (int index = 0; index < size; index++)
	{
                 cout << array[index].name << " ";
		cout << array[index].score << endl;
	}
	cout << endl;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Sorting an array of structs

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3992
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: Sorting an array of structs

Posted 05 December 2012 - 02:53 PM

When sorting structures you need to use one of the member variables for the sort condition, but actually swap the complete structure, not the member variables.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Sorting an array of structs

Posted 05 December 2012 - 05:46 PM

There is already a built in sort method in the standard library. The sort is generalized so you can pass your own comparison function.

i.e.
#include <iostream>
#include <algorithm>

struct Person {
   int age;
   double height;
   std::string name;
};

bool personCompare(const Person perA, const Person perB) {
   return perA.age > perB.age;
}

int main() {
   Person people[3] = {{10, 6.2, "Justin"}, {22, 5.9, "Rick"}, {44, 6.0, "Bob"}};
   std::sort(people, people + 3, personCompare);

   return 0;
}


This post has been edited by jjl: 05 December 2012 - 05:47 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1