Solution
// Programmer: Jennifer Leopold
// Date: January 28, 2016
// File: hw2.cpp
// Purpose: Calculate the voltage needed for an electric fence
// based on user's input of resistance, # animals,
// fence length, and whether a bull is present.
//
// To compile: fg++ hw2.cpp -o hw2
//
// To execute: ./hw2
#include <iostream>
using namespace std;
const float AMPERAGE = 0.05; // power source amps
const short COW_RESISTANCE = 20000; // resistance depending
const short SHEEP_RESISTANCE = 6000; // on animal type
const int VOLTAGE_STABILIZATION = 98000; // stabilizing factor
const short BULL_DETERENT_VOLTS = 5000; // needed to deter bull
int main()
{
int resistance; // user inputs
short numAnimals;
int fenceLength;
bool isBullPresent;
int voltage; // calculated voltage
// Greeting
cout << "Welcome to the Volto-Matic Computa-tator!\n";
// Get user inputs
cout << "Please enter the following:\n\n";
cout << " Resistance ("
<< COW_RESISTANCE << " for cows or "
<< SHEEP_RESISTANCE << " for sheep): ";
cin >> resistance;
cout << " How many critters in your pen: ";
cin >> numAnimals;
cout << " Length of fence (meters): ";
cin >> fenceLength;
cout << " Bull present? (1 = yes, 0 = no): ";
cin >> isBullPresent;
// Calculate and output necessary voltage
voltage = static_cast<int>
(
(AMPERAGE * resistance) +
((static_cast<float>(numAnimals) / fenceLength) *
VOLTAGE_STABILIZATION) +
(BULL_DETERENT_VOLTS * isBullPresent)
);
cout << "\nVoltage required: " << voltage << "v\n";
// Sign-off
cout << "\nHave a nice day!\n";
return 0;
}