Hi Tricia,
largest
is the number of votes, so on line 69 you go out of bounds with the array with this part :
candidate[largest]
A couple of other things:
Avoid using magic numbers like 5 in your code, make them
const
variables instead:
const int ArraySize = 5;
Then use
ArraySize
throughout your code. That way, if you want to change the number of candidates, you can do it in one place only.
Always initialise your variables to something when you declare them, even if they get assigned to again later (like
count
). Having uninitialised variables is very often a huge source of problems.
This:
sum = sum + votes[count];
can be written more concisely as:
sum += votes[count];
This comment might seem pedantic, but one should always be wary of potential problems. So check for divide by zero, before doing this part:
percent[count]= votes[count]/ sum;
I know you just read in all the votes and calculated the sum just prior to this, but it is prudent to do a check regardless. Also it is a very good idea to check that reading the input actually worked. It is all about making the code as bullet proof as reasonably possible.
With your variable names, don't be afraid to make them longer if it adds more meaning, I might have had:
SumOfVotes
,
LargestVote
,
CandidateName
,
CandidateVotes
VotePerCentage
I imagine that in your future assignments, you will learn how to group these related variables together into a C style
struct
or a C++
class
The following might be too advanced for the stage you are at in your class - your teacher will realise you had help if you implement this, but I am mentioning it to help you out in the future.
It is a good idea to provide comments to say what the valid ranges of your variables are, and to do validation code to prove that they are before you use the values.
For example the number, largest & sum of the votes should always be positive, and if you knew there were 100,000 voters in an electorate, then the sum shouldn't be bigger than that, Percentages should add to 100.0. The last 2 are called post conditions, while the others are pre conditions.
Some checking can be enforced by the type of your variables: votes, largest and sum could be
unsigned int
.
I can see why you made votes
double
- because of the percentage calc has to be
double
. There is another way, it is possible to cast the value of a variable to another type, here is the C version which is easy to understand:
|
VotePerCentage[count]= CandidateVotes[count]/ (double)SumOfVotes;
|
The C++ version which is better, looks like this:
|
VotePerCentage[count]= CandidateVotes[count] / static_cast<double>(SumOfVotes);
|
Do the divide by zero check before casting to double, because comparing doubles to zero is not as easy as what one might expect.
Finally, avoid line 21: it brings in the entire standard namepace, which has thousands of variables & functions in it. The purpose of namspaces is to avoid clashes with variable & function names, but bringing in the whole thing actually increases the risk of clashes!! For example the
std
namespace
has left, right, distance variables, so if you had this:
1 2 3
|
using namespace std;
double distance = 0.0; // immediate error std::distance is a very different thing
|
So the answer there is to pefix
std
things with
std::
, as in
std::cout
Really one should put their own variables & functions & classes into their own namespace, or more than one if it is warranted.
There you go - plenty to think about!! Hope all goes well, any questions - just ask :+D