1A)
apvector<int> CalculateModes(const apvector<int>
&tally) {
apvector<int> result;
int a=0;
int max = FindMax(tally);
// find out how many modes we have
for (int c = 0; c < tally.length();
c++)
if (tally[c] == max) a++;
result.resize(a); // now make enough
room for it
// now set the results
a = 0;
for (int c = 0; c < tally.length();
c++) {
if (tally[c] == max)
result[a++] = c;
return result;
}
1B)
int KthDataValue(const apvector<int> &tally, int k) {
int index = 0;
while (k>0) {
k = k - tally[index];
index++;
}
index--;
return index;
}
2A)
void ChangePrices(GroceryStore &store, istream &input) {
apstring groceryname;
double newprice;
while (input >> groceryname >> newprice)
{
store.SetPrice(groceryname, newprice);
}
}
2B)
apstring BargainItem(const GroceryStore &store, char category) {
apvector<apstring> items=
store.GetItems(category);
if (category == "P" || category == "M" || category
== "D") {
double bestbargain = store.GetPrice(items[0]) /
store.GetSize(items[0])
apstring bestname = items[0];
for (int a = 0; a < items.length(); a++)
if (bestbargain >
(store.GetPrice(items[a]) / store.GetSize(items[a]))) {
bestbargain =
store.GetPrice(items[a])/ store.GetSize(items[a]);
bestname =
items[a];
}
return bestname;
}
else return "none";
}
3A)
Position Position::Northeast () const {
return North().East();
}
3B)
Neighborhood Fish::ForwardNbrs(const Environment &env) {
Neighborhood result;
if (myDir == "N") {
if (env.isempty(myPos.North())
result.Add(myPos.North());
if (env.isempty(myPos.Northeast())
result.Add(myPos.Northeast();
if (env.isempty(myPos.Northwest())
result.Add(myPos.Northwest();
}
else if (myDir == "NE") {
if (env.isempty(myPos.North())
result.Add(myPos.North());
if (env.isempty(myPos.Northeast())
result.Add(myPos.Northeast());
if (env.isempty(myPos.East())
result.Add(myPos.East());
}
... // the other cases, which the prompt says we can
use ... to indicate
return result;
}
3C)
apstring Position::DirectionTo(const Position & other) const {
apstring result;
if (other.equals(myPos.North()) result = "N";
if (other.equals(myPos.Northeast()) result = "NE";
... // the other cases
return result;
}
4A)
int Flight::EmptySeatCount(const apstring & seatType) const {
int countseats = 0;
for (int a = 0; a < mySeats.numrows(); a++)
for (int b = 0; b <
mySeats.numcols(); b++) {
if (seatType == "any"
&& mySeats[a][b].GetPassenger().GetName()== "") countseats++;
else if
(mySeats[a][b].GetType() == seatType &&
mySeats[a][b].GetPassenger().GetName() == "") countseats++;
}
return countseats;
}
4B)
int Flight::FindBlock(int row, int seatsNeeded) const {
bool found = false;
int seatsfound=0;
int indexfound = 0;
for (int a = 0; a < mySeats.numcols(); a++) {
if
(mySeats[row][a].GetPassenger().GetName() == "") {
seatsfound = 1;
indexfound = a;
for (;(a <
mySeats.numcols() && seatsfound < seatsNeeded) &&
(mySeats[row][a].GetPassenger().GetName() == ""); a++) {
seatsfound++;
}
if (seatsfound ==
seatsNeeded) {
found = true;
break;
}
}
}
if (found==true) return indexfound;
return -1;
}
4C)
bool Flight::AssignGroup(const apvector<Passenger> &group)
{
bool result = false;
int startindex;
for (int row = 0; row < mySeats.numrows();
row++) {
startindex = FindBlock(row,
group.length());
if (startindex != -1) {
result = true;
for (int col = 0; col
< group.length(); col++)
mySeats[row][col+startindex] = group[col];
}
if (result == true) break; // don't do
this more than once
}
return result;
}