Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Help with strings & doubles using Dynamic Arrays in C++ (http://www.go4expert.com/forums/help-strings-doubles-using-dynamic-t21884/)

tdeyoung 25Apr2010 08:31

Help with strings & doubles using Dynamic Arrays in C++
 
I am new to the programming community and I am having difficulty with one function in my program. We are suppose to have the user enter the number of candidates which will set my array size. They then enter the candidates name and the number of votes the candidate received. My program then takes that information and adds up the percentages and prints them out with the corresponding information. My program is also suppose to declare the winner. I can get the largest number of votes, but I can't get the name to print out with it. That is where I am lost. Can anyone help me with this? Here is my program so far:

Code:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void percentOfVotes (string *candidate, int arraySize, double *votes, double *percent, double sum);
void printElectionTable(string *candidate, int arraySize, double *votes, double *percent, double sum);
void maxVotes (int arraySize, string *candidate, double *votes);
int main()
{
    string *candidate;    //the * takes the place of the array bracket on the end
    int arraySize;        //which makes it a pointer.  Pointers are arrays and arrays are pointers.
    double *votes;
    double sum = 0;
    double *percent;
   
    cout << "Please enter the Number of Candidates. ";
    cin >> arraySize;
    cout << endl;
   
    candidate = new string[arraySize];  //builds the array we asked for
    votes = new double[arraySize];      //assigns the arraySize we asked for to our variables.
    percent = new double[arraySize];
   
   
    for (int index = 0; index < arraySize; index++)
    {
      cout << "Enter the Last Name of the Candidate: ";
        cin >> candidate[index];
       
        cout << "Enter the Votes " << candidate[index] << " " << "received: ";
        cin >> votes[index];
       
        sum = sum + votes[index];
        cout << endl;
    }
   
    percentOfVotes(candidate, arraySize, votes, percent, sum);
    maxVotes (arraySize, candidate, votes);
           
system("pause");
return 0;
}
void percentOfVotes (string *candidate, int arraySize, double *votes, double *percent, double sum)
{
   
   
    for (int index = 0; index < arraySize; index++)
    {
        percent[index] = ((votes[index] / sum) * 100);
       
        cout << fixed << showpoint << setprecision(2);
        cout << candidate[index] << " " << votes[index] << " ";
        cout << "% Received " << percent[index] << endl;
    }     
}
void maxVotes (int arraySize, string *candidate, double *votes)
{
   
    int maxIndex = 0;
    double maxIndex;

    for (int index = 0; index < arraySize; index++)
    {
        if (votes[maxIndex] < votes[index])
            maxIndex = index;
        maxVotes = votes[maxIndex];
    }

    //These are some of the things I tried
//    for (int index = 0; index < arraySize; index++)
//    {
//        if (candidate[index][votes])
//            maxVotes = index;
//        candidate[index] = maxVotes;
//    }
//    for (int index = 0; index < arraySize; index++)
//    {
//      votes[maxIndex] = candidate[index];
//    if (candidate[index] = maxVotes)
       
          cout << "The Winner of the Election is:  " << *candidate << votes[maxIndex] << endl;
}   
       
//void printElectionTable (string *candidate, int arraySize, double *votes, double *percent, double sum)


ThorAsgard 26Apr2010 01:04

Re: Help with strings & doubles using Dynamic Arrays in C++
 
Hi tdeyoung,

I am new here and to C++ as well
I looked at your code and found this works for me


Code:


#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
 
void percentOfVotes (string *candidate, int arraySize, double *votes, double *percent, double sum);
void maxVotes (int arraySize, string *candidate, double *votes);
 
int main()
{
string *candidate; //the * takes the place of the array bracket on the end
int arraySize; //which makes it a pointer. Pointers are arrays and arrays are pointers.
double *votes;
double sum = 0;
double *percent;
 
cout << "Please enter the Number of Candidates. ";
cin >> arraySize;
cout << endl;
 
candidate = new string[arraySize]; //builds the array we asked for
votes = new double[arraySize]; //assigns the arraySize we asked for to our variables.
percent = new double[arraySize];
 
 
for (int index = 0; index < arraySize; index++)
{
cout << "Enter the Fisrt Name of the Candidate: ";
cin >> candidate[index];
 
cout << "Enter the Votes " << candidate[index] << " " << "received: ";
cin >> votes[index];
 
sum = sum + votes[index];
cout << endl;
}
 
percentOfVotes(candidate, arraySize, votes, percent, sum);
maxVotes (arraySize, candidate, votes);
 
return 0;
}
void percentOfVotes (string *candidate, int arraySize, double *votes, double *percent, double sum)
{
 
 
for (int index = 0; index < arraySize; index++)
{
percent[index] = ((votes[index] / sum) * 100);
 
cout << fixed << showpoint << setprecision(2);
cout << candidate[index] << " " << votes[index] << " ";
cout << "% Received " << percent[index] << endl;
}
}
void maxVotes (int arraySize, string *candidate, double *votes)
{
 
int maxIndex = 0;
double maxVotes;
for (int index = 0; index < arraySize; index++)
{
if (votes[maxIndex] < votes[index])
maxIndex = index;
maxVotes = votes[maxIndex];
}
 
cout << "The Winner of the Election is: " << candidate[maxIndex] << " with " << votes[maxIndex] << " votes" << endl;
}

The only change i have made is in the maxVotes routine
This is my first time helping some one so i hope i have got it correct

Thor :)

tdeyoung 1May2010 02:38

Re: Help with strings & doubles using Dynamic Arrays in C++
 
Thor

I did get it and I appreciate your help. I realized after I sent it, that I was one word off which is usually the case. Sometimes you just need an extra pair of eyes:)


All times are GMT +5.5. The time now is 00:49.