Hey all,
I need your help in order to write a helper function for one of the classes I developed a c+ programming course. This function enables me to output who the winner of an election is. The parameters a,b,c,d stand for the total number of votes received for 'bush', 'gore', 'buchanan', and 'other'.
I am creating two arrays, loading them both with the parameters, sorting one of them so I could tell which one the highest number is. After I decided which one the highest number is, I go back and look in the original array for that same highest number. Whichever array element that highest number falls in is the winner. Wait, it gets better!
I have to also account for ties also. Theoretically, they can all be ties.
In the end, if its a tie the function should return something like "The winner is a tie between " .....(however many tied for 1st place). If there is a clear winner, output something like, "The winner is Bush". Here's what I have so far.
Code:
void Tally::Winner(int a, int b, int c, int d)
  // This function should determine who the winner(s) is(are)
{
  // Create two arrays
  int vote[5];
  int vote_sort[5];

  // place bush tally in first element
  vote[0] = a;
  vote_sort[0] = a;
  // place gore tally in second element
  vote[1] = b;
  vote_sort[1] = b;
  // place buchanan tally in third element
  vote[2] = c;
  vote_sort[2] = c;
  // place other tally in fourth element
  vote[3] = d;
  vote_sort[3] = d;

  vote[4] = '\0';
  vote_sort[4] = '\0';
  // Next we sort the elements in descending
  // order(highest to lowest) into vote_sort[]
  int high = a;
  int temp; // temp variable
  for (int i = 1; i < 4; i++){
    if (vote_sort[i] > high){      // if next element is less than or equal to the current high variable
      // move on
      temp = vote_sort[i];
      vote_sort[i] = vote_sort[(i-1)];
      vote_sort[i] = temp;
    }
  }

  // At this point we are assuming we have an descending ordered list
  // We now go ahead and check if there are any 'ties' in the highest votes
  // by checking if the vote_sort array has any duplicates of the highest votes
  // (which is element 0). If there are no ties in the votes, there should only
  // be one highest number. We have to go back to our original array vote[] and check
  // which element number the highest number is residing in.

  // For example if the highest number in vote_sort[] is 15 and the number 15 appears
  // in array element number 1, this means that gore wins the vote and had a total vote of 15.

}
I know that this code is kind of convoluted but this is the last leg of my humongoid program and my brain is fritzed. Any and all feedback is greatly appreciated.