Question

In: Computer Science

Write a C++ program using dynamic arrays that allows the user to enter the last names...

Write a C++ program using dynamic arrays that allows the user to enter the last names of the candidates in a local election and the number of votes received by each candidate. The program must ask the user for the number of candidates and then create the appropriate arrays to hold the data. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. (20 points) A sample output is: Name of Candidate Votes Received % of Total Votes Ali 5000 25.91 Imran 4000 20.73 Ahmad 6000 31.09 Ijaz 2500 12.95 Khan 1800 9.33 Total 19300 The winner of the local election is Ahmad.

Solutions

Expert Solution

C++ PROGRAM ===>

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

int n;
cout<<"Enter number of candidate : ";
cin>> n;
string name[n]; // array of n size to store last name
int vote [n]; // array of n size to store received vote

int total_vote=0;
for(int i=0;i<n;i++)
{
cout<<"Enter candidate name : ";
cin>>name[i];
cout<<"Enter vote : ";
cin>>vote[i];
}
// for loop to calculate total vote
for(int i =0;i<n;i++)
{
total_vote = total_vote+vote[i];
}

int max = vote[0];
int k,index;
// calculate index of maximum in vote array
for(k=0;k<n;k++)
{
if(vote[k]>=max)
{
max = vote[k];
index = k;
}
}
cout<<endl<<endl;
double percentage;
cout<<left<<setw(35)<<"Name of candidate "<<left<<setw(15)<< "Vote"<<left<<setw(15)<<"Received % "<<endl;

for(int i=0;i<n;i++)
{
percentage = vote[i]*100.0/(total_vote*1.0); // calculate vote percentage
cout<<left<<setw(35)<<name[i]<<left<<setw(15)<<vote[i]<<left<<setw(15)<<fixed<<setprecision(2)<<percentage<<endl;
}
cout<<endl<<"Total vote : "<<total_vote<<endl;
cout<<"The winner is "<<name[index]<<endl<<endl;

return 0;
}

OUTPUT SCREENSHOT  ===>


Related Solutions

C++ Vector Write a program that allows the user to enter the last names of the...
C++ Vector Write a program that allows the user to enter the last names of the candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, votes received by that candidate, and the percentage of the total votes received by the candidate. Assume a user enters a candidate's name more than once and assume that two or more candidates receive the same number of votes. Your program should output the...
Using Pseudocode: Design a program that allows the user to enter 20 names into a String...
Using Pseudocode: Design a program that allows the user to enter 20 names into a String array. Sort the array in ascending (alphabetical) order and display its contents.
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA 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 each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
DATA STRUCTURES USING C++ 2ND EDITION Write a program that allows the user to enter the...
DATA STRUCTURES USING C++ 2ND EDITION Write a program that allows the user to enter the last names of five candidates in a local election and the votes received by that candidate. The program should then output each candidates name, votes received by that candidate, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is as follow Johnson    5000 25.91 miller    4000   ...
Write an application that allows a user to enter the names and birthdates of up to...
Write an application that allows a user to enter the names and birthdates of up to 10 friends. Continue to prompt the user for names and birthdates until the user enters the sentinel value ZZZ for a name or has entered 10 names, whichever comes first. When the user is finished entering names, produce a count of how many names were entered, and then display the names. In a loop, continuously ask the user to type one of the names...
Please code C# 10. Write a program that allows a user to input names and corresponding...
Please code C# 10. Write a program that allows a user to input names and corresponding heights (assumed to be in inches). The user can enter an indefinite number of names and heights. After each entry, prompt the user whether they want to continue. If the user enters true, ask for the next name and height. If the user enters false, display the name of the tallest individual and their height. Sample run: “Name?” James “Height?” 50 “Continue?” True “Name?”...
USING PYTHON. Thank you in advance Write a program that allows the user to enter a...
USING PYTHON. Thank you in advance Write a program that allows the user to enter a series of string values into a list. When the user enters the string ‘done’, stop prompting for values. Once the user is done entering strings, create a new list containing a palindrome by combining the original list with the content of the original list in a reversed order. Sample interaction: Enter string: My Enter string: name Enter string: is Enter string: Sue Enter string:...
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm Explain step by step
Write a program in c++ using only if statements that prompts the user to enter an...
Write a program in c++ using only if statements that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1 …., and Saturday is 6) then displays today. Also, prompt the user to enter the number of days after today for a future day and display the future day of the week. The future day can be computed as follows: (today + number of days after today) % 7 Sample run...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT