Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A democrat and a republican are running for an open seat in the House of Represe

ID: 3596302 • Letter: A

Question

A democrat and a republican are running for an open seat in the House of Representatives. In an exit poll voters are asked which candidate they voted for. We want a C++ program to calculate what percentage of votes each candidate received. We will do this in two steps

(a) Write a program to do the following. It asks the data entry clerk to enter which candidate each voter voted for. If the voter voted for the democrat, enter the letter ‘D’. If the voter voted for the republican, enter the letter ‘R’. If the voter voted for anybody else, enter the letter ‘O’. The program should use a while loop to asks for the votes one by one. When there is no more votes to enter, ‘Q’ will be entered to terminate the loop. Don’t count anything yet. Simply ask for the votes with a while loop. Remember that the user may enter uppercase or lowercase letters. Your program should be able to handle both.

(b) Modify the program in part (a) so that it will do the following using a switch statement: (10 points)

      (1) counts and display the number of votes the democrat candidate received

      (2) counts and display the number of votes the republican candidate received

      (3) counts and display the number of votes other candidates received, i.e. the number of times ‘O’ or ‘o’ is entered

      (4) calculate and display what percentage of total votes each candidate received (democrat, republican, and others)

Explanation / Answer

#include<iostream>
using namespace std;
int main()
{
   int republican=0,democrat=0,others=0,k=1;
   while(true)
   {
       char ch;
       cout<<"Enter D for Democrat ";
       cout<<"Enter R for Republican ";
       cout<<"Enter O for Others ";
       cout<<"Enter Q if No more Votes ";
       cout<<"Enter Candidate Letter :";
       cin>>ch;
       switch(ch)
       {
           case 'D':
           case 'd':
               democrat++;
               break;
           case 'R':
           case 'r':
               republican++;
               break;
           case 'O':
           case 'o':
               others++;
               break;
           case 'Q':
           case 'q':
               k=0;
               break;
           default :
               cout<<" Enter corect choice ";
       }
       if(k==0)
       {
           break;
       }
   }
   cout<<" Number of Votes the Democrat candidate received :"<<democrat;
   cout<<" Number of Votes the Republican candidate received :"<<republican;
   cout<<" Number of Votes the Other candidate received :"<<others;
   float dem,rep,oth;
   cout<<endl;
   dem=(float)democrat/(float)(democrat+republican+others);
   cout<<dem*100.0<<"% votes Democrat candidate received ";
   rep=(float)republican/(float)(democrat+republican+others);
   cout<<rep*100.0<<"% votes Republican candidate received ";
   oth=(float)others/(float)(democrat+republican+others);
   cout<<oth*100.0<<"% votes Others candidate received ";
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote