Create a voting simulator. Requirements: (1) There are four candidates. You may
ID: 3929926 • Letter: C
Question
Create a voting simulator. Requirements:
(1) There are four candidates. You may assign probability for each candidate. The example shown below assigned 45% probability for candidate C and T, 7% probability for candidate J, and 3% probability for candidate S. Hint: Use a random number generator to generate a number and use if / switchcase statements to decide which candidate receives the vote.
(2) The number of votes will be 100 million. Hint: Use a loop.
(3) The result must be well formatted. Name, Number of accumulated votes, and histogram associated with the number of votes need to be shown. One * represents one million in the example. Hint: Use formatting and loops.
(4) You must verify the total number of votes by adding the number of votes for each candidate.
Output example :
Explanation / Answer
Please follow the code and comments for description :
CODE :
#include <iostream> // required header files
#include <cstdlib>
#include <ctime>
using namespace std;
int main() // driver method
{
cout << "Welcome to Election Simulator." << endl; // prompt about the code
cout << " The Results : " << endl;
srand(time(NULL)); // nullyfing the random generator time
long cVotes = (rand() % 1000000) + 44500000; // cVotes in the range 45000000
cout << "Candidate C : "<< cVotes << " : ";
if(cVotes >= 44500000 && cVotes <= 45000000){ // checking the votes
for(int i = 1; i <= 45; i++){
cout << "*"; // printing the histogram
}
} else if(cVotes > 45000000){
for(int i = 1; i <= 46; i++){
cout << "*"; // printing the histogram
}
}
cout << endl;
long jVotes = (rand() % 1000000) + 6500000; // jVotes in the range 7000000
cout << "Candidate J : "<< jVotes << " : ";
if(jVotes >= 6500000 && jVotes <= 7000000){
for(int i = 1; i <= 7; i++){ // printing the histogram
cout << "*";
}
} else if(jVotes > 7000000) {
for(int i = 1; i <= 8; i++){ // printing the histogram
cout << "*";
}
}
cout << endl;
long sVotes = (rand() % 1000000) + 2500000; // sVotes in the range 3000000
cout << "Candidate S : "<< sVotes << " : ";
if(sVotes >= 2500000 && sVotes <= 3000000){
for(int i = 1; i <= 3; i++){
cout << "*"; // printing the histogram
}
} else if(sVotes > 3000000) {
for(int i = 1; i <= 4; i++){
cout << "*"; // printing the histogram
}
}
cout << endl;
long remVotes = 100000000 - (cVotes + sVotes + jVotes);
long tVotes = remVotes; // tVotes in the range 45000000
cout << "Candidate T : "<< tVotes << " : ";
if(remVotes >= 44500000 && remVotes <= 45000000){
for(int i = 1; i <= 45; i++){
cout << "*"; // printing the histogram
}
} else if(remVotes > 45000000){
for(int i = 1; i <= 46; i++){
cout << "*"; // printing the histogram
}
}
cout << endl;
cout << " Total Votes : # 100000000" << endl; // printing the total votes
return 0;
}
OUTPUT :
Welcome to Election Simulator.
The Results :
Candidate C : 44972930 : *********************************************
Candidate J : 7176418 : ********
Candidate S : 3052188 : ****
Candidate T : 44798464 : *********************************************
Total Votes : # 100000000
Hope this is helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.