int WordCount(const Vector & list, string word, int numElts) // precondition: numElts = # of values in list // postcondition: returns number of occurrences of word in list { int count = 0; int k; for(k=0; k < numElts; k++) { if (list[k] == word) { count++; } } return count; } string ModalValue(const Vector & list, int numElts) // precondition: 0 < numElts and numElts = # of values in list // postcondition: returns mode, or maximally occuring string in list { int max = 0; // # times modal value occurs string mode; // modal string int k; int val; for(k=0; k < numElts; k++) { if ( (val = WordCount(list,list[k],numElts) ) > max) { max = val; mode = list[k]; } } return mode; } int RowSpaceCount(const Matrix & mat, int row, int numCols) // precondition: mat has numCols columns // postcondition: returns number of spaces ('.') in row // (row specified by value of parameter row) { int k; int space = 0; for(k=0; k < numCols; k++) { if (mat[row][k] == '.') { space++; } } return space; } int TotalSpaceCount(const Matrix & mat, int numRows, int numCols) // precondition: mat has numRows rows and numCols columns // postcondition: returns number of spaces ('.') in mat { int count = 0; int k; for(k=0; k < numRows; k++) { count += RowSpaceCount(mat,k,numCols); } return count; } void ReflectDiagonal(Matrix & mat, int row, int col) // precondition: mat is square with mat.Rows() rows and columns // 0 <= row < mat.Rows() and 0 <= col < mat.Rows() // either row == 0 or col == 0 // postcondition: down-left diagonal starting in mat at (row,col) // is reflected (along the main / diagonal) { int topr = row; int topc = col; int bottomr = mat.Rows() - col - 1; int bottomc = mat.Cols() - row - 1; while (topr != bottomr) { Swap(mat[topr][topc],mat[bottomr][bottomc]); topr++; topc++; bottomr--; bottomc--; } } void ReflectDiagonal(Matrix & mat, int row, int col) { int diagSize; int k; int size = mat.Rows(); if (row == 0) { diagSize = size - col; } else { diagSize = size - row; } // diagSize = size - (row == 0 ? col : row); for(k=0; k < diagSize/2; k++) { Swap(mat[row+k][col+k],mat[size-col-1-k][size-row-1-k]); } } void ReflectAll(Matrix & mat) // precondition: matrix is square with mat.Rows() rows and columns // postcondition: all diagonals reflected { int k; int size = mat.Rows(); ReflectDiagonal(mat,0,0); // main diagonal for(k=0; k < size; k++) { ReflectDiagonal(mat,0,k); ReflectDiagonal(mat,k,0); } } void Expand(Vector & list, int numElts) // precondition: all elements of list are greater than 0 // list has list.Length() elements // postcondition: each occurrence of number n replaced by n occurrences of n { int j,k; Vector aux(numElts); // will grow as needed int auxIndex = 0; // index into aux for(k=0; k < list.Length(); k++) { for(j=0; j < list[k]; j++) { if (auxIndex >= aux.Length()) // grow if needed { aux.SetSize(aux.Length()*2); } aux[auxIndex] = list[k]; auxIndex++; } } list = aux; // copy and resize list.SetSize(auxIndex); } void Expand(Vector & list, int numElts) // precondition: all elements of list are greater than 0 // list has list.Length() elements // postcondition: each occurrence of number n replaced by n occurrences of n { int count = 0; int j,k; for(k=0; k < numElts; k++) // get exact # of slots needed { count += list[k]; } list.SetSize(count); // grow the vector int index = count-1; // from back to front for(k=numElts-1; k >= 0; k--) { for(j=0; j < list[k]; j++) // copy right # of values { list[index] = list[k]; index--; } } } void FlexAccounts::ReadData(string filename) // postcondition: data file 'filename' is read and information // is stored, each social security number in data file // occurs once in myList with total of all expenditures // for that social security number { ifstream input; input.open(filename); string ssn; double cost; while (input >> ssn >> cost) { for(int k=0; k < myCount; k++) // search all entries { if (myList[k].ssn == ssn) // found, update { myList[k].expenses += cost; break; } } if (k >= myCount) // not found, add { if (myCount >= myList.Length()) // grow if needed { myList.SetSize(myList.Length() * 2); } myList[myCount].ssn = ssn; myList[myCount].expenses = cost; myCount++; } } }