1A)
bool IsMode(const apvector<int> &data, int k) {
return ((data[k-1] < data[k]) && (data[k]
> data[k+1]));
}
1B)
int ModeIndex(const apvector<int> &data) {
int a, result = -1;
for (a = 0; a < data.length(); a++)
if (IsMode(data, a) result = a;
return result;
}
1C)
void PrintHistogram(const apvector<int> &data, int
longestBar, char barChar) {
int mode;
double barspervalue;
mode = data[ModeIndex(data)];
barspervalue = longestBar / mode;
for (int a = 0; a < data.length(); a++) {
for (int b = 0; b < (data[a] *
barspervalue); b++)
cout <<
barChar;
cout << endl;
}
}
#2 is skipped since this deals with another case study
3A)
int Occurrences(const WordCollection &C, const apstring &word) {
int count=0;
for (int a = 0; a < C.Size(); a++)
if (C.FindKth(a+1) == word) count++; //
note, the counting for FindKth starts at 1
return count;
}
3B)
void RemoveDuplicates(WordCollection &C, const apstring &word) {
int instances;
apstring word;
for (int a = C.Size(); a > 0; a--) {
word = C.FindKth(a);
instances = Occurrences(C,word);
if (instances > 1) {
for (int b = 1; b <
instances; b++) {
C.Remove(word);
a--; // we
have one less item in the collection now
}
}
}
}
3C)
// This is not the most efficient imaginable answer, but it'll work
perfectly fine
apstring MostCommon(const WordCollection &C) {
int instances, mostinstances;
apstring word, biggestword;
biggestword = C.FindKth(C, 1);
mostinstances = Occurrences(C, biggestword);
for (int a = 0; a < C.Size(); a++) {
word = C.FindKth(a);
instances = Occurrences(C,word);
if (instances > mostinstances) {
mostinstances = instances;
biggestword = word;
}
}
return biggestword;
}
4A)
Point Encryptor::GetCoordinates(char ch) const {
Point result;
for (int a = 0; a < 6; a++)
for (int b = 0; b < 6; b++)
if (myMat[a][b]==ch) {
result.row
= a;
result.col
= b;
}
return result;
}
4B)
// note: this can be done in a single line!
apstring Encryptor::EncryptTwo(const apstring &pair) const {
Point position1, position2;
apstring result;
position1 = GetCoordinates(pair[0]);
position2 = GetCoordinates(pair[1]);
result = myMat[position1.row][position2.col];
result = result +
myMat[position2.row][position1.col];
return result;
}
4C)
apstring Encryptor::EncryptWord(const apstring &word) const {
apstring result = "";
apstring pair;
for (int a = 0; a < word.length()-1; a+=2) {// -1
means we're not going to try to process the stray last character (in
case it exists)
pair = word[0];
pair = pair + word[1];
result = result + EncryptTwo(pair);
}
if (a != word.length()) result = result +
word[word.length()-1]; // add in the stray character if you should
return result;
}