 The Simplistic Primary Predictor in C++

Subject Computer Science C-Family Programming

Question

The Simplistic Primary Predictor predicts the winner of a state's primary election based on several factors:

The state that the candidate comes from
The region of the country the candidate comes from
The number of times a television commercial for the candidate has aired in that state
The number of days the candidate has spent campaigning in that state
The number of primary elections the candidate has won so far this year
The input to the program will be the name of a state (as a 2-letter abbreviation), followed by a list of information about each of several candidates. The information about each candidate will be:

A two-letter set of initials representing the name of the candidate
A two-letter state abbreviation, representing the home state of the candidate
An integer, representing the number of television commercials for the candidate that have aired in the current state.
An integer, representing the number of days that the candidate has spent campaigning in the state.
An integer, representing the number of primaries and caucuses the candidate has won so far this year
The program will compute a score for each candidate that attempts to predict the candidate's likelihood of success. The score is computed as follows: Begin with a score of 0. If the candidate's home state is the same as the current state, add 50 points. If the candidate's home state's region is the same as the current state's region, add 20 points. Add 1 for every 10 commercials that have aired. (Round down to the nearest integer: 37 commercials gives 3 points.) Add 1 for every day the candidate has spent campaigning in the state. Add 5 for every primary or caucus the candidate has won so far.
Use these regions in your calculations:

Region number Region Description States in Region
1 New England ME, NH, VT, MA, CT, RI
2 Northeast NY, PA, NJ, DE, MD
3 Southeast VA, NC, SC, GA, FL, AL, MS, TN, KY, WV, AR, LA
4 Lakes OH, MI, IN, IL, WI, MN
5 Central IA, MO, ND, SD, NE, KS, OK, TX
6 West MT, WY, CO, NM, AZ, UT, ID, NV
7 Pacific WA, OR, CA, AK, HI
The output will be a list of candidates' names and scores.

Disclaimer: While I have based the sample data below on some actual 2016 candidates, I have completely invented the numbers for commercials, campaign days, and wins so far. At the time of writing this document, there have not yet been any primary elections; the number of wins will, of course, change constantly throughout the primary season.

You are guaranteed that there will be at least one candidate in the input, and that there will be no more than 50 candidates.

Note: As you write your programs, try to implement the classification into regions as elegantly as possible. Try to avoid the 100-line solution, if you can:

if state = "ME" then
region = 1
elseif state = "NH" then
region = 1
...
end if;
Many languages at least permit the use of boolean operators:

if state = "ME" or state = "NH" or ... then
and some languages contain structures such as lists and sets that allow even more compact solutions.
Think about how to use the control constructs and data structures of your language to implement this classification process as elegantly as your language will allow.

Example 1:

Input:

NH
HC NY 78 21 1
MO MD 30 10 0
BS VT 93 15 0
Output:

HC 33
MO 13
BS 44
Explanation:
Candidate HC: 0 + 0 + 7 + 21 + 5*1 = 33
Candidate MO: 0 + 0 + 3 + 10 + 5*0 = 13
Candidate BS: 0 + 20 + 9 + 15 + 5*0 = 44

Example 2:

Input:

FL
12
JB FL 113 8 5
BC MD 163 9 3
CC NJ 95 4 3
TC TX 210 6 5
CF CA 40 1 0
JG VA 22 1 0
MH AR 25 2 0
JK OH 35 9 2
RP KY 13 2 0
MR FL 135 5 5
RS PA 38 2 0
DT NY 298 3 4
Output:
JB 94
BC 40
CC 28
TC 52
CF 5
JG 23
MH 24
JK 22
RP 23
MR 113
RS 5
DT 52
Explanation:

Candidate JB: 50 + 20 + 11 + 8 + 5*5 = 94
Candidate BC: 0 + 0 + 16 + 9 + 5*3 = 40
Candidate CC: 0 + 0 + 9 + 4 + 5*3 = 28
Candidate TC: 0 + 0 + 21 + 6 + 5*5 = 52
Candidate CF: 0 + 0 + 4 + 1 + 5*0 = 5
Candidate JG: 0 + 20 + 2 + 1 + 5*0 = 23
Candidate MH: 0 + 20 + 2 + 2 + 5*0 = 24
Candidate JK: 0 + 0 + 3 + 9 + 5*2 = 22
Candidate RP: 0 + 20 + 1 + 2 + 5*0 = 23
Candidate MR: 50 + 20 + 13 + 5 + 5*5 = 113
Candidate RS: 0 + 0 + 3 + 2 + 5*0 = 5
Candidate DT: 0 + 0 + 29 + 3 + 5*4 = 52

Testing suggestion:

Since the input for this problem is lengthy, I suggest creating a file containing the test data, and then using either cut-and-paste with the mouse or input redirection to run the test examples. This will save a considerable amount of typing.

1. Favorite-language problem

Write a program to solve the homework problem described above, using one of the following languages: Java, C, C++, or Python. (If you have another favorite language you'd prefer to use, talk to me.)

The input will contain the current state on a line by itself, then the number of candidates on a line by itself, then each candidate's information on a separate line. A candidate's information will be the five items listed in the general problem description (a 2-letter set of initials, a 2-letter state, and three integers), separated by spaces.

The output will be a list of candidates' names and scores, using the format illustrated in the examples below.

Examples:

Input:

NH
3
HC NY 78 21 1
MO MD 30 10 0
BS VT 93 15 0
Output:

HC 33
MO 13
BS 44
Input:

FL
12
JB FL 113 8 5
BC MD 163 9 3
CC NJ 95 4 3
TC TX 210 6 5
CF CA 40 1 0
JG VA 22 1 0
MH AR 25 2 0
JK OH 35 9 2
RP KY 13 2 0
MR FL 135 5 5
RS PA 38 2 0
DT NY 298 3 4
Output:
JB 94
BC 40
CC 28
TC 52
CF 5
JG 23
MH 24
JK 22
RP 22
MR 113
RS 5
DT 52

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

typedef struct
{
string initials;
string state;
int numberOfCommercials;
int numberOfDaysInCampaign;
int numberOfWonElections;
}Candidate;

// Returns the first two letters from the string and cuts it from the origin
string GetSubString2(string * st)
{
string str = "";
string s = *st;

//Check if the string is longer than 1 letter and finds the first ' '
if ((s.length() > 1) && (s.find_first_of(' ') != s.npos))
{
str = s.substr(0, 2);
s = s.substr(3, s.length() - 3);
*st = s;
}
return str;
}

// Checks if the string contains only digits
bool IfNumber(string s)
{
bool isNumber = true;
int j = s.length();
int i = 0;
while ((isNumber) && (i < j))
{
isNumber &= ((s[i] >= '0') && (s[i] <= '9'));
i++;
}
return isNumber;
}...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

Do-While Loop For a Ticket-Taker Problem \$15.00
Programming
Computer Science
C++
Ticket-Taker
Prices
Do-While Loop
Discounts
Children
Constant Values
Integers
Sales
Messages
Input
Output
Grade Point Average (GPA) Calculator \$88.00
Name
GPA
Calculator
Flowchart
Pseudocode
Test
Plan
Analysis
Computer
Science
C-Family
Programming
Attributes & Methods in C++ \$10.00
Programming
C++
Computer Science
Attributes
Methods
Students
People
Inheritance
Employees
Salaries
Job Titles
Classes
Statements
Variables
Programming Questions \$40.00
Programming
C++
Computer Science
Strings
Codes
Characters
Pointers
Buffer
CMYK Colors
Encoding
Issues
Compiler Errors
Assembly Language
Intel Machine
Lines
Symbols
Restaurant Waitlist Using C++ \$13.00
Programming
C++
Computer Science
Queue
People
Restaurant Waitlist
Guests
Reservations
Statements
Variables
Data Sets
Constructors
Pointers
Search
Input
Output
Superhero Fight in C++ \$35.00
Programming
Classes
Vectors
Operators
Pointers
Computer Science
Superhero Fight
Game Developments
C++
OOP
Powers
Drivers
Lasers
Strength
Variables
Loops
Statements
Interfaces
Strings
Integers
Random Functions
Input
Output
Live Chats