Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

selection sort on an array of class objects incomplete

$
0
0
i have written a selection sort algorithm to go sort an array of class objects by age in ascending order, the problem is that the output being given does not match what i think the code should do. when the program runs the 3 records are added to the array and when they are sorted should be outputed in ascending order, the problem is that with my code the last 2 are sorted properly but the first element does not seem to move, it remains the same as the original unsorted value.

thanks for any help given.

my code for the class, selection sort function and the display method are below:


void selectionSort()
{
	int i, minIndex, minValue;
	for (i = 0; i < (arrlength - 1); i++)
	{
		minIndex = i ;
		minValue = player[i].getAge() ;
		for (int index = i + 1; index < arrlength; index++)
		{
			if (player[index].getAge() < minValue)
			{
				minValue = player[index].getAge();
				minIndex = index;

			}
		}

		player[minIndex].setAge(player[i].getAge());
		player[i].getAge() == minValue;

	}



}





void displayallinfo()
{

	selectionSort();

	for (int i=0; i < 3; i++)
	{
		cout << "\n First Name : " << player[i].getFirstName() << "\n" << "Last Name : " << player[i].getLastName() <<
			"\n" << "Age : " << player[i].getAge() << "\n" << "Current Team : " << player[i].getCurrentTeam() << 
			"\n" << "Position : " << player[i].getPosition() << "\n" << "Status :  " << player[i].getStatus()  << "\n\n";
	}

	cin.get() ;

	menu() ;
}







class user 
{
	string firstname, lastname, currentteam, position, status ;
	int age ;
public:
	user() {};
	user(string fname, string lname, string cteam, string pos, string stat, int age) 
	{
		setFirstName(fname);
		setLastName(lname);
		setCurrentTeam(cteam);
		setPosition(pos);
		setStatus(stat);
		setAge(age);
	} ;



	user& operator = (const user& source)
	{
		firstname = source.firstname;
		lastname = source.lastname ;
		currentteam = source.currentteam ;
		position = source.position ;
		status = source.status ;
		age = source.age ;
	}


	void setFirstName(string fname)
		{firstname = fname;}
	void setLastName(string lname)
		{lastname = lname;}
	void setCurrentTeam(string cteam)
		{currentteam = cteam;}
	void setPosition(string pos)
		{position = pos;}
	void setStatus(string stat)
		{status = stat;}
	void setAge(int _age)
		{age = _age;}

	string getFirstName()
		{return firstname ;}
	string getLastName()
		{return lastname ;}
	string getCurrentTeam()
		{return currentteam ;}
	string getPosition()
		{return position ;}
	string getStatus()
		{return status ;}
	int getAge()
		{return age ;}
};




Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>