Advertisement
Guest User

cs50

a guest
Jan 13th, 2024
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. // Record pairs of candidates where one is preferred over the other
  2. void add_pairs(void)
  3. {
  4.  
  5. pair_count = candidate_count * (candidate_count - 1) / 2; //calculate number all pairs
  6. int ties = 0;
  7.  
  8. for (int i = 0; i < pair_count; i++)
  9. {
  10. for (int j = 0; j < pair_count; j++)
  11. {
  12. if(preferences[i][j] == preferences[j][i]) // if tie, tie++
  13. {
  14. ties++;
  15. }
  16. else if(preferences[i][j] > preferences[j][i]) // this adds to pairs array
  17. {
  18. pairs[i].winner = i;
  19. pairs[i].loser = j;
  20. }
  21. else // this too
  22. {
  23. pairs[i].winner = j;
  24. pairs[i].loser = i;
  25. }
  26.  
  27. }
  28. }
  29.  
  30. printf("ties: %i", ties); // check50 shows mistake without that line and i dont get it
  31. /* the mistake:
  32. :( add_pairs generates correct pair count when no ties
  33. add_pairs function did not produce 3 pairs
  34. */
  35. pair_count = pair_count - ties; // subtract number of ties from pairs to get number of valid pairs
  36. return;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement