Question

In: Computer Science

1. All members may be public for this example 2. You will need three data members:...

1. All members may be public for this example
2. You will need three data members:
a. An array of 3 doubles to keep the votes

b. An array of 3 strings to place the candidates names

c. An array of 3 doubles to place the calculates vote breakdown

3. You will need three methods:
a. A constructor that receives three strings and sets the names of the candidates to these strings and set all the votes initially to zero.
b. A "calcPercentages" method that has no arguments or return values and simply calculates each vote percentage as one vote count divided by the sum times 100
c. A "display Results" method that has no arguments and displays each candidate's names and vote percentage.

source:
#include <iostream>       //for cout
#include <string>       //for string
#include <stdlib.h>        //for rand, srand
#include <time.h>       //for time

using namespace std;
//DO NOT EDIT ANY CODE ABOVE THIS LINE EXCEPT FOR LINE 12 (EDITOR'S NAME)


class CElection
{
   public:
      
};

//DO NOT EDIT ANY CODE BELOW THIS LINE!
int main(void)
{
   //Instantiate a CElection object with three names:
   CElection TOOP("Alice","Bob","Other");

   //First display a hello message
   cout << "\n\n* Welcome to Decision 2019 *\n";

   //Randomize the PRN
   srand(time(NULL));

   int vote;
   for (int i=1;i<=10000;i++)
   {
       vote = rand()%5;
       switch (vote)
       {
       case 0:
       case 1:
           TOOP.votes[0]++;
           break;
       case 2:
       case 3:
           TOOP.votes[1]++;
           break;
       case 4:
           TOOP.votes[2]++;
       }
   }

   TOOP.calcPercentages();

   TOOP.displayResults();
}

​​​​​​​

Solutions

Expert Solution

****This requires some effort so please drop a like if you are satisfied with the solution****

I have satisfied all the requirements of the question and I'm providing the screenshots of code and output for your reference...

Code:

#include <iostream> //for cout
#include <string> //for string
#include <stdlib.h> //for rand, srand
#include <time.h> //for time
using namespace std;
class CElection
{
public:
    double votes[3];
    string candidateNames[3];
    double votePercentages[3];
    CElection(string candidate1,string candidate2,string candidate3){
        this->candidateNames[0]=candidate1;
        this->candidateNames[1]=candidate2;
        this->candidateNames[2]=candidate3;
        for(int i=0;i<3;i++)
            this->votes[i]=0;
   }
void calcPercentages(){
   double sum=0;
   for(int i=0;i<3;i++)
       sum+=this->votes[i];
   for(int i=0;i<3;i++)
       this->votePercentages[i]=(this->votes[i]/sum)*100;
   }
   void displayResults(){
       for(int i=0;i<3;i++)
           cout<<"\n"<<candidateNames[i]<<" "<<votePercentages[i];
   }
};
//DO NOT EDIT ANY CODE BELOW THIS LINE!
int main(void)
{
//Instantiate a CElection object with three names:
CElection TOOP("Alice","Bob","Other");
//First display a hello message
cout << "\n\n* Welcome to Decision 2019 *\n";
//Randomize the PRN
srand(time(NULL));
int vote;
for (int i=1;i<=10000;i++)
{
vote = rand()%5;
switch (vote)
{
case 0:
case 1:
TOOP.votes[0]++;
break;
case 2:
case 3:
TOOP.votes[1]++;
break;
case 4:
TOOP.votes[2]++;
}
}
TOOP.calcPercentages();
TOOP.displayResults();
}

Output Screenshot:

Code Screenshots:


Related Solutions

Problem 2: There are no data errors that need to be checked as all the data...
Problem 2: There are no data errors that need to be checked as all the data will be assumed correct. You can use the array of the previous example to test your program, however, I suggest that you also use other input arrays to validate the correctness and efficiency of your solution. • Your program MUST be submitted only in source code form (.java file). Write a program in Java to implement a recursive search functionint terSearch(int A[], int l,...
Probability and Random Processes 1. What are members of a Set called? 2. Give an example...
Probability and Random Processes 1. What are members of a Set called? 2. Give an example of countable and uncountable sets. 3. Give an example of when a Venn Diagram would be helpful. 4. Give an example when Permutations and Combination must be used.
Describe 5 different types of data distributions. You may include jpegs or bitmaps. Provide 2 example...
Describe 5 different types of data distributions. You may include jpegs or bitmaps. Provide 2 example of a variable that is representative for each distribution. You may not use the standard normal. t-distribution, F-distribution, Chi-Square distribution, Binomial distribution, or uniform distribution. These distributions are all covered in the course.
PUBLIC RELATIONS: 1) Public Relations: a. Definition and b. marketing example 2) Publicity: a. Definition and...
PUBLIC RELATIONS: 1) Public Relations: a. Definition and b. marketing example 2) Publicity: a. Definition and b. marketing example 3) Product placement 4) Consumer education 5) Sponsorship 6) Managing Unfavorable Publicity a. definition b. Crisis management—marketing example
Give an example of a problem when you may need to use the central limit theorem...
Give an example of a problem when you may need to use the central limit theorem to compute thr cjances of an event and why
1. We are all may be here for a different reason - some of us need...
1. We are all may be here for a different reason - some of us need the general education requirement, some are fulfilling a program requirement, and some may find the material interesting! Either way, I'm sure that we all come into this class with a preconceived view about the field of psychology. So...what do you think?  Are psychologists just those bearded, white-haired old timers who nod at your every word while judging everything you say!? Do you have a preconceived...
You may need to use the appropriate technology to answer this question. The following data are...
You may need to use the appropriate technology to answer this question. The following data are from a completely randomized design. Treatment A B C 161 143 126 143 157 121 164 125 137 144 141 139 149 137 151 169 143 124 Sample mean 155 141 133 Sample variance 122.8 107.2 130.0 (a) Compute the sum of squares between treatments. (b) Compute the mean square between treatments. (c) Compute the sum of squares due to error. (d) Compute the...
Answer all three 1. Identify two traits shared by members of the phyla Echinodermata and Chordata...
Answer all three 1. Identify two traits shared by members of the phyla Echinodermata and Chordata not seen in the other animal phyla you observed. 2. Hypothesize why the skeletons of vertebrates have the same general bone types. 3. What is an advantage of bilateral body symmetry?
I need a masters research thesis topic on data science or data analysis. you may wish...
I need a masters research thesis topic on data science or data analysis. you may wish to add an outline. thanks
You may need to use the appropriate technology to answer this question. Consider the following data...
You may need to use the appropriate technology to answer this question. Consider the following data for a dependent variable y and two independent variables, x1 and x2. x1 x2 y 30 12 93 47 10 108 25 17 112 51 16 178 40 5 94 51 19 175 74 7 170 36 12 117 59 13 142 76 16 212 The estimated regression equation for these data is ŷ = −19.56 + 2.03x1 + 4.76x2. Here, SST = 15,418.9,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT