/* Programmer: Jennifer Leopold
Date: October 13, 2016
File: hw7_functs.cpp
Purpose: Function definitions 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.
*/
#include <iostream>
#include "hw7_functs.h"
using namespace std;
void greeting()
{
cout << "\nWelcome to Lisa & Chief Wiggum's Trash Pile "
<< "and Offender Database System\n";
return;
}
void signOff()
{
cout << "\nThank you for your interest in our database!\n";
return;
}
int myRand(const int low, const int high)
{
return((rand() % (high - low + 1)) + low);
}
void createTrashDB(trashPile trashDB[], const int n)
{
for (int i = 0; i < n; i++)
{
trashDB[i].sectorNum = myRand(SECTOR_NUM_MIN,
SECTOR_NUM_MAX);
trashDB[i].weight = myRand(TRASH_WEIGHT_MIN ,
TRASH_WEIGHT_MAX);
}
return;
}
void createOffenderDB(offender offenderDB[], const int n)
{
for (int i = 0; i < n; i++)
{
offenderDB[i].name =
OFFENDER_NAMES[rand() % NUM_OFFENDER_NAMES];
offenderDB[i].guilt = myRand(OFFENDER_GUILT_MIN,
OFFENDER_GUILT_MAX);
}
return;
}
void printTrashPile(const trashPile t)
{
cout << "Sector " << t.sectorNum
<< ": weight " << t.weight << " lbs\n";
return;
}
void printTrashPiles(const trashPile trashDB[], const int n)
{
cout << "\nContents of Chief Wiggum's Trash Pile Database\n";
for (int i = 0; i < n; i++)
printTrashPile(trashDB[i]);
return;
}
void printTrashBySector(const trashPile trashDB[], const int n)
{
int sectorTotalWeight;
// For each sector...
for (int sector = SECTOR_NUM_MIN; sector <= SECTOR_NUM_MAX;
sector++)
{
// ...add up the weight for each pile in that sector
sectorTotalWeight = 0;
for (int i = 0; i < n; i++)
if (trashDB[i].sectorNum == sector)
{
sectorTotalWeight += trashDB[i].weight;
printTrashPile(trashDB[i]);
}
cout << "Sector " << sector
<< ": total weight " << sectorTotalWeight
<< " lbs\n\n";
}
return;
}
void printOffender(const offender o)
{
cout << o.name << ", guilt = " << o.guilt << endl;
return;
}
void printOffenders(const offender offenderDB[], const int n)
{
cout << "\nContents of Lisa's Offender Database\n";
for (int i = 0; i < n; i++)
printOffender(offenderDB[i]);
return;
}
int calculateTotalTrashWeight(const trashPile trashDB[],
const int n)
{
int total = 0;
for (int i = 0; i < n; i++)
total += trashDB[i].weight;
return(total);
}
int determineNumLargestPiles(const trashPile trashDB[],
const int n)
{
// Calculate PERCENT_TOTAL_TRASH_FACTOR % of total trash
// pile weight
int weightMetric =
static_cast<int>(calculateTotalTrashWeight(trashDB, n) *
PERCENT_TOTAL_TRASH_FACTOR);
// Calculate # highest-weighted piles such that sum of their
// weights is <= weightMetric
int i = 0;
int total = 0;
while ((i < n) && (total <= weightMetric))
{
total += trashDB[i].weight;
i++;
}
return(i);
}
int findTrashPile(const trashPile trash,
const trashPile trashDB[],
const int numTrashEntries)
{
int trashEntryNum = 0;
bool found = false;
while ((trashEntryNum < numTrashEntries) && (!found))
if (trashDB[trashEntryNum] == trash)
found = true; // found at position trashEntryNum
else trashEntryNum++;
if (!found) trashEntryNum = -1; // indicates 'not found'
return(trashEntryNum);
}
int getIntInput(const string prompt, const int minValue,
const int maxValue)
{
bool invalidEntry;
int value;
do
{
cout << prompt;
cin >> value;
invalidEntry = (value < minValue) || (value > maxValue);
if (invalidEntry)
cout << "Invalid entry! Can't be < " << minValue
<< " or > " << maxValue << "\n";
} while (invalidEntry);
return(value);
}
void getInputForTrashEntry(trashPile &trash)
{
trash.sectorNum = getIntInput("\nEnter trash sector #: ",
SECTOR_NUM_MIN,
SECTOR_NUM_MAX);
trash.weight = getIntInput("Enter trash weight (lbs): ",
TRASH_WEIGHT_MIN,
TRASH_WEIGHT_MAX);
return;
}
void findContributorsOfMostTrash(const trashPile trashDB[],
const int numTrashEntries,
const offender offenderDB[])
{
int trashContributors;
// Calculate # of trash piles that account for
// PERCENT_TOTAL_TRASH_FACTOR % of total trash pile weight
trashContributors =
determineNumLargestPiles(trashDB, numTrashEntries);
// Output the entries in offenderDB[0..trashContributors-1]
cout << "\nOffenders who contribute at least "
<< (PERCENT_TOTAL_TRASH_FACTOR * 100)
<< "% of trash:\n";
for (int i = 0; i < trashContributors; i++)
printOffender(offenderDB[i]);
return;
}
void findOffenderByTrashPile(const trashPile trashDB[],
const int numTrashEntries,
const offender offenderDB[])
{
trashPile trash;
int trashEntryNum;
char doAgain;
cout << "\nNow you have the opportunity to find the offender"
<< "\nmost likely to have created a particular trash "
<< "pile!\n";
do
{
// Get user input for a particular trash pile
getInputForTrashEntry(trash);
// Get the index position in the trashDB where this entry
// occurs (-1 if it does not exist)
trashEntryNum = findTrashPile(trash, trashDB,
numTrashEntries);
if (trashEntryNum < 0)
cout << "\nNo such trash pile exists in the database!\n";
else
{
// Output the offender in the corresponding index position
// in the offenderDB
cout << "\nThat was most likely created by ";
printOffender(offenderDB[trashEntryNum]);
}
cout << "\nDo you want to look up another one? (y/n) ";
cin >> doAgain;
}
while ((doAgain == 'y') || (doAgain == 'Y'));
return;
}