Question

In: Computer Science

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    20.72

duffy    6000 31.09

robinson    2500 12.95

sam    1800 9.33

total 19300

the winner of the election is Duffy

Solutions

Expert Solution

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
string *names;
double *votes;
double sum=0;
double winner=-1;
string winnerName="";
int total=0;
int size;
cout<<"Enter the number of candiates: ";
cin>>size;
names=new string[size];
votes=new double[size];
//reading votes
for(int i=0;i<size;i++){
cout<<"Enter candidate's name :";
cin>>names[i];
  
cout<<"Enter votes received :";
cin>>votes[i];
sum+=votes[i];
//finding the winner
if(winner<votes[i]){
winner=votes[i];
winnerName=names[i];
}
}
  
cout<<endl;
//printing the data
cout << setprecision(2)<<fixed;

cout<<setw(20) << left <<"Name"<<setw(20) << left <<"Votes"<<setw(20) << left <<"Percentage"<<endl;
for(int i=0;i<5;i++){
cout<<setw(20) << left<<names[i]<<setw(20) << left <<votes[i]<<votes[i]/sum*100<<"%"<<endl;
total+=votes[i];
  
}
cout<<setw(20) << left<<"Total"<<setw(20) << left <<total<<endl;
cout<<"The Winner of the Election is "<<winnerName;
  
return 0;
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


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...
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
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...
Using Visual Studio write a program that allows the user to enter multiple scores ranging from...
Using Visual Studio write a program that allows the user to enter multiple scores ranging from 0 to 100, computes the average of those scores and then writes out a letter grade using the typical grading scale. Your program will also: 1. Allow the user to enter any number of scores he or she wishes but assume there will be at least one score entered. 2. Use a sentinel-controlled loop variable to terminate the loop when the user has no...
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
Using C++ Write a program to ask user to enter a password and validity the password....
Using C++ Write a program to ask user to enter a password and validity the password. The password should be 8 – 20 characters long (8 is included and 20 is included), it should contain at least one uppercase letter and one lower case letter. The password should contain at least one digit, and at least one symbol other than alphabets or numbers. Most importantly, the password may not contain any white space. Functions: You will need at least the...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Fat Percentage Calculator Create a C++ program that allows the user to enter the number of...
Fat Percentage Calculator Create a C++ program that allows the user to enter the number of calories and fat grams in a food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. One gram of fat has 9 calories, so: Calories from fat = fat grams *...
In Python, write a program that allows the user to enter a number between 1-5 and...
In Python, write a program that allows the user to enter a number between 1-5 and returns the vitamins benefits based on what the user entered. The program should: Continue to run until the user quits and Validate the user’s input (the only acceptable input is 0, 1, 2, 3, 4, or 5), If the user enters zero “0” the program should terminate You can use the following facts: 1- Vitamin A protects eyes from night blindness and age-related decline...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT