Using C Write a program that allows the user to enter the la

(Using C++)

Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate.

The program should then output:

1. each candidate’s name, the number of votes received and the percentage of the total votes;

2. the winner of the election.

A sample output should be like:

Candidate Votes Received % of Total Votes

John 5000 25.91

Miller 4000 20.73

Duffy 6000 31.09

Robin 2500 12.95

Ashley 1800 9.33

Total 19300

The winner of the election is Duffy.

Note: MUST USE DYNAMIC ARRAYS!!!!

Solution

//code has been tested on gcc compiler

#include <iostream>
#include <array>
#include <string>
using namespace std;

int main()
{
cout<<\"set the max value of the dynamic array \"<<endl; //set the max value of the dynamic array
int max;   
cin>>max;
string* candidate_name = new string[max]; //dynamic string array for storing last name
int* votes_count=new int[max]; //dynamic integer array for storing votes

cout << \"Enter the candidates last name\" << endl; //asking for candidates last name

for(int i=0;i<max;i++)
cin>>candidate_name[i]; //storing the candidate name


cout<<\"enter no of votes\"<<endl; //asking for count of votes
for(int i=0;i<max;i++)
cin>>votes_count[i]; //storing votes count



double total=0; //total variable for stoting total votes
float avg[max]; //array for storing percentage of votes
for(int i=0;i<max;i++)
total=total+votes_count[i]; //logic for calculation of total
  
for(int i=0;i<max;i++)   
avg[i]=(double(votes_count[i])/total)*100; //calculating percentage of votes and storing for each //candidate
  


double winner=0; //variable for winner no.of votes
int winner_index=0; //variable for index of winner
for(int i=0;i<max;i++)
{
if(avg[i]>=winner) //calculating the winner using percentage of votes
{winner=avg[i];
winner_index=i; }
}



cout<<\"Candidate\"<<\" \"<<\"Votes Received\"<<\" \"<<\"% of Total Votes\"<<endl;
for(int i=0;i<max;i++)
{
cout<<candidate_name[i]<<\" \"<<votes_count[i]<<\" \"<<avg[i]<<endl;
}
cout<<\"Total\"<<\" \"<<total<<endl;
cout<<\"The winner of the election is \"<<candidate_name[winner_index]<<\".\"<<endl;
return 0;
}
//end of code

*********OUTPUT************
set the max value of the dynamic array
5   
Enter the candidates last name
John
Miller
Duffy   
Robin   
Ashley
enter no of votes
5000
4000
6000
2500
1800
Candidate Votes Received % of Total Votes
John 5000 25.9067   
Miller 4000 20.7254   
Duffy 6000 31.0881
Robin 2500 12.9534
Ashley 1800 9.32642   
Total 19300   
The winner of the election is Duffy.

*********OUTPUT************

(Using C++) Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candida
(Using C++) Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candida

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site