170 likes | 318 Views
Assignment. Lin Chen 09/06/2011. 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
E N D
Assignment Lin Chen 09/06/2011
Global variables • const intmaxCandidates = 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 • intdelegatesForThisState; • // How many delgates have been won by each candidate • intdelegatesWon[maxCandidates]; • // How many candidates in the national election? • intnCandidates;
Global variables • // How many candidates in the primary for the state being processed • intnCandidatesInPrimary; • // How many states participate in the election • intnStates; • // How many delegates in the election (over all states) • inttotalDelegates = 0; • // How many votes were cast in the primary for this state • inttotalVotes; • // How many votes wone by each candiate in this state's primary • intvotesForCandidate[maxCandidates]; • intfindCandidate (std::string name);
Check program int main(intargc, char** argv) { readCandidates(); intnStates; cin >> nStates; for (inti = 0; i < nStates; ++i) { readState(); assignDelegatesToCandidates(); } for (inti = 0; i < nCandidates; ++i) { printCandidateReport(i); } return 0; }
Check program void readCandidates () { cin >> nCandidates; string line; getline (cin, line); for (inti = 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
Check program int main(intargc, char** argv) { readCandidates(); intnStates; cin >> nStates; for (inti = 0; i < nStates; ++i) { readState(); assignDelegatesToCandidates(); } for (inti = 0; i < nCandidates; ++i) { printCandidateReport(i); } return 0; } Read how many states
Check program int main(intargc, char** argv) { readCandidates(); intnStates; cin >> nStates; for (inti = 0; i < nStates; ++i) { readState(); assignDelegatesToCandidates(); } for (inti = 0; i < nCandidates; ++i) { printCandidateReport(i); } return 0; }
Check program int main(intargc, char** argv) { readCandidates(); intnStates; cin >> nStates; for (inti = 0; i < nStates; ++i) { readState(); assignDelegatesToCandidates(); } for (inti = 0; i < nCandidates; ++i) { printCandidateReport(i); } return 0; }
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 (inti = 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 Check program Read vote number for each candidate Read candidate name for each candidate
Check program int main(intargc, char** argv) { readCandidates(); intnStates; cin >> nStates; for (inti = 0; i < nStates; ++i) { readState(); assignDelegatesToCandidates(); } for (inti = 0; i < nCandidates; ++i) { printCandidateReport(i); } return 0; }
Check program intassignDelegatesToCandidates () { intremainingDelegates = delegatesForThisState; for (inti = 0; i < nCandidatesInPrimary; ++i) { intcandidateNum = findCandidate(candidate[i]); intnDel = (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
Check program int main(intargc, char** argv) { readCandidates(); intnStates; cin >> nStates; for (inti = 0; i < nStates; ++i) { readState(); assignDelegatesToCandidates(); } for (inti = 0; i < nCandidates; ++i) { printCandidateReport(i); } return 0; } Print won number and candidate name
Input Name has not space 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 … Names match candidate name
It does not matter how you organize the functions in modules, just use “extern” to avoid multiple define variables main.cpp Header file main function Modules candidates.h candidates.cpp Primaries readState() findCandidate(string) printCandidateReport(int) states.h states.cpp readCandidates() assignDelegatesToCandidates() Check example
main.cpp #include <iostream> 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(); }
#include <iostream> using namespace std; int main() { set(); print(); } main.cpp print.cpp print.h #ifndef PRINT_H #define PRINT_H extern int var1 = 0; void print(); #endif #include <iostream> #include “print.h” #include “set.h” using namespace std; int var1 = 0; void print() {cout<<var1<<var2<<endl;} Use extern to void multiple define set.h set.cpp #ifndef SET_H #define SET_H #include <iostream> using namespace std; extern int var2 = 0; void set(); #endif #include <iostream> #include “print.h” #include “set.h” using namespace std; int var2 = 0; void set(){var1= 0; var2= 0;}