144k views
5 votes
write a c++program that allows the user to enter the last names of fivecandidates in a local election and the number of votes received byeach candidate. The program should then output each candidate'sname, the number of votes recieved, and the percentage of the totalvotes received by the candidate. Your program should also outputthe winner of the election.

1 Answer

5 votes

Answer:

#include<iostream>

#include<stdlib.h>

using namespace std;

int main(){

//initialization

string str1[5];

int arr_Vote[5];

int Total_Vote=0,max1_Index;

int max1=INT_MIN;

//loop for storing input enter by user

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

cout<<"Enter the last name of candidate: "<<endl;

cin>>str1[i];

cout<<"Enter the number of vote receive by the candidate: "<<endl;

cin>>arr_Vote[i];

//calculate the sum

Total_Vote = Total_Vote + arr_Vote[i];

//find the index of maximum vote

if(arr_Vote[i]>max1){

max1=arr_Vote[i];

max1_Index=i;

}

}

//display

cout<<"\\The output is......"<<endl;

//loop for display data of each candidate

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

cout<<str1[i]<<"\t"<<arr_Vote[i]<<"\t" <<float(arr_Vote[i]*100)/Total_Vote<<"%"<<endl;

}

//display winner

cout<<"The winner candidate is"<<endl;

cout<<str1[max1_Index]<<"\t"<<arr_Vote[max1_Index]<<"\t"<<float(arr_Vote[max1_Index]*100)/Total_Vote<<" %"<<endl;

}

Step-by-step explanation:

Create the main function and declare the two arrays and variables.

the first array for storing the names, so it must be a string type. second array stores the votes, so it must be int type.

after that, take a for loop and it runs for 5 times and storing the values entered by the user in the arrays.

we also calculate the sum by adding the array data one by one.

In the same loop, we also find the index of the maximum vote. take an if statement and it checks the element in the array is greater than the max1 variable. Max1 variable contains the very small value INT_MIN.

If condition true, update the max1 value and update the max1_Index value.

The above process continues for each candidate for 5 times.

After that, take a for loop and display the data along with percentage by using the formula:


Vote\,percent=(Vote\,receive*100)/(Total\,vote)

the, finally display the winner by using the max1_Index value.

User Freek Buurman
by
8.0k points