Thread: voting function()

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    voting function()

    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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: voting function()

    Code:
    Tally::Winner( type array[], type size )
    {
        int x = 0, tie = 0, winner = -1, highcell = -1, highvalue = -1;
    
        for( x = 0; x < size; x++ )
            if( array[x] > highvalue )
            {
                highvalue = array[x];
                highcell = x;
            }
            else
            if( array[x] == highvalue )
            {
                tie = 1;
            }
        if( tie )
        {
            for( x = 0; x < size; x++ )
            {
                if( array[x] == highvalue )
                    // so and so is tied...
            }
        }
        else
            // array[highcell] is the winner with highvalue votes
    }
    Something like that is probably what I'd do.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    Thanks quzah for the tip but I must pass in the four parameters into the function. The function prototype must remain the same. Thanks anyway.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, slight modification then:

    type array[4] = { a, b, c, d };
    const type size 4;

    There you go. Add those two variables to your declarations, and keep your prototype the same. Change type to whatever you need.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    supaben34

    You completely lost me now Quzah. Could you please explain it a bit more clearly?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    Tally::Winner( int a, int b, int c, int d )
    {
        int x = 0, tie = 0, winner = -1, highcell = -1, highvalue = -1;
        int array[4] = { a, b, c, d };
        const int size = 4;
    
        for( x = 0; x < size; x++ )
    
        ... the rest of the code here ...
    A simple modification to the code I showed the first time gives it your original prototype, and still makes use of the array method.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    Hey all,
    quzah, if you're there I was hoping you could help me out on more little thing. The function you gave me works great. The thing is when you are tied a person, the program requirements demand that you output a comma seperating each one of them. Where do I put this? This is basically the code you have given me tqeaked just a bit:
    Code:
    void Tally::Winner(int a, int b, int c, int d)
    {
      // This function is developed with help from user: 'quzah' of cprogramming.com
    
      int x = 0, tie = 0, winner = -1, highcell = -1, highvalue = -1;
      int array[4] = { a, b, c, d };
      const int size = 4;
      for( x = 0; x < size; x++ ){
        if( array[x] > highvalue )
          {
            highvalue = array[x];
            highcell = x;
          }
        else
          if( array[x] == highvalue )
            {
              tie = 1;
            }
      }
      if( tie )
        {
          cout << "The winner for district " << district_num << " is a tie between:  ";
          for( x = 0; x < size; x++ )
            {
              // where would I put the comma ???
    if( array[x] == highvalue ){
                  switch (x){
                  case 0:
                    cout << "Bush ";
                    break;
                  case 1:
                    cout << "Gore ";
                    break;
                  case 2:
                    cout << "Buchanan ";
                    break;
                  case 3:
                    cout << "Other ";
                    break;
    }
              }
            }
          cout << "\n";
        }
      else
        {
          cout << "The winner for district " << district_num << " is ";
    
          switch (array[highcell]){
          case 1:
            cout << "Bush";
            break;
          case 2:
            cout << "Gore";
            break;
          case 3:
            cout << "Buchanan";
            break;
          case 4:
            cout << "Other";
            break;
          }
          cout << "\n";
    
        }
    }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could make another slight modification. Instead of setting "tie" to 1, increment it every time.

    Then do something like:
    Code:
    if( tie == 1 )
        //two way tie, output a line of text with both names, formatted however you like
    else
    if( tie == 2 )
       //three way tie, see above
    else
      //everyone tied
    Personally, I'd make a quick lookup table to use for their names. A simple static array in your class would work:
    Code:
    static char *names[] = "Bush", "Gore", "Buchanan", "Other";
    Then you may use it something like:
    Code:
    switch( tie )
    {
        case 0:
            cout << names[highcell] << " has won the election!" << endl;
            break;
        case 1:
            cout << names[highcell] << " and ";
            for( x = 0; x < size; x++ )
                if( x != highcell && array[x] == highvalue )
                    cout << names[x] << " have tied!" << endl;
             break;
            ...other cases...
    There are many many ways to do this. Above are a few.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM