0

I wrote this function in PHP. It divides each variable $votesX, where X is a politic party name, for 1,2,3... to the value of $seats. Now I've to search the the biggest values of the array and replace them with a zero. How can I do it?

function hondt ($votesA,$votesB,$votesC,$votesD,$votesE,$seats){
for ($i=1; $i<=$seats; $i++){
$votes[0][$i]=$votesA/$i;
}
for ($i=1; $i<=$seats; $i++){
$votes[1][$i]=$votesB/$i;
}
for ($i=1; $i<=$seats; $i++){
$votes[2][$i]=$votesC/$i;
}
for ($i=1; $i<=$seats; $i++){
$votes[3][$i]=$votesD/$i;
}
for ($i=1; $i<=$seats; $i++){
$votes[4][$i]=$votesE/$i;
}
}

I need it urgently.

Thank you in advance.

Edited by digital-ether: Do use clear and relevant titles for new threads

5
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by Hani1991
0

Now I've to search the the biggest values of the array and replace them with a zero. How can I do it???

Can you give one example with live values?

2

I think you can do the following:

function hondt ($votesA,$votesB,$votesC,$votesD,$votesE,$seats)
{
    for ($i=1; $i<=$seats; $i++)
    {
        $votes[0][$i]=$votesA/$i;
        $votesMax[0] = ($votes[0][$i] > $votesMax[0])? $votes[0][$i] : $votesMax[0];
    }
    for ($i=1; $i<=$seats; $i++)
    {
        $votes[1][$i]=$votesB/$i;
        $votesMax[1] = ($votes[1][$i] > $votesMax[1])? $votes[1][$i] : $votesMax[1];
    }
    for ($i=1; $i<=$seats; $i++)
    {
        $votes[2][$i]=$votesC/$i;
        $votesMax[2] = ($votes[2][$i] > $votesMax[2])? $votes[2][$i] : $votesMax[2];
    }
    for ($i=1; $i<=$seats; $i++)
    {
        $votes[3][$i]=$votesD/$i;
        $votesMax[3] = ($votes[3][$i] > $votesMax[3])? $votes[3][$i] : $votesMax[3];
    }
    for ($i=1; $i<=$seats; $i++)
    {
        $votes[4][$i]=$votesE/$i;
        $votesMax[4] = ($votes[4][$i] > $votesMax[4])? $votes[4][$i] : $votesMax[4];
    }
    for ($n=0; $n <= 4; $n++)
    {
        $TheVotesMax = ($votesMax[$i] > $TheVotesMax)? $votesMax[$i] : $TheVotesMax[$i];
    }
}

And I think that the following code will do the best briefly:

function hondt ($votesA,$votesB,$votesC,$votesD,$votesE,$seats)
{
    $Names = array($votesA,$votesB,$votesC,$votesD,$votesE);
    for($ii=0; $ii <= 5; $ii++)
    {
        for ($i=1; $i<=$seats; $i++)
        {
            $votes[$ii][$i]=$Names[$ii]/$i;
            $votesMax[$ii] = ($votes[$ii][$i] > $votesMax[$ii])? $votes[$ii][$i] : $votesMax[0];
        }
    }
    for ($i=0; $i <= 4; $i++)
    {
        $TheVotesMax = ($votesMax[$i] > $TheVotesMax)? $votesMax[$i] : $TheVotesMax[$i];
    }
}

Wish it's done what you want. :)

Edited by Pro2000: n/a

0

you need to define $votes as an array
like

$votes = array();

Edited by perfectweb: n/a

0

you need to define $votes as an array

You can try it.. It works perfectly fine (100%) without defining it as an array...
PHP is an easy language that doesn't oblige you to type every single detail (With no excess).

0

okay i allways define everything then later i dont get any problem

make it work make it nice make it fast

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.