17
Assignment Lin Chen 09/06/2011

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

Embed Size (px)

Citation preview

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

Assignment

Lin Chen09/06/2011

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

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;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#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

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

#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

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

Thank you