Return to Snippet

Revision: 69803
at September 10, 2015 00:27 by garciacarmonaam


Initial Code
void distributeSeats()
		{
			for (int i = 0; i < this->seats; i++)
			{
				int mostvoted_code = 0;
				int votes = 0;
				for (int j = 0; j < this->candidatures_number; j++)
				{
					if (distribution[i][j] > votes)
					{
						mostvoted_code = j;
						votes = distribution[i][j];
					}
				}

				Candidature *mostvoted = NULL;
				bool hasItBeenFound = false;
				this->candidatures->point1stCandidature();
				while (!this->candidatures->isCandidaturesListEndPointed() && !hasItBeenFound)
				{
					this->candidatures->checkPointedCandidature(mostvoted);
					if (mostvoted->getCode() == mostvoted_code)
						hasItBeenFound = true;
					else
						this->candidatures->moveCandidaturesPointer();
				}

				int current_seats = mostvoted->getObtainedSeats();
				mostvoted->setObtainedSeats(current_seats + 1);
				this->candidatures->point1stCandidature();

				if (i + 1 < this->seats)
				{
					distribution[i + 1][mostvoted_code] = distribution[0][mostvoted_code] / (2 * mostvoted->getObtainedSeats() + 1);
					int k = 0;
					while (k < this->candidatures_number)
					{
						if (k != mostvoted_code)
							distribution[i + 1][k] = distribution[i][k];
						k++;
					}
				}
			}
		}

Initial URL
https://github.com/garciacarmonaam/saintelague

Initial Description
Really, this is part of a set of classes that is stored in a Git repository and under MIT license.
The function I'll expose is Circunscription::distributeSeats().

Initial Title
An algorithm of Sainte-Laguë\'s method

Initial Tags
c++

Initial Language
C++