Upload
kory-poole
View
215
Download
2
Embed Size (px)
Citation preview
Assignment
Lin Chen09/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
• 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;
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);
Check program
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;}
Check program
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 andInitialize the number of winning delegates to be
zero
Check program
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
Check program
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;}
Check program
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;}
Check programvoid 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
Check program
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;}
Check program
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
Check program
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
Input
4I.M.FrontRunnerU.R.RabbleRouserWill.I.WinU.N.Known33 545000 I.M.FrontRunner30000 U.R.RabbleRouser25000 U.N.Known…
Name has not space
Names match candidate name
Modules
Primariescandidates.cpp
states.cpp
candidates.h
states.h
main.cpp
Check example
It does not matter how you organize the functions in modules, just use “extern” to avoid multiple
define variables
#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();}
main.cpp
#include <iostream>using namespace std;
int main(){ set(); print();}
main.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;}
#ifndef PRINT_H#define PRINT_Hextern 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;}
print.cpp print.h
set.cppset.h
Use extern to void multiple define
Thank you