Question:
c++ election program help?
1970-01-01 00:00:00 UTC
c++ election program help?
Three answers:
Damasta AM inductee
2008-07-07 11:17:49 UTC
Easy, try this skeleton



#include



using namespace std;



int main()

{

int n;

cout<<"Enter the number of candidates";

if (n>=2 and n<=10)

{

int candidates[n];

for(....)

{

.....

}

}

else if(n<2)

{

cout<<"Too few candidates";

}

else

{

cout<<"Too many candidates";

}

cin.get();

return 0;

}



All arrays are are continuous variables of the same type in memory. Think of it as mailboxes. you declare an int example[10] in memory it looks like this



example[0]

example[1]

example[2]

example[3]

example[4]

example[5]

example[6]

example[7]

example[8]

example[9]



It starts at 0 because if you just cout<
2008-07-07 13:30:09 UTC
>can someone please explain arrays in a simplified way for me please.



Arrays are sets of values that are indexed by number. For example, in C++ you can have a single integer:



int mynumber;



To make an array, you use square brackets and the number of items in the array. For example, this:



int mynumbers[5];



would make an array of five integers (0 to 4 inclusive) which can each be different values and which can be referenced by number. The elements of the array are accessed the same way as any other variable, for example if you made a function that included the following line:



int mynumbers[3]=17;



that would set the fourth item in the mynumbers array to be equal to 17. You can also access the elements dynamically, for example this code:



int x=1+2;

int mynumbers[x]=17;



does exactly the same thing as the code above. Obviously in this case it would simply do the same thing and take longer doing it, but you can see how this could be expanded on to make it easier to write more complex behavior into your program. As mentioned above, the array is numbered from 0 to whatever number is one less than the number of items in the array (for example an array that goes 0, 1, 2 has three items, but only goes up to 2 because it starts at 0). You can also make arrays out of other things besides ints, like this:



float myfloats[7];

char mycharacters[19];

string mystrings[2];

bool mybooleans[53];



If your program has any structures or classes, you can even make arrays of them the same way. In addition, with some kinds of arrays, you can also assign values to the array elements during initialization. For example if you had the following code:



int mynumbers[3] = {5,7,12};



that would make an array of three integers and also assign the integers values of 5, 7 and 12, in order. So mynumbers[0] would be 5, mynumbers[2] would be 12, you get the idea. You don't need to enter values in the array directly on initialization, but it is advisable to assign any given value in the array before trying to access that value for any other purpose (failure to do this might cause odd and unwanted behavior). Finally, you can even create multidimensional arrays, like this:



int mynumbers[3][4];



This creates an array of three arrays, each of which contains four numbers. You access these the same way, for example mynumbers[2][0] would be one number, mynumbers[2][1] would be another number, and mynumbers[1][2] would be yet another number. The above example is a two-dimensional array, but as far as I know there is essentially no limit on the number of dimensions, so something like int numbers[5][5][5][5][5] would also be possible, and could contain up to 3125 different numbers. However, it is NOT possible to have an array that contains different types of variables, nor is it possible to change the size of an array on the fly, nor is it possible to have a multidimensional array that is not 'rectangular' (i.e. with subarrays of the same dimension that hold different numbers of variables). If you really need this kind of functionality, you will need to move to structures and classes, and even then it won't be easy going.



In the case of writing the program you have described, you will need to start by initializing one array of integers (to count the votes) and one array of strings (to store the names), both with a size of at least 10. Have the user input the names and votes for each candidate. In the meantime, add up the total number of votes in another variable. When this is done, have the program go back through the array, dividing each candidate's votes by the total number of votes (you're going to have to use floating point arithmetic to get an accurate result out of this) and multiply that by 100 to get the percentage, then display that. This loop should also check each candidate to see if their number of votes is higher than the current highest number of votes, so that by the time you get to the end you know which candidate has the highest number of votes and declare them the winner.
2008-07-07 11:56:45 UTC
okaay, you'll need 2 arrays. One will hold the names of the candidates that is, string array, and the other will hold their votes so it's of type integer



first you get the number of candidates :

int n = 0;

do

{

cout<<"\nEnter the number of candidates [2-10] : ";

cin>>n;

} while(n<2 || n>10)



after getting the number of of candidates n, and performing the basic test to it to ensure it is within 2 and 10, you need to define the containers that will hold the data :



string names[n];

int votes[n];



these two lines defines the required two arrays.Also, for the votes percentage purpose you'll need the total number of votes. so we'll define a variable for this task and its value will increase in the entering data step:



int totalVotes = 0;



The next step is to ask the user to enter the required data one by one. each record consists of two fields : name and votes :



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

{

cout<<"\nCandidate number "<< (i+1) ;

// get the candidate name

cout<<"\nEnter the name :";

cin>> names[i];



// get the candidate votes

cout<<"\nEnter the number of votes :";

cin>> votes[i];



// add that number of votes to the total number of votes

totalVotes = totalVotes + votes[i];

}



now you know how to declare arrays and to assign values to it's elements. Next we'll see how to retrieve the values from these arrays and make use of it like this :



cout<<"\n***************************************************\n";

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

{

cout<<"\nCandidate number "<< (i+1) ;

// output candidate's name

cout<<"\nName : " << names[i];



// output the candidate's percentage votes

cout<<"\tPercentage votes : "<< votes[i] / totalVotes * 100;

}



finally to output the winner, we'll sort the votes array and get the maximum element of it like this :



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

for (int j = i; j < n; j++)

if ( votes[i] < votes[j] )

{

// sawp the votes array elements

int tempVotes = votes[i];

votes[i] = votes[j];

votes[j] = tempVotes ;



// you need to make the same changes to the names array to keep the correspondence between names and votes



string tempName = names[i];

names[i] = names[j];

names[j] = tempName;



} // enf if, for j, for i



// now votes array is sorted in descending order, the max is the first



cout<< "\n\nThe Winner is : "<
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...