Question:
Write a C++ program to read, store, count, and sort the number of votes received by N candidates in a local?
1970-01-01 00:00:00 UTC
Write a C++ program to read, store, count, and sort the number of votes received by N candidates in a local?
Three answers:
2009-05-13 11:19:23 UTC
I agree that making use of C++ libraries is the best way, such as vector. However, given that this is a school project (or looks like it), pretty much C can be used.



I'm not going to write this entire program for you; it's far too long for someone to answer comfortably (just telling you for future reference; you might want to split it into parts)
Ngo Cong Huan
2009-05-13 11:13:14 UTC
1. I should define a class

typedef class Student{

public:

static int totalVotes;

int vote;

};

2. You should learn about template vector<> in C++ standard libray for store dynamically N candicates. The size of vector is automaticall raised up.

3. You should write for your self a sorting function based on buble sort,quick sort,insertion sort, or whatever sort kind you knew. Or studying function sort() in in C++ Standard Library.



if you can do it,you will feel you're getting better,confident,and enjoying with you've done.
rk
2009-05-13 20:24:38 UTC
This program, i feel , meets all your needs.







#include

#include

int n=8;

int votes[8];

void initialise()

{

int i;

for (i=0;i
votes[i]=0;

}

void read_store()

{

int i;

for (i=0;i
{

cout<<"enter votes for candidate "<
cin>>votes[i];

}



}

void show_winner()

{

int max, index,i;

max = votes[0];

index =0;

for (i=1;i<8;i++)

{

if (max
{

max=votes[i];

index = i;

}

}

cout<<"winner is "<<"candidate "<
}

void show_votes()

{

}

void show_percentage()

{

float sum,percent;

int i;

sum =0;

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

sum += votes[i];

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

cout<<"candidate "<
j=1;

}

}

if (j==0)

cout<<" No such Votes obtained by any candidate"<
}



void main()

{

int m;

clrscr();

initialise();

read_store();

show_winner();

show_votes();

show_percentage();

sort_show();

cout<<"enter votes obtained to search for";

cin>>m;

search(m);

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...