In: Computer Science
Please up vote ,comment if any query . Thanks for question .Be safe.
Note : check attached image for output ,code compiled and c++14 code blocks ide .
Program Plan:
Program:
#include<iostream>
#define totalCandidate 4
using namespace std;
int getTotalVotes(int voteArray[],int length);
void calculatePercentage(int voteArray[],int length,double
percentageArray[]);
void sortCandidateVotes(int voteArray[],int length,double
percentageArray[],string candidateNameArray[]);
void printVoteDetail(int voteArray[],int length,double
percentageArray[],string candidateNameArray[]);
int main()
{
string
candidateName[]={"Joe","Bill","Sue","Marry"}; //Array of string
candidate name
int candidateVotes[]= {20,30,40,35}; //Array of
integer candidate vote got
double votingPercentage[totalCandidate];
//percentage array for calculation
calculatePercentage(candidateVotes,totalCandidate,votingPercentage);//calculate
percentage
sortCandidateVotes(candidateVotes,totalCandidate,votingPercentage,candidateName);//sort
votes in ascending order using selection sort
printVoteDetail(candidateVotes,totalCandidate,votingPercentage,candidateName);
//print detail
}
//this method get vote array as argument and its size
//return integer total votes
int getTotalVotes(int voteArray[],int length)
{
int sum=0;
for(int i=0;i<length;i++) //loop from 0 to
size
{
sum+=voteArray[i];
//store in sum
}
return sum;//return sum
}
//this function takes vote array and percentage array and its
size
//return void
//calculate the percentage of vote each candidate
void calculatePercentage(int voteArray[],int length,double
percentageArray[])
{
int totalVotes=getTotalVotes(voteArray,length);
//get total votes
for(int i=0;i<length;i++) //run a loop from 0
to size
{
//vote of
candidate*100/total votes
percentageArray[i]=(double)voteArray[i]*100.0/totalVotes;
//calculate percentage
}
}
//this function takes name ,vote and percentage array its
length
//return void
//sort the vote array by selection sort and its name and percentage
too
void sortCandidateVotes(int voteArray[],int length,double
percentageArray[],string candidateNameArray[])
{
//run a loop from 0 to size -1 (4-1)
for(int i=0;i<length-1;i++)
{
int index=i; //store
index in i
for(int
j=i+1;j<length;j++) //loop from next element of i index to
length
{
if(voteArray[j]<voteArray[index]) //check index element is
greater than current array element
{
index=j; //store minimum vote index in index variable
}
}
//swap votes ,percentage
and name
int
tempVotes=voteArray[i]; //store i index votes in temp
double
percentage=percentageArray[i]; //store in perc. in temp
string
name=candidateNameArray[i]; //store name
voteArray[i]=voteArray[index]; //swap value
voteArray[index]=tempVotes;
percentageArray[i]=percentageArray[index];
percentageArray[index]=percentage;
candidateNameArray[i]=candidateNameArray[index];
candidateNameArray[index]=name;
}
}
//takes all array and return void print detail on console
void printVoteDetail(int voteArray[],int length,double
percentageArray[],string candidateNameArray[])
{
cout<<"****************Voting
Result******************"<<endl;
cout<<"Name\tvotes\tvote%"<<endl;
//print header by tab
for(int
i=0;i<length;i++)
{
//print name tab votes and percentage separated by tab
cout<<candidateNameArray[i]<<'\t'<<voteArray[i]<<'\t'<<percentageArray[i]<<"%"<<endl;
}
}
Output :
Please up vote ,comment if any changes.