PROGRAM:
//COUNTING OF VOTES PROGRAM
#include<iostream.h>
#include<string.h>
#include<conio.h>
class election //Defining class
{
public:
int iv,n,x,temp,contest[20],z,i,j,votes[20]; //declaring data members
char name[20][20],temp2[20][20];
public:
void getdata() //defining function for getting basic data
{
cout<<"Enter the total number of candidates\t:\t";
cin>>n; //getting total number of candidates
cout<<"\nEnter the names of the candidates\n";
for(i=1;i<=n;i++) //getting the names of the candidates using loop
{
cin>>name[i];
contest[i]=0;
}
}
void elect() //defining elect function
{
cout<<"Enter the total number of votes polled\t:\t";
cin>>x; //getting total number of votes polled
cout<<"Enter the votes\n";
z=0;
iv=0;
for(i=0;i<x;i++)
{
cin>>votes[i]; //getting the votes
for(j=1;j<=n;j++)
{
if(j==votes[i]) //checking the votes
{
z=votes[i];
}
}
if(z!=0) //using a flag varirable
{
contest[z]++;
z=0;
}
else
{
iv++; //incrementing for invalid votes
}
}
}
void result() //definition for result function
{
temp=0;
for(i=1;i<=n;i++) //sorting the votes in descending order
{
for(j=i+1;j<=n;j++)
{
if(contest[i]<contest[j])
{
temp=contest[j]; //using a temporary variable to swap
contest[j]=contest[i];
contest[i]=temp;
strcpy(temp2[i],name[j]);
strcpy(name[j],name[i]);
strcpy(name[i],temp2[i]);
}
}
}
}
void display() //definition for display function
{
cout<<"\n\nTotal number of votes polled="<<x<<"\n";
cout<<"\n\nTotal number of valid votes="<<x-iv<<"\n";
cout<<"\n\nTotal number of invalid votes="<<iv<<"\n";
cout<<"\n\nThe result is \n";
for(i=1;i<=n;i++) //announcing the winner
{
cout<<name[i]<<"\twith\t"<<contest[i]<<"\tvotes\n";
}
i=1;
cout<<"\n\nTHE WINNER IS "<<name[i]<<"!!!!!\n\n"<<"CONGRAJULATIONS!!!";
}
}t; //object creation
int main()
{
clrscr();
t.getdata(); //calling getdata function
t.elect(); //calling elect function
t.result(); //calling result function
t.display(); //calling display function
return 0;
}
OUTPUT:
Enter the total number of candidates : 5
Enter the names of the candidates
harry
ron
ginny
snape
fred
Enter the total number of votes polled : 15
Enter the votes
1
1
2
2
2
3
3
3
3
4
4
4
64
5
Total number of votes polled=15
Total number of valid votes=14
Total number of invalid votes=1
The result is
ginny with 4 votes
ron with 3 votes
snape with 3 votes
harry with 2 votes
fred with 2 votes
THE WINNER IS ginny!!!!!
CONGRAJULATIONS!!!
No comments:
Post a Comment