1 / 17

Assignment

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

alva
Download Presentation

Assignment

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Assignment Lin Chen 09/06/2011

  2. 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;

  3. 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);

  4. 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; }

  5. 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

  6. 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

  7. 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; }

  8. 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; }

  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 (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

  10. 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; }

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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(); }

  16. #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;}

  17. Thank you

More Related