Question

In: Computer Science

this is data structures with C++ language, please do "#1 & a" separately and please send...

this is data structures with C++ language, please do "#1 & a" separately and please send me copyable file

1. 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 only once and assume that no two candidates receive the same number of votes. Your program should output the winner of the election.

a. Modify your work, above question solution as follows:

replace the int array with a double vector

the winner gets at most 20% of the loser's assets

the loser withdraws from the game if its assets are less than 1.00. In this case the winner gets all of the loser's assets.

Solutions

Expert Solution

#include <iostream>
#include <string>

using namespace std;

int findWinner(int votes[]);
void printResults(string candidates[], int votes[]);
double calculatePercentage(int votes[], int vote);

const int NUMBER_OF_CANDIDATES = 5;

int main()
{
  
string candidates[NUMBER_OF_CANDIDATES];
int votes[NUMBER_OF_CANDIDATES];
  
cout << "Please input the 5 candidates followed by the votes they recieved i.e. Smith 5000: ";
for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
cin >> candidates[i] >> votes[i];
}
printResults(candidates, votes);
cout << "And the winner is: " << candidates[findWinner(votes)] << endl;
return 0;
}

double calculatePercentage(int votes[], int vote){
int sumOfVotes = 0;
  
for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
sumOfVotes += votes[i];
}
  
double percentage = static_cast<double>(vote) / sumOfVotes;
  
return percentage*100;
}


void printResults(string candidates[], int votes[]){
cout << "Name:" << setw(15) << "Votes:" << setw(15) << "Percentage:" << endl;
for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
cout << candidates[i] << setw(15) << votes[i] << setw(15);
int percentage = calculatePercentage(votes, votes[i]);
cout << percentage << "%" << endl;
  
}
}

int findWinner(int votes[]){
int winner = 0;
for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
if (votes[i] > winner)
winner = i;
}
return winner;
}


Related Solutions

Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q...
Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q (4) Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the 1st token...
C++ (data structure using c++). please send me copyable file. Write a program and test a...
C++ (data structure using c++). please send me copyable file. Write a program and test a program that translates the following Bubble Sort algorithm to a bubblesort function. The function's prototype is, void bubblesort(int a[], int size); Bubble Sort The inner loop moves the largest element in the unsorted part of the array to the last position of the unsorted part of the array; the outer loop moves the last position of the unsorted part of the array. The Bubble...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.) Have a unit test implemented in main(). And comment every code. Show examples from the executions. Assume that the edges defined by the vertex pairs in the data base are one-way. Question: Write a program that can answer if there is a path between any to vertices. For the vertex pairs use this as your input example: AL...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.) Have a unit test implemented in main(). And comment every code. Show examples from the executions. Assume that the edges defined by the vertex pairs are two-way. Question: First step: write a program based on DFS which can answer questions of the type: "Find the a path from X to Y" Which should result in a list of...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.) Have a unit test implemented in main(). And comment every code. Show examples from the executions. Question: First step: write a program based on DFS which can answer questions of the type: "Find the a path from X to Y" Which should result in a list of vertices traversed from X to Y if there is a path....
Instructions: 1. Please use only C as the language of programming. 2. Please submit the following:...
Instructions: 1. Please use only C as the language of programming. 2. Please submit the following: (1) the client and the server source files each (2) a brief Readme le that shows the usage of the program. 3. Please appropriately comment your program and name all the identifiers suitable, to enable enhanced readability of the code. Problem: Write an ftp client and an ftp server such that the client sends a request to ftp server for downloading a file. The...
Please use C language and use link list to do this program. This program should ask...
Please use C language and use link list to do this program. This program should ask user to enter two fraction polynomials. Then user chocie if he want add it or multiple it. I need you please to test it to see if it work with these two fraction polynomials 1-  Left Poly Pointer: 1/1x2 + 3/4x + 5/12 2-Right Poly Pointer: 1/1x4 – 3/7x2 + 4/9x + 2/11 AND AFTER ADDING 3- Resulting Poly Pointer: 1/1x4 + 4/7x2 + 43/36x...
PLEASE CAN YOU DO IN C LANGUAGE. A Sudoku puzzle uses a 9 × 9 grid...
PLEASE CAN YOU DO IN C LANGUAGE. A Sudoku puzzle uses a 9 × 9 grid in which each column and row, as well as each of the nine 3 × 3subgrids, must contain all of the digits 1 ⋯ 9. Figure 4.26 presents an example of a valid Sudoku puzzle. This consists of designing a multithreaded application that determines whether the solution to a Sudoku puzzle is valid. There are several different ways of multithreading this application. One suggested...
c# only please Objectives Use the more advanced data structures introduced to accomplish a difficult problem....
c# only please Objectives Use the more advanced data structures introduced to accomplish a difficult problem. Tasks This assignment has two parts: Read from a file. Write to a file. Task 1 – File Input The first piece of information we need is a text file’s name and location. Ask the user through the console what the file’s name and location are. After that, please ensure that the file does exist and at the given location. If the file does...
what are Sorted lists and their efficiency? in c++ and data structures
what are Sorted lists and their efficiency? in c++ and data structures
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT