2 Replies - 339 Views - Last Post: 04 December 2012 - 03:54 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 (using pointers)

Posted 04 December 2012 - 03:23 PM

I have to sort an array of structures and I must use pointers. I think I more or less have it worked out, I am just getting one error. When I call my sortArray function it says "Scores is incompatible with type doule". Any tips are appreciated!

the specific error is here:

sortScores(ptrScores, numScores);



Here is the code:

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

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

struct Scores 
{
	string name;
	int score;
};

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;		
	
	ptrScores = new Scores [numScores];

	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);

	return 0;
}

/***************sortScores******************/
//This function sorts test scores in an array
void sortScores(double *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] > 	array[count + 1])
			{
				temp = array[count];
				array[count] = array[count + 1];
				array[count + 1] = temp;
				swap = true;
			}
		}
	 }while(swap);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Sorting an array of structs (using pointers)

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 765
  • View blog
  • Posts: 2,225
  • Joined: 20-March 10

Re: Sorting an array of structs (using pointers)

Posted 04 December 2012 - 03:51 PM

Hi,


Your function sortScores...

should be


void sortScores (Scores *, int);


and you will have to declare the struct Scores before you declare this new function

and so to the actual function..


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)
	




the above is the way to get round your little problem
I leave it up to you to implement the rest...


Best Wishes


Snoopy.
Was This Post Helpful? 0
  • +
  • -

#3 slappy5star  Icon User is offline

  • New D.I.C Head

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

Re: Sorting an array of structs (using pointers)

Posted 04 December 2012 - 03:54 PM

Well, I guess I should have seen that! Thanks for the pointer!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1