Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <string> #include <fstream> #include <iomanip> usin

ID: 3634740 • Letter: #

Question

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

struct candidate
{
string name;
int region1, region2, region3, region4;
int total;
};


int main()
{
candidate candidate[5];
int i;
int highvotes = 0;
int totalvotes = 0;
int winner = 0 ;
string temp;


ifstream infile;
ofstream outfile;


infile.open("sructure.txt");
outfile.open("sructure.out");


outfile<<endl;
outfile<<" -------------Election Results---------------"<<endl;
outfile<<"Candidate Votes"<<endl;
outfile<<"Name Region1 Region2 Regoion3 Region4 Total"<<endl;
outfile<<"---- ------- ------- -------- ------- -----"<<endl;


for (i=0; i<5; i++)

infile>>candidate[i].name>>candidate[i].region1>>candidate[i].region2>>candidate[i].region3>>candidate[i].region4;

for (i=0; i<5; i++)
{

candidate[i].total= candidate[i].region1+ candidate[i].region2+candidate[i].region3+candidate[i].region4;
}

for (i=0; i<5; i++)
{
if(highvotes < candidate[i].total)
{
highvotes = candidate[i].total;
winner = i;
}
totalvotes += candidate[i].total;

outfile<<left<<setw(8)<<candidate[i].name;
outfile<<right<<setw(8)<<candidate[i].region1<<right<<setw(8)<<candidate[i].region2<<right<<setw(12)<<candidate[i].region3;
outfile<<right<<setw(12)<<candidate[i].region4;
outfile<<right<<setw(12)<<candidate[i].total<<endl;
}
outfile<<"winner: " <<candidate[winner].name<<setw(8)<<" votes received: "<< candidate[winner].total<<endl;

infile.close();
outfile.close();

return 0;
}

Please modify the program according to the below instructions

(You must use at least three parallel functions to complete it. The first is to compute the total votes for each candidate. Second is to determine the winner of the election and by how many votes. The third is to compute the grand total votes for all the candidates. At your discretion use additional functions as many as you wish.)

Explanation / Answer

Please Rate:Thanks


This code will help you

#include<iostream>

#include<string>

#include<fstream>

#include<iomanip>

using namespace std;

struct candName

{

string name;

int reg1, reg2, reg3, reg4;

int total;

};

int candidateTotal(candName cand);

int findWinner(candName cand[]);

int findTotal(candName cand[]);

int main()

{

       candName cand[5];

       int i;

       int highestvotes=0;

       int totalvotes=0;

       int winner=0;

       string temp;

       bool swap;

       int temp_reg1;

       int temp_reg2;

       int temp_reg3;

       int temp_reg4;

       ifstream inputFile;

       ofstream outputFile;

       inputFile.open("candreg.txt");

       outputFile.open("parellel.out");

       outputFile<<endl;

       outputFile<<" --------------Election Results--------------"<<endl;

       outputFile<<endl;

       outputFile<<"Candidate Votes"<<endl;

       outputFile<<"Name Region1 Region2 Region3 Region4 Total"<<endl;

       outputFile<<"-------- ------- ------- ------- ------- ------"<<endl;

       for (i=0; i<5; i++)

       {

              inputFile>> cand[i].name >>cand[i].reg1 >>cand[i].reg2 >>cand[i].reg3 >>cand[i].reg4;

              cand[i].total = candidateTotal(cand[i]);

       }

       do

       {

              swap = false;

              for(int i=0;i<(5-1);i++)

              {

              if(cand[i].name > cand[i+1].name)

              {

              temp = cand[i].name;

              cand[i].name = cand[i+1].name;

              cand[i+1].name = temp;

              swap = true;

              }

              if(cand[i].reg1 > cand[i+1].reg1)

              {

              temp_reg1 = cand[i].reg1;

              cand[i].reg1 = cand[i+1].reg1;

              cand[i+1].reg1 = temp_reg1;

              swap = true;

              }

              if(cand[i].reg2 > cand[i+1].reg2)

              {

              temp_reg2 = cand[i].reg2;

              cand[i].reg2 = cand[i+1].reg2;

              cand[i+1].reg2 = temp_reg2;

              swap = true;

              }

              if(cand[i].reg3 > cand[i+1].reg3)

              {

              temp_reg3 = cand[i].reg3;

              cand[i].reg3 = cand[i+1].reg3;

              cand[i+1].reg3 = temp_reg3;

              swap = true;

              }

              if(cand[i].reg4 > cand[i+1].reg4)

              {

              temp_reg4 = cand[i].reg4;

              cand[i].reg4 = cand[i+1].reg4;

              cand[i+1].reg4 = temp_reg4;

              swap = true;

              }

              }

       }while (swap);

       winner=findWinner(cand);

       totalvotes =findTotal(cand);

       for (i =0; i< 5; i++)

       {

              outputFile<<setw(8)<<cand[i].name;

              outputFile<<setw(8)<<cand[i].reg1 <<setw(8)<< cand[i].reg2<<setw(12) << cand[i].reg3 <<setw(12);

              outputFile<<setw(12)<< cand[i].reg4;

              outputFile<<setw(8)<<cand[i].total<<endl;

       }

       outputFile<<"Winner: "<<cand[winner].name<<setw(8)<<",Votes Received: "<< cand[winner].total<<endl;

       outputFile<<"Total votes polled: "<<totalvotes<<endl;

       inputFile.close();

       outputFile.close();

       return 0;

}

int candidateTotal(candName cand)

{

       int total;

       total=cand.reg1 + cand.reg2 + cand.reg3 + cand.reg4;

       return total;

}

int findWinner(candName cand[])

{

       int winner;

       int highestvotes=0;

       for (int i =0; i< 5; i++)

       {

              if(highestvotes < cand[i].total)

              {

              highestvotes = cand[i].total;

              winner =i;

              }

       }

       return winner;

}

int findTotal(candName cand[])

{

       int totalvotes = 0;

       for (int i =0; i< 5; i++)

              totalvotes += cand[i].total;

       return totalvotes;

}