Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
#include <climits>

 

using namespace std;

 

void Input_Ballot(string[]);
void Output_Ballot(const int, int, int, string[], int[], double[]);
void Input_Votes(string[], int[], const int);
void Cal_Vote_Total(int[], int&, const int);
void Cal_Vote_Percent(int[], double[], int, const int);
void Cal_Winner(int[], int&, const int);




int main()
{
const int BALLOT_MAX = 10;
int totalVotes = 0;
int winPos = 0;
string ballot[BALLOT_MAX] = {""};
int votes[BALLOT_MAX] = {0};
double votePercent[BALLOT_MAX] = {0.0};
Input_Ballot(ballot);
Input_Votes(ballot, votes, BALLOT_MAX);
Cal_Vote_Total(votes, totalVotes, BALLOT_MAX);
Cal_Vote_Percent(votes, votePercent, totalVotes, BALLOT_MAX);
Cal_Winner(votes, winPos, BALLOT_MAX);
Output_Ballot(BALLOT_MAX, totalVotes, winPos, ballot, votes, votePercent);

 

return 0;
}

 

void Input_Ballot(string ballot[]) // checked
{
string ballotInput = " ";
int count = 0;
ifstream myfile;
myfile.open("ballot.txt");
while(myfile >> ballotInput)
{
ballot[count] = ballotInput;
count++;
}
myfile.close();
}

 

void Output_Ballot(const int BALLOT_MAX, int totalVotes, int winPos, string ballot[],int votes[], double votePercent[])
{
cout << showpoint << setprecision(2) << fixed;
cout << left << setw(10) << "Candidate" << right << " " << setw(10) << "Votes Received" << " " << setw(15) << "% of Total Votes" << endl;
for(int i = 0; i < BALLOT_MAX; i++)
{
if(ballot[i] != "")
cout << left << setw(10) << ballot[i] << right << " " << setw(10) << votes[i] << " " << setw(15) << votePercent[i] << endl;
else
{
cout << left;
cout << setw(19) << "Total" << totalVotes << endl;
cout << "The Winner of the Election is " << ballot[winPos] << "." << endl;
break;
}
}
}

 

void Input_Votes(string ballot[], int votes[], const int BALLOT_MAX) // TODO: input validation
{
for(int i = 0; i < BALLOT_MAX; i++)
{
if(ballot[i] != "")
{
while(!cin.fail())
{
cout << "Enter the number of votes received by " << ballot[i] << ": ";
cin >> votes[i];
cout << endl;
if(cin.fail())
{
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "That is not a number! Please try again." << endl << endl;
continue;
}
if(votes[i] < 0)
{
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "The number of votes cannot be less than 0. Please try again." << endl << endl;
continue;
}
else
{
break;
}
}
}
}
}

 

void Cal_Vote_Total(int votes[], int& totalVotes, const int BALLOT_MAX)
{
for(int i = 0; i < BALLOT_MAX; i++)
{
if(votes[i] != 0)
{
totalVotes += votes[i];
}
}
}

 

void Cal_Vote_Percent(int votes[], double *votePercent, int totalVotes, const int BALLOT_MAX)
{
for(int i = 0; i < BALLOT_MAX; i++)
{
if(votes[i] != 0)
{
double dot = votes[i] * 100;
votePercent[i] = dot / totalVotes;
}
else
break;
}
}

 

void Cal_Winner(int votes[], int& winPos, const int BALLOT_MAX)
{
for(int i = 0; i < BALLOT_MAX; i++)
{
if(votes[winPos] < votes[i])
winPos = i;
}
}
 
 
 
Questions:
  1. Design/Implementation: Are there noticeable issues/flaws in the design and implementation of the code? How would you remedy such issues?
  2. Readability: Is the code well organized and easy to follow? Can you understand what is going on, easily?
  3. Documentation: Is the documentation well-written? does it explain what the code is accomplishing and how? Remember good documentation includes descriptive variable and function names in addition to clear/concise annotation.
  4. Efficiency: Was the code efficient, without sacrificing readability and understanding?
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education