Please write a code in C++, compile it and make sure there are no warnings or er
ID: 3627261 • Letter: P
Question
Please write a code in C++, compile it and make sure there are no warnings or errors. Thanks, i really appreciate it!For this assignment you will take the results of a recent election for the mayor of a small city and report the results. The city has three precincts and you can be sure that there will never be more than ten candidates running for the mayor’s position.
First you must read the results from a data file and store the values into arrays. The file has the last name of each candidate and the number of votes received in each of the three precincts on one line. There could be any number of lines (up to 10) in the file so your program will count them as it goes.
The first report should include the results in a tabular form, with the total votes for each candidate at the right side of the table, and the totals for each precinct along the bottom.
Use appropriate headings and line up the columns properly. Also print the total number of votes cast in the election.
Finally, you must print the results arranged in order from the winner down to the person with the fewest votes. This means that you will have to sort the array holding each candidate’s totals, keeping the names array in parallel. There are several sorting routines in your textbook. You may use any of these but they will need some modification. The report should include only the name of each candidate, the total votes received by that candidate and the percentage of the votes cast in the election for that candidate – arranged in order from largest to smallest. Again, print the percentages with one decimal place.
The data file is mp5election.txt. mp5election.txt data is below
Lincoln 120 300 400
Parks 100 500 250
Shakespeare 0 30 50
Ghandi 250 100 40
Ashe 300 50 175
Rodriguez 87 118 320
Curie 284 0 112
If you read your textbook, you will see that the case study at the end of the chapter has a similar problem. This can give you a starting point but your assignment needs substantive changes from that case study. Your input is different and the reports you must print out are also different. Remember to use functions to modularize the program and pass the arrays as parameters to each function. Think about when it is appropriate to make the arrays const parameters.
Explanation / Answer
please rate - thanks
message me if any problems
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void swap(string[],int,int);
void swap(int[],int,int);
void report(string [],int [],int[],int [],int ,int ,int,int [],int,int);
void sort(string [],int [],int[],int [],int [],int);
void report2(string[],int[],int,int);
int main()
{string name[10];
int pct1[10],pct2[10],pct3[10],pct1Tot=0,pct2Tot=0,pct3Tot=0,tot[10],total=0;
int i=0;
ifstream input;
input.open("mp5election.txt"); //open file
if(input.fail()) //is it ok?
{ cout<<"input file did not open please check it ";
system("pause");
return 1;
}
input>>name[i];
while(input)
{input>>pct1[i]>>pct2[i]>>pct3[i];
pct1Tot+=pct1[i];
pct2Tot+=pct2[i];
pct3Tot+=pct3[i];
tot[i]=pct1[i]+pct2[i]+pct3[i];
total+=tot[i];
i++;
input>>name[i];
}
report(name,pct1,pct2,pct3,pct1Tot,pct2Tot,pct3Tot,tot,i,total);
sort(name,pct1,pct2,pct3,tot,i);
report2(name,tot,total,i);
input.close();
system("pause");
return 0;
}
void swap(string a[],int i,int j)
{string t;
t=a[i];
a[i]=a[j];
a[j]=t;
}
void swap(int a[],int i,int j)
{int t;
t=a[i];
a[i]=a[j];
a[j]=t;
}
void sort(string name[],int pct1[],int pct2[],int pct3[],int tot[],int n)
{int i,j;
for(i=0;i<n-1;i++)
for(j=i;j<n;j++)
{if(tot[i]<tot[j])
{swap(name,i,j);
swap(pct1,i,j);
swap(pct2,i,j);
swap(pct3,i,j);
swap(tot,i,j);
}
}
}
void report2(string name[],int tot[],int total,int n)
{int i;
cout<<" Election report 2 ";
cout<<"Candidate votes percent of votes ";
for(i=0;i<n;i++)
{
cout<<left<<setw(12)<<name[i]<<setw(12)<<right<<tot[i]<<setw(12)<<
setprecision(1)<<fixed<<tot[i]*100./total<<endl;
}
}
void report(string name[],int pct1[],int pct2[],int pct3[],
int pct1Tot,int pct2Tot,int pct3Tot,int tot[],int n,int total)
{int i;
cout<<"Election report 1 ";
cout<<"Candidate precint 1 precint 2 precint 3 total ";
for(i=0;i<n;i++)
cout<<left<<setw(12)<<name[i]<<setw(12)<<right<<pct1[i]<<setw(12)
<<pct2[i]<<setw(12)<<pct3[i]<<setw(12)<<tot[i]<<endl;
cout<<left<<setw(12)<<"Totals"<<setw(12)<<right<<pct1Tot<<setw(12)
<<pct2Tot<<setw(12)<<pct3Tot<<setw(12)<<total<<endl;
}