/* Programmer: Jennifer Leopold
Date: October 13, 2016
File: hw7_functs.h
Purpose: Constants, type definitions, and function prototypes
used in program that creates a database of trash
piles and a database of offenders. Shows user
worst offenders, offenders who contribute high
percentage of trash, and offender who likely
created a particular trash pile.
*/
#ifndef HW7_FUNCTS_H
#define HW7_FUNCTS_H
#include <iostream>
#include <cstdlib>
using namespace std;
// ***********************************************************
// Constants
// # entries in each 'database' array
const int NUM_DB_ENTRIES = 15;
// Possible offender names
const int NUM_OFFENDER_NAMES = 15;
const string OFFENDER_NAMES[NUM_OFFENDER_NAMES] =
{"Homer", "Maggie", "Bart", "Moe", "Barney", "Marge", "Cletus",
"Snake", "Mr. Burns", "Dr. Nick", "Krusty the Clown",
"Chief Wiggum", "Ralph Wiggum", "Skinner", "Lenny"};
// Minimum and maximum values for database entries
const int SECTOR_NUM_MIN = 1;
const int SECTOR_NUM_MAX = 7;
const int TRASH_WEIGHT_MIN = 25;
const int TRASH_WEIGHT_MAX = 500;
const int OFFENDER_GUILT_MIN = 100;
const int OFFENDER_GUILT_MAX = 10000;
// Percent of trash (weight) that is considered majority
const float PERCENT_TOTAL_TRASH_FACTOR = 0.5;
// ***********************************************************
// Type declarations
struct trashPile // Entry in Wiggum's DB
{
int sectorNum;
int weight;
bool operator >(const trashPile &other) const
{ return(weight > other.weight); }
bool operator ==(const trashPile &other) const
{
return((weight == other.weight) &&
(sectorNum == other.sectorNum));
}
};
struct offender // Entry in Lisa's DB
{
string name;
int guilt;
bool operator >(const offender &other)
{ return(guilt > other.guilt); }
};
// ***********************************************************
// Function prototypes
// Display a greeting to the user.
// Preconditions: None
// Postconditions: A message has been displayed on the screen.
void greeting();
// Display a sign-off message to the user.
// Preconditions: None
// Postconditions: A message has been displayed on the screen.
void signOff();
// Generate a random number within a specified range.
// Preconditions: Parameter low is <= parameter high.
// Postconditions: A value >= low and <= high is returned.
int myRand(const int low, const int high);
// Populate an array with n randomly created trashPile entries.
// Preconditions: n should be > 0.
// Postconditions: Elements of trashDB[0..n-1] now contain
// randomly chosen data values that are within valid ranges for
// those respective fields.
void createTrashDB(trashPile trashDB[], const int n);
// Populate an array with n randomly created offender entries.
// Preconditions: n should be > 0.
// Postconditions: Elements of offenderDB[0..n-1] now contain
// randomly chosen data values. Guilt value is within valid
// range and name has been chosen from OFFENDER_NAMES.
void createOffenderDB(offender offenderDB[], const int n);
// Output all information about a trashPile.
// Preconditions: sectorNum and weight fields of trashPile t
// should contain data.
// Postconditions: sectorNum and weight fields of trashPile have
// been output to the screen.
void printTrashPile(const trashPile t);
// Output entries in trashPile array.
// Preconditions: trashDB[0..n-1] should contain data, n > 0.
// Postconditions: Data in trashDB[0..n-1] have been output to
// the screen.
void printTrashPiles(const trashPile trashDB[], const int n);
// Output total weight of trash piles in each sector.
// Preconditions: trashDB[0..n-1] should contain data, n > 0.
// Postconditions: Total weight of trash piles in each sector
// has been output to the screen.
void printTrashBySector(const trashPile trashDB[], const int n);
// Output all information about an offender.
// Preconditions: name and guilt fields of offender o should
// contain data.
// Postconditions: name and guilt fields of offender have been
// output to the screen.
void printOffender(const offender o);
// Output entries in offender array.
// Preconditions: offenderDB[0..n-1] should contain data, n > 0.
// Postconditions: Data in offenderDB[0..n-1] have been output to
// the screen.
void printOffenders(const offender offenderDB[], const int n);
// Calculate the total weight for all trash piles in trashDB.
// Preconditions: trashDB[0..n-1] should contain data, n > 0.
// Postconditions: Returned is total weight for all trash piles
// in trashDB.
int calculateTotalTrashWeight(const trashPile trashDB[],
const int n);
// Determine the # highest-weighted piles such that sum of their
// weights is <= PERCENT_TOTAL_TRASH_FACTOR percent of total
// trash pile weight in trashDB.
// Preconditions: trashDB[0..n-1] should contain data, n > 0.
// Postconditions: Returned is # highest-weighted piles such that
// sum of their weights is <= PERCENT_TOTAL_TRASH_FACTOR percent
// of total trash pile weight in trashDB.
int determineNumLargestPiles(const trashPile trashDB[],
const int n);
// Find the index position in the trashPile array where a
// particular entry exists.
// Preconditions: trash should contain data for the trashPile
// that is to be found, trashDB[0..n-1] should contain data,
// n > 0.
// Postconditions: Returned is the index position where trash
// occurs in trashDB; if it does not exist, -1 is returned.
int findTrashPile(const trashPile trash,
const trashPile trashDB[],
const int numTrashEntries);
// Output a prompt and get input for an integer, validating that
// it is within a specified range.
// Preconditions: prompt should be non-empty, and minValue should
// be <= maxValue.
// Postconditions: Returned is an integer that is >= minValue and
// <= maxValue.
int getIntInput(const string prompt, const int minValue,
const int maxValue);
// Get input for the fields of a trashPile.
// Preconditions: None.
// Postconditions: Pass-by-reference parameter trash is returned
// with data values for sectorNum and weight.
void getInputForTrashEntry(trashPile &trash);
// Display the offenders who are most likely responsible for
// contributing to PERCENT_TOTAL_TRASH_FACTOR percent of total
// trash pile weight in trashDB.
// Preconditions: trashDB[0..numTrashEntries-1] and
// offenderDB[0..numTrashEntries-1] should contain data,
// numTrashEntries > 0.
// Postconditions: Offenders who are most likely responsible for
// contributing to PERCENT_TOTAL_TRASH_FACTOR percent of total
// trash pile weight in trashDB will have been displayed on the
// screen.
void findContributorsOfMostTrash(const trashPile trashDB[],
const int numTrashEntries,
const offender offenderDB[]);
// Let the user query the databases, specifying a particular
// trash pile, and finding the most likely offender responsible
// for it.
// Preconditions: trashDB[0..numTrashEntries-1] and
// offenderDB[0..numTrashEntries-1] should contain data,
// numTrashEntries > 0.
// Postconditions: Offender responsible for each trash pile that
// is input by user will have been displayed on the screen.
void findOffenderByTrashPile(const trashPile trashDB[],
const int numTrashEntries,
const offender offenderDB[]);
// ***********************************************************
// Templated functions
// Sorts the elements of the given array into descending order.
// Preconditions: arraySize should be > 0, and a[0..arraySize-1]
// should contain data. Base type of array (i.e., T) must be
// comparable using > operator.
// Postconditions: Elements of a[0..arraySize-1] are in
// descending order.
template <typename T>
void sortArray(T a[], const int arraySize)
{
T maxValue, temp;
int positionOfMax;
for (int i = 0; i < arraySize-1; i++)
{
// Assume max from position i to end of array is the
// value at position i
maxValue = a[i];
positionOfMax = i;
// See if you can find a larger value from position i+1
// to the end of the array
for (int j = i+1; j < arraySize; j++)
{
if (a[j] > maxValue)
{
maxValue = a[j];
positionOfMax = j;
}
}
// Swap the value at positionOfMax with the value at
// position i
temp = a[i];
a[i] = maxValue;
a[positionOfMax] = temp;
}
return;
}
#endif