int IndexMax(const Vector & list, int numElts) { int index = 0; int k; for(k=0; k < numElts; k++) { if (a[k] > a[index]) { index = k; } } return index; } int IndexSecond(const Vector & list, int numElts) { int k; int max = MaxIndex(list,numElts); int index = 0; if (max == 0) { index = 1; // tentative second max } for(k=0; k < numElts; k++) { if (k != max && a[k] > a[index]) { index = k; } } return index; } // version two int IndexSecond(const Vector & list, int numElts) { Vector aux; int max = IndexMax(list,numElts); aux = numElts; // copy all values aux[max] = INT_MIN; // smallest value possible return IndexMax(aux,numElts); } double NetWorth(Vector & portfolio, int numStocks) { Broker b; int k; double total = 0.0; double net; for(k=0; k < numStocks; k++) { net = b.CurrentPrice(portfolio[k].name) - portfolio[k].price; total += net*portfolio[k].shares; } return total; } int Count(const Vector & list, int numStr, const String & key) // precondition: numStr = # of entries in list // postcondition: returns # of occurrences of key in list { int count = 0; int k; for(k=0; k < numStr; k++) { if (list[k] == key) { count++; } } return count; } void Modal(const Vector & list, int numStr, String & maxStr, int & occs) // precondition: numStr = # of entries in list // postcondition: maxStr = string occurring most often in list // occs = # of occurrences of maxStr in list { int k; maxStr = list[0]; // tentative max; occs = Count(list,numStr,maxStr); int temp; for(k=1; k < numStr; k++) { temp = Count(list,numStr,list[k]); if (temp > occs) { maxStr = list[k]; occs = temp; } } } int MaxRun(Vector & list, int numItems) // precondition: list is sorted in non-decreasing order // postcondition: returns length of maximal run { Vector counts(list[numItems-1],0); // histogram int k; for(k=0; k < numItems; k++) { counts[ list[k] ]++; } return counts[IndexMax(list,numItmes)]; }