
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
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:
- Design/Implementation: Are there noticeable issues/flaws in the design and implementation of the code? How would you remedy such issues?
- Readability: Is the code well organized and easy to follow? Can you understand what is going on, easily?
- 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.
- Efficiency: Was the code efficient, without sacrificing readability and understanding?
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by stepSolved in 3 steps with 1 images

Knowledge Booster
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
- #include <bits/stdc++.h>using namespace std;int main() { double matrix[4][3]={{2.5,3.2,6.0},{5.5, 7.5, 12.6},{11.25, 16.85, 13.45},{8.75, 35.65, 19.45}}; cout<<"Input no in first row of matrix"<<endl; for(int i=0;i<3;i++){ double t; cin>>t; matrix[0][i]=t; } cout<<"Contents of the last column in matrix"<<endl; for(int i=0;i<4;i++){ cout<<matrix[i][2]<<" "; } cout<<"Content of first row and last column element in matrix is: "<<matrix[0][3]<<endl; matrix[3][2]=13.6; cout<<"Updated matrix is :"<<endl; for(int i=0;i<4;i++){ for(int j=0;j<3;j++){ cout<<matrix[i][j]<<" "; }cout<<endl; } return 0;} Please explain this codearrow_forwardinitial c++ file/starter code: #include <vector>#include <iostream>#include <algorithm> using namespace std; // The puzzle will always have exactly 20 columnsconst int numCols = 20; // Searches the entire puzzle, but may use helper functions to implement logicvoid searchPuzzle(const char puzzle[][numCols], const string wordBank[],vector <string> &discovered, int numRows, int numWords); // Printer function that outputs a vectorvoid printVector(const vector <string> &v);// Example of one potential helper function.// bool searchPuzzleToTheRight(const char puzzle[][numCols], const string &word,// int rowStart, int colStart) int main(){int numRows, numWords; // grab the array row dimension and amount of wordscin >> numRows >> numWords;// declare a 2D arraychar puzzle[numRows][numCols];// TODO: fill the 2D array via input// read the puzzle in from the input file using cin // create a 1D array for wodsstring wordBank[numWords];// TODO:…arrow_forward#include <iostream>#include <list>#include <string>#include <cstdlib>using namespace std;int main(){int myints[] = {};list<int> l, l1 (myints, myints + sizeof(myints) / sizeof(int));list<int>::iterator it;int choice, item;while (1){cout<<"\n---------------------"<<endl;cout<<"***List Implementation***"<<endl;cout<<"\n---------------------"<<endl;cout<<"1.Insert Element at the Front"<<endl;cout<<"2.Insert Element at the End"<<endl;cout<<"3.Front Element of List"<<endl;cout<<"4.Last Element of the List"<<endl;cout<<"5.Size of the List"<<endl;cout<<"6.Display Forward List"<<endl;cout<<"7.Exit"<<endl;cout<<"Enter your Choice: ";cin>>choice;switch(choice){case 1:cout<<"Enter value to be inserted at the front: ";cin>>item;l.push_front(item);break;case 2:cout<<"Enter value to be inserted at the end:…arrow_forward
- #include<bits/stdc++.h>using namespace std;bool isPalindrome(string &s){ int start=0; int end=s.length()-1; while(start<=end) { if(s[start]!=s[end]) { return false; } start++; end--; } return true;}int main(){ string s; cout<<"ENTER STRING:"; getline(cin,s); int n=s.length(); bool flag=true; for(int i=1;i<n;i++) { string lowerHalf=s.substr(0,i); string upperHalf=s.substr(i,n-i); if(isPalindrome(lowerHalf) && isPalindrome(upperHalf)) { flag=false; cout<<"String A is:"<<lowerHalf<<"\n"; cout<<"String B is:"<<upperHalf<<"\n"; break; } } if(flag) { cout<<"NO\n"; } return 0;} change to stdio.h string.harrow_forward#include <iostream>#include <string>#include "hashT.h" using namespace std; class stateData{ friend ostream& operator<<(ostream&, const stateData&); // used to print state data on screen friend istream& operator>>(istream&, stateData&); // used to load data from file public: // setting values to the class object void setStateInfo(string sName, string sCapital, double stateArea, int yAdm, int oAdm); // retrieving data with call-by reference from the class object void getStateInfo(string& sName, string& sCapital, double& stateArea, int& yAdm, int& oAdm); string getStateName(); //return state name string getStateCapitalName(); //return state capital's name double getArea(); //return state's area int getYearOfAdmission();//return state's admision year int getOrderOfAdmission();//return state's order of admission //print stateName,…arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education