Question

In: Computer Science

Modify the following code to make the input and output look like this. Input 5 Vader...

Modify the following code to make the input and output look like this.

Input

5
Vader 1300
Kirk 1250
Adama 1000
Reynolds 1615
Oneill 1470

Output

Enter the number of candidates: 5
Enter candidate's name :Vader 1300
Enter votes received :Enter candidate's name :Kirk 1250
Enter votes received :Enter candidate's name :Adama 1000
Enter votes received :Enter candidate's name :Reynolds 1615
Enter votes received :Enter candidate's name :Oneill 1470
Enter votes received :
Name                Votes               Percentage          
Vader               1300.00             19.59%
Kirk                1250.00             18.84%
Adama               1000.00             15.07%
Reynolds            1615.00             24.34%
Oneill              1470.00             22.16%
Total               6635                
The Winner of the Election is Reynolds

//Abduljabbr sale11/8/2019//

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

int main()
{
int size;
cout<<"Enter the number of candidates: ";
cin>>size;
string *names= new string[size];
double *votes=new double[size];
double sum=0;
double winner=-1;
string winnerName="";
int total=0;
//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;
}

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;
}



Related Solutions

modify code to write the output as an HTML table to a file in the output...
modify code to write the output as an HTML table to a file in the output directory. The file that is saying to work at : SOURCE CODE IN PERL: print "Enter principal amount: "; $P=; while($P<=0) { print "Principal must be positive. Try again: "; $P=; } print "Enter number of times interest is applied in a year: "; $n=; while($n!=12 && $n!=4 && $n!=2 && $n!=1) { print "It must be 12, 4, 2 or 1. Try again:...
look this code is a correct but i want modify it to allow the client to...
look this code is a correct but i want modify it to allow the client to have three attempts to login to the server package hw2; import java.net.*; import java.util.Formatter; import java.util.Random; import java.util.Scanner; import java.io.*; public class Client {    Socket server;    int port;    Formatter toNet = null;    Scanner fromNet = null;    Scanner fromUser = new Scanner(System.in);    public Client() {        try {            // login at server at local host...
please use linux or unix to complete, and include pictures of the output. Modify the code...
please use linux or unix to complete, and include pictures of the output. Modify the code below to implement the program that will sum up 1000 numbers using 5 threads. 1st thread will sum up numbers from 1-200 2nd thread will sum up numbers from 201 - 400 ... 5th thread will sum up numbers from 801 - 1000 Make main thread wait for other threads to finish execution and sum up all the results. Display the total to the...
In Coral Code Please!!!! The assignment is to get an integer from input, and output that...
In Coral Code Please!!!! The assignment is to get an integer from input, and output that integer squared, ending with newline. (Note: This assignment is configured to have students programming directly in the zyBook. Instructors may instead require students to upload a file). Below is a program that's been nearly completed for you. Click "Run program". The output is wrong. Sometimes a program lacking input will produce wrong output (as in this case), or no output. Remember to always pre-enter...
Java: modify the given example code to make it possible to create a student object by...
Java: modify the given example code to make it possible to create a student object by only specifying the name, all other info may be absent. it may also be possible to add a tag with an absent value. use OPTIONAL TYPE and NULL object design pattern.   import java.util.HashMap; import java.util.Map; public class Student { private final String aName; private String aGender; private int aAge; private Country aCountry; private Map aTags = new HashMap<>(); public Student(String pName) { aName =...
Java: modify the given example code to make it possible to create a student object by...
Java: modify the given example code to make it possible to create a student object by only specifying the name, all other info may be absent. it may also be possible to add a tag with an absent value. import java.util.HashMap; import java.util.Map; public class Student { private final String aName; private String aGender; private int aAge; private Country aCountry; private Map<String, String> aTags = new HashMap<>(); public Song(String pName) { aName = pName; } public String getName() { return...
After the code executes (in C++), what will the linked list 8->4->3->7->5 look like? (8 is...
After the code executes (in C++), what will the linked list 8->4->3->7->5 look like? (8 is the head) 1a) Node* newNode = new Node(26); newNode->next = head->next; head = newNode; 1b) Node* curNode = head; for(int i=0; i<2; i++) curNode = curNode->next; Node* temp = curNode->next; curNode->next = curNode->next ->next; delete temp; temp = nullptr;
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
How to print these methods in a separate main class? need the output to look like...
How to print these methods in a separate main class? need the output to look like this: Enter three integers whose GCD is to be found -> Enter an integer n to find the nth Fibonacci number -> Enter the base and exponent , an integer, of a power -> Enter two positive integers 1 and j where i < j -> gcd() = fib() = (a number ^ a number) = a number There are ___ palindromic numbers between...
c# code working but output not right, I need to output all numbers like : Prime...
c# code working but output not right, I need to output all numbers like : Prime factors of 4 are: 2 x 2 here is just 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 here is just 2,5 Prime factors of 50 are: 2 x 5 x 5 here is just 2,5 1) How I can fix it 2)I...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT