erikccoder / cs50-php

Php rendition of cs50's Lab and Problem Sets
0 stars 0 forks source link

runoff - tabulate #2

Open erikccoder opened 3 years ago

erikccoder commented 3 years ago
function setPreference($pref, $stage)
{
    global $candidates;
    $c_index = $pref[$stage];
    if(isset($c_index))
    {
        $candidate = $candidates[$c_index];
        if(isset($candidate))
        {
            if($candidate['eliminated'])
            {
                setPreference($pref, $stage+1);
            }
            else
            {
                $candidates[$c_index]['vote'] += 1;
            }
        }
    }
}

function tabulate()
{
    global $candidates, $preferences;
    $num_candidates = count($candidates);    
    for($i=0; $i<$num_candidates; $i++)
    {
        $candidates[$i]['vote'] = 0;
    }

    foreach($preferences as $pref)
    {   
        setPreference($pref, 0);
    }
}
worldRecycled commented 3 years ago
         function tabulate(){

        global $preferences;
        global $candidates;
        global $numOfVoters;
        global $numOfCandidate;

        // Init
        for($j=0; $j<$numOfCandidate; $j++)
        {
            $candidates[$cand]["votes"] = 0;
        }

        for ($i=0; $i<$numOfVoters; $i++)
        {
            $pref = $preferences[$i];

            for($j=0; $j<$numOfCandidate; $j++)
            {
                $cand = $pref[$j];

                if($candidates[$cand]["eliminated"] == false)
                {
                    $candidates[$cand]["votes"]++;
                    break;
                }
            }
        }
    }
kentoncckm commented 3 years ago
function tabulate(){
    global $pref, $cand;

    for ($i=0, $ccand=count($cand); $i<ccand; $i++){
        $cand[$i][1] = 0;
    }

    for ($i=0, $cpref=count($pref); $i<cpref; $i++){
        for ($j=0; $j<count($cand); $j++){
            $votefor = $pref[$i][$j];
            if (!$cand[$votefor][2]) {
                $cand[$votefor][1]++; 
                break;
            } 
        }
    }
}
jiroyiu commented 3 years ago
function tabulate($voter_count, $candidate_count)
{
    global $preferences,$candidates;
    for ($i = 0; $i < $voter_count; $i++)
    { 
        for ($j = 0; $j < $candidate_count; $j++)
        {
            if($preferences[$i][1] == $candidates[$j]["name"])
            {
                $candidates[$j]["int"]++;
            }
        }
    }

}