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

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

Factorial Preparatory Exercise #include using namespace std; double fact(double); int fact(int); int main(void) { const int n=20; ofstream my_file("results.txt");
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
1 Objectives Understand streamed input and output.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
Chapter 4 Summation.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
ITEC 320 C++ Examples.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
1 Writing a Good Program 8. Elementary Data Structure.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Lin Chen 09/13/2011. Count words number in the file arrayUtils.h include the adding functions template int addInOrder (T* array, int& size, T value);
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
C++ Basics L KEDIGH. Objectives 1. basic components of a c++ program using proper terminology and programming conventions adopted for this class. 2. List.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
C++ Basics L KEDIGH. Objectives 1. basic components of a c++ program using proper terminology and programming conventions adopted for this class. 2. List.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
System Programming Practical Session 7 C++ Memory Handling.
EIToolkit stub doc. main.cpp file int main(int argc, char* argv[]) { EIProperties::UseStandards(); // create the stub ExampleStub stub; stub.Start();
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
11 Introduction to Object Oriented Programming (Continued) Cats.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
1 Compiler directive: #define, usage 1 #include using namespace std; #define TAX //double TAX=0.08; #define LAST_NAME "Li" #define FIRST_NAME "Dennis"
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Functions Input and output Lecture 2. Constants #define – is a preprocessor directive Most common use.
C++ CSCE 343.
MT262A Review.
#define #include<iostream> using namespace std; #define GO
Command Line Arguments
Agenda Make Utility Command Line Arguments in Unix
Understand argc and argv
Structure of C Programs
Arrays Part-1 Armen Keshishian.
Command-Line Arguments
Multi-dimensional Array
Pointers.
L09 - Pokémon.
C++ Templates CSE 333 Spring 2018
C Stuff CS 2308.
Code::Block vs Visual C++
C++ File Structure and Intro to the STL
Namespaces How Shall I Name Thee?.
C++ File Structure and Intro to the STL
Class rational part2.
CSE Module 1 A Programming Primer
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

4 I.M.FrontRunner U.R.RabbleRouser Will.I.Win U.N.Known I.M.FrontRunner U.R.RabbleRouser U.N.Known … Name has not space Names match candidate name

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

#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

#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