fork download
  1. #include <stdio.h>
  2.  
  3. void run_election(int votes[5][4], const char* label_case) {
  4. printf("=== %s ===\n", label_case);
  5.  
  6. for (int i = 0; i < 5; i++) {
  7. printf("%d\t\t", i + 1);
  8. for (int j = 0; j < 4; j++) {
  9. printf("%d\t\t", votes[i][j]);
  10. }
  11. printf("\n");
  12. }
  13.  
  14. int total_votes_candidate[4] = {0};
  15. int grand_total = 0;
  16.  
  17. for (int j = 0; j < 4; j++) {
  18. for (int i = 0; i < 5; i++) {
  19. total_votes_candidate[j] += votes[i][j];
  20. }
  21. grand_total += total_votes_candidate[j];
  22. }
  23.  
  24. char candidate_names[4] = {'A', 'B', 'C', 'D'};
  25. printf("\nHasil Perhitungan Suara:\n");
  26. for (int j = 0; j < 4; j++) {
  27. double percentage = ((double)total_votes_candidate[j] / grand_total) * 100.0;
  28. printf("Kandidat %c: %d suara (%.2f%%)\n", candidate_names[j], total_votes_candidate[j], percentage);
  29. }
  30. printf("Total Keseluruhan Suara: %d\n\n", grand_total);
  31.  
  32. int winner_index = -1;
  33. for (int j = 0; j < 4; j++) {
  34. double percentage = ((double)total_votes_candidate[j] / grand_total) * 100.0;
  35. if (percentage > 50.0) {
  36. winner_index = j;
  37. break;
  38. }
  39. }
  40.  
  41. if (winner_index != -1) {
  42. // c. Pemenang mutlak (> 50%)
  43. printf("PENGUMUMAN: Kandidat %c memenangkan pemilihan secara mutlak dengan perolehan di atas 50%% suara!\n\n", candidate_names[winner_index]);
  44. } else {
  45. int first_max = -1, second_max = -1;
  46. int first_idx = -1, second_idx = -1;
  47.  
  48. for (int j = 0; j < 4; j++) {
  49. if (total_votes_candidate[j] > first_max) {
  50. second_max = first_max;
  51. second_idx = first_idx;
  52. first_max = total_votes_candidate[j];
  53. first_idx = j;
  54. } else if (total_votes_candidate[j] > second_max) {
  55. second_max = total_votes_candidate[j];
  56. second_idx = j;
  57. }
  58. }
  59. printf("PENGUMUMAN: Tidak ada kandidat yang meraih lebih dari 50%% suara.\n");
  60. printf("Terjadi pemilu lanjutan (runoff) antara Kandidat %c dan Kandidat %c.\n\n", candidate_names[first_idx], candidate_names[second_idx]);
  61. }
  62. }
  63.  
  64. int main() {
  65. int original_votes[5][4] = {
  66. {192, 48, 206, 37},
  67. {147, 90, 312, 21},
  68. {186, 12, 121, 38},
  69. {114, 21, 408, 39},
  70. {267, 13, 382, 29}
  71. };
  72.  
  73. run_election(original_votes, "Uji Coba 1: Data Asli");
  74.  
  75. int modified_votes[5][4] = {
  76. {192, 48, 206, 37},
  77. {147, 90, 312, 21},
  78. {186, 12, 121, 38},
  79. {114, 21, 108, 39},
  80. {267, 13, 382, 29}
  81. };
  82.  
  83. run_election(modified_votes, "Uji Coba 2: Kandidat C Mendapat 108 Suara di Precinct 4");
  84.  
  85. return 0;
  86. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
=== Uji Coba 1: Data Asli ===
1		192		48		206		37		
2		147		90		312		21		
3		186		12		121		38		
4		114		21		408		39		
5		267		13		382		29		

Hasil Perhitungan Suara:
Kandidat A: 906 suara (33.77%)
Kandidat B: 184 suara (6.86%)
Kandidat C: 1429 suara (53.26%)
Kandidat D: 164 suara (6.11%)
Total Keseluruhan Suara: 2683

PENGUMUMAN: Kandidat C memenangkan pemilihan secara mutlak dengan perolehan di atas 50% suara!

=== Uji Coba 2: Kandidat C Mendapat 108 Suara di Precinct 4 ===
1		192		48		206		37		
2		147		90		312		21		
3		186		12		121		38		
4		114		21		108		39		
5		267		13		382		29		

Hasil Perhitungan Suara:
Kandidat A: 906 suara (38.02%)
Kandidat B: 184 suara (7.72%)
Kandidat C: 1129 suara (47.38%)
Kandidat D: 164 suara (6.88%)
Total Keseluruhan Suara: 2383

PENGUMUMAN: Tidak ada kandidat yang meraih lebih dari 50% suara.
Terjadi pemilu lanjutan (runoff) antara Kandidat C dan Kandidat A.