Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lin Chen 09/06/2011. Global variables const int maxCandidates = 10; // Names of the candidates participating in this state's primary string candidate[maxCandidates];

Similar presentations


Presentation on theme: "Lin Chen 09/06/2011. Global variables const int maxCandidates = 10; // Names of the candidates participating in this state's primary string candidate[maxCandidates];"— Presentation transcript:

1 Lin Chen 09/06/2011

2 Global variables const int maxCandidates = 10; // Names of the candidates participating in this state's primary string candidate[maxCandidates]; // Names of all candidates participating in the national election std::string candidateNames[maxCandidates]; // How many delegates are assigned to the state being processed int delegatesForThisState; // How many delgates have been won by each candidate int delegatesWon[maxCandidates]; // How many candidates in the national election? int nCandidates;

3 Global variables // How many candidates in the primary for the state being processed int nCandidatesInPrimary; // How many states participate in the election int nStates; // How many delegates in the election (over all states) int totalDelegates = 0; // How many votes were cast in the primary for this state int totalVotes; // How many votes wone by each candiate in this state's primary int votesForCandidate[maxCandidates]; int findCandidate (std::string name);

4 int main(int argc, char** argv) { readCandidates(); int nStates; cin >> nStates; for (int i = 0; i < nStates; ++i) { readState(); assignDelegatesToCandidates(); } for (int i = 0; i < nCandidates; ++i) { printCandidateReport(i); } return 0; }

5 void readCandidates () { cin >> nCandidates; string line; getline (cin, line); for (int i = 0; i < nCandidates; ++i) { getline (cin, candidateNames[i]); delegatesWon[i] = 0; } Read how many candidates Skip “enter” Read candidates’ name and Initialize the number of winning delegates to be zero

6 int main(int argc, char** argv) { readCandidates(); int nStates; cin >> nStates; for (int i = 0; i < nStates; ++i) { readState(); assignDelegatesToCandidates(); } for (int i = 0; i < nCandidates; ++i) { printCandidateReport(i); } return 0; } Read how many states

7 int main(int argc, char** argv) { readCandidates(); int nStates; cin >> nStates; for (int i = 0; i < nStates; ++i) { readState(); assignDelegatesToCandidates(); } for (int i = 0; i < nCandidates; ++i) { printCandidateReport(i); } return 0; }

8 int main(int argc, char** argv) { readCandidates(); int nStates; cin >> nStates; for (int i = 0; i < nStates; ++i) { readState(); assignDelegatesToCandidates(); } for (int i = 0; i < nCandidates; ++i) { printCandidateReport(i); } return 0; }

9 void readState () { totalVotes = 0; cin >> nCandidatesInPrimary >> delegatesForThisState; totalDelegates += delegatesForThisState; // "x += y" is a shorthand for "x = x + y" string word, line; getline (cin, line); for (int i = 0; i < nCandidatesInPrimary; ++i) { cin >> votesForCandidate[i]; totalVotes = totalVotes + votesForCandidate[i]; cin >> word; getline (cin, line); candidate[i] = word + line; } Read candidate number and delegate number in this state Read vote number for each candidate Read candidate name for each candidate

10 int main(int argc, char** argv) { readCandidates(); int nStates; cin >> nStates; for (int i = 0; i < nStates; ++i) { readState(); assignDelegatesToCandidates(); } for (int i = 0; i < nCandidates; ++i) { printCandidateReport(i); } return 0; }

11 int assignDelegatesToCandidates () { int remainingDelegates = delegatesForThisState; for (int i = 0; i < nCandidatesInPrimary; ++i) { int candidateNum = findCandidate(candidate[i]); int nDel = (delegatesForThisState * votesForCandidate[i] + (totalVotes- 1)) / totalVotes; if (nDel > remainingDelegates) nDel = remainingDelegates; delegatesWon[candidateNum] += nDel; remainingDelegates -= nDel; } Find index in the array Calculate win number and round up Accumulate the win number

12 int main(int argc, char** argv) { readCandidates(); int nStates; cin >> nStates; for (int i = 0; i < nStates; ++i) { readState(); assignDelegatesToCandidates(); } for (int i = 0; i < nCandidates; ++i) { printCandidateReport(i); } return 0; } Print won number and candidate name

13 4 I.M.FrontRunner U.R.RabbleRouser Will.I.Win U.N.Known 3 3 5 45000 I.M.FrontRunner 30000 U.R.RabbleRouser 25000 U.N.Known … Name has not space Names match candidate name

14 Primaries candidates.cpp states.cpp candidates.h states.h main.cpp Header file main function readCandidates() assignDelegatesToCandidates() readState() findCandidate(string) printCandidateReport(int) Check example It does not matter how you organize the functions in modules, just use “extern” to avoid multiple define variables

15 #include using namespace std; int var1 = 0; int var2 = 0; void set() {var1= 0; var2= 0;} void print() {cout<<var1<<var2<<endl;} int main() { set(); print(); } main.cpp

16 #include using namespace std; int main() { set(); print(); } main.cpp #ifndef SET_H #define SET_H #include using namespace std; extern int var2 = 0; void set(); #endif #include #include “print.h” #include “set.h” using namespace std; int var2 = 0; void set() {var1= 0; var2= 0;} #ifndef PRINT_H #define PRINT_H extern int var1 = 0; void print(); #endif #include #include “print.h” #include “set.h” using namespace std; int var1 = 0; void print() {cout<<var1<<var2<<endl;} print.cppprint.h set.cpp set.h Use extern to void multiple define

17


Download ppt "Lin Chen 09/06/2011. Global variables const int maxCandidates = 10; // Names of the candidates participating in this state's primary string candidate[maxCandidates];"

Similar presentations


Ads by Google