New programmer here. I need to write a program using dynamic arrays to simulate an election. The program should first ask for a number of candidates, then take the candidate names and the number of votes received. Lastly, find the winner and print out the results.
I want to see if I've used the dynamic arrays correctly, and in what ways my code could be improved. I feel like I used a few too many for loops here. When I previously wrote this program, I created a candidate class to store each name and vote, however I do not believe you are able to create an object when using a dynamic array.
Any feedback or better insight on how to better use dynamic arrays is appreciated!
#include <iostream>
using namespace std;
void electionResults(double *votes, string *names, int candidates)
{
double total_votes;
int max = 0;
int winner;
// double Percentage[canidates];
// counts the total number of votes
for (int i = 0; i < candidates; i++)
{
total_votes += votes[i];
}
// determines who has the most votes
for (int i = 0; i < candidates; i++)
{
if (votes[i] > max)
{
max = votes[i];
winner = i;
}
}
// prints results
cout << "Result of the Election********" << endl;
cout << "Name of Candidate"
<< "\t"
<< "Votes Received"
<< "\t"
<< "Percentage" << endl;
for (int i = 0; i < candidates; i++)
{
cout << names[i] << "\t\t\t";
cout << votes[i] << "\t\t";
cout << (votes[i] * 100) / total_votes << "%" << endl;
}
cout << "Total Votes: " << total_votes << "\n";
for (int i = 0; i < candidates; i++)
{
if (winner == i)
{
cout << "The winner is " << names[i] << "\n";
}
}
}
int main()
{
string name;
double votes;
int Num_candidates;
// dynamic array declerations
double *voting_Array;
string *name_Array;
cout << "Enter the number of candidates\n";
cin >> Num_candidates;
voting_Array = new double[Num_candidates];
name_Array = new string[Num_candidates];
for (int i = 0; i < Num_candidates; i++)
{
cout << "Enter candidate's " << i + 1 << " last name \n";
cin >> name;
name_Array[i] = name;
}
for (int i = 0; i < Num_candidates; i++)
{
cout << "Enter votes for " << name_Array[i] << " ";
cin >> votes;
voting_Array[i] = votes;
}
electionResults(voting_Array, name_Array, Num_candidates);
voting_Array = NULL;
name_Array = NULL;
return 0;
}
There are some issues with your
electionResults()function.double total_votes;is an uninitialized variable, sototal_votes += votes[i];afterwards is undefined beehvior. Same withint winner;Also, since
votesis an array ofdoubles,maxshould also be adouble, not anint. But then, can you really have fractions of a vote? Perhapsvotesshould be an array ofints instead?That being said, your use of dynamic arrays is almost correct. You are creating, filling, and displaying the arrays correctly, but where you are going wrong is freeing the arrays. Since the arrays are allocated with
new[], you need to usedelete[]to free them, eg:Though, you really should be using
std::vectorinsted ofnew[]/delete[]directly. Letvectorhandle the dynamic memory for you.And yes, you can create objects in arrays.
Try something more like this: