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

Could you modify the code make output arranged into 5x5, like this: 1 2 3 4...
Could you modify the code make output arranged into 5x5, like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 The code is: import java.util.*; public class RandomGen { public static void main(String[] args) { int[][] ArrayRand = new int [5][5]; Random rand = new Random(); int sum = 0; System.out.println("The generated random matris is: ");    for(int i = 0; i <...
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...
21. What is the output of the following segment of code if 7 is input by...
21. What is the output of the following segment of code if 7 is input by the user when asked to enter a number?____________ int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 7: total = total + 3; case 8: total = total + 6; default: total = total + 4; } cout...
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...
C Code Edit this code to make it output all unique letters IN LOWERCASE of an...
C Code Edit this code to make it output all unique letters IN LOWERCASE of an input file. For example, with this input: Raspberry Grapefruit Straw berry raspBERRY Blue$$berry apple Pine_apple raspberry The output should be: raspberry grapefruit straw berry blue berry apple pine NOTE: Words with different capitlization, like raspberry and raspBERRY count as 1 unique word, so raspberry should only be seen once in the output. #include <stdio.h> #include <stdlib.h> int main() {     //Pointer to access the...
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;
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT