CPS 100E, Fall 1995, Test 1

Owen Astrachan and Susan Rodger
October 10, 1995

Vector a la Mode (10 points)

Part A(4 points)

Write the function WordCount whose header is given below. WordCount returns the number of occurrences of word in Vector list. For example, the call WordCount(list,"dog",8) should evaluate to 2 since "dog" appears twice in list as shown below.

"bird""cat""dog" "bird""cat""mouse" "bird""dog"
int WordCount(const Vector<string> & list, string word, int numElts) // precondition: numElts = # of values in list // postcondition: returns number of occurrences of word in list

Part B(6 points)

Write the function ModalValue whose header is shown below. ModalValue returns the string that occurs most frequently in its parameter list. If there are ties, i.e., more than one string occurs most frequently, return any maximally occurring string. For example, if list is the array shown on the previous problem, the call ModalValue(list,8) should return "bird" since bird occurs more often than any other string in list. In writing ModalValue you can call function WordCount from part A. Assume that WordCount works as specified regardless of what you wrote for part A.

string ModalValue(const Vector<string> & list, int numElts) // precondition: 0 < numElts and numElts = # of values in list // postcondition: returns mode, or maximally occuring string in list

Matrices (20 points)

For this question you will write four functions. The matrix mat diagrammed below will be used to illustrate the functions (this matrix might come from the WordPuzzle class we discussed in class, but doesn't have to).
..... .
.HI.J.
....O.
....E.
.MY...
....AT

Part A (4 points)

Write the function RowSpaceCount that determines the number of spaces (indicated by the character '.') in the row specified by parameter row. For example, the call RowSpaceCount(mat,0,6) should return 6 since there are six spaces ('.') in the row with index 0 and the call RowSpaceCount(mat,2,6) should return 5 since there are five spaces in the row with index 2.

Complete RowSpaceCount below.

int RowSpaceCount(const Matrix<char> & mat, int row, int numCols) // precondition: mat has numCols columns // postcondition: returns number of spaces ('.') in row // (row specified by value of parameter row)

Part B: (4 points)

Write a function TotalSpaceCount that returns the total number of spaces ('.') in a matrix. The call TotalSpaceCount(mat,6,6) should return 27 since there are 27 spaces in the matrix shown above. In writing TotalSpaceCount you can call the function RowSpaceCount from part A. Assume that RowSpaceCount works as specified regardless of what you wrote in part A.

int TotalSpaceCount(const Matrix<char> & mat, int numRows, int numCols) // precondition: mat has numRows rows and numCols columns // postcondition: returns number of spaces ('.') in mat

Part C: (8 points) Write the function ReflectDiagonal whose header is given below. ReflectDiagonal reflects the elements in a ``down-right'' diagonal of a square matrix. In a square matrix, a down-right diagonal (in the direction: \) can be specified by the top-left row and column of the diagonal one of which must be 0. A down-right diagonal is reflected in the up-left main diagonal / shown in the diagram below on the left as a dotted line. For example, given the matrix on the left below, the call ReflectDiagonal(mat,0,1) yields the matrix shown in the middle and the call ReflectDiagonal(mat,0,0) yields the matrix on the right. The down-right diagonals reflected are shown in each matrix.

*

void ReflectDiagonal(Matrix<char> & 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)

Part D: (4 points)

Write the function ReflectAll that reflects all diagonals that start in either the row with index 0 or the column with index 0. For example, the call ReflectAll(mat) where mat is shown on the left yields the matrix on the right. In writing ReflectAll you may call function ReflectDiagonal from part B. Assume that ReflectDiagonal works as specified regardless of what you wrote for Part C.

*

void ReflectAll(Matrix<char> & mat) // precondition: matrix is square with mat.Rows() rows and columns // postcondition: all diagonals reflected

Expand ( 10 points )

Write a function Expand that replaces each occurrence of a number n with n occurrences of the number. For example, 4 is replaced by four occurrences of 4. The vector below on the left should be transformed into the vector on the right.
2134
22133 34444

You MAY use an auxiliary vector to do the expansion.

void Expand(Vector<int> & list) // 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

Flexing Muscles (12 points)

In this problem you will write part of a program that processes data for Flex purchases on campus. The data is stored as social security numbers and purchase amounts as shown below; the output is a list of the total amount spent for each social security number. The data file ssn is reproduced below.

054-133-1234   3.25
123-199-6660   2.55
054-133-1234   2.75
123-199-6660   5.55
939-939-9339   0.15
123-199-6660   1.00
054-133-1234   2.75

When run the output should be a list of social security numbers with the total flex expenses for each number.

054-133-1234 8.75
123-199-6660 9.10
939-939-9339 0.15

Consider the class FlexAccounts shown below and the client/user program given in main to process all flex accounts in the file ssn. The program is complete except for the member functions ReadData and Next which you will write. Account information is stored in a Vector myList of structs, where each struct stores a social security number and expenses.

#include "CPstring.h" #include "vector.h" #include <fstream.h> struct Info { string ssn; // social security number double expenses; // total flex expenses }; class FlexAccounts { public: FlexAccounts(); // construct void ReadData(string filename); // read from specified file void First(); // set up for first entry bool IsDone(); // returns true of all entries processed void Next(); // advance to next entry Info Current(); // return current entry private: Vector<Info> myList; // list of ssn's and expenses int myCount; // # of entries stored in myList int myIndex; // index/location of entry to be processed }; FlexAccounts::FlexAccounts() : myList(100) // postcondition: all fields initialized, 0 accounts stored { myCount = myIndex = 0; } 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) { } } void FlexAccounts::First() // postcondition: Current() will return first item { myIndex = 0; } bool FlexAccounts::IsDone() // postcondition: returns true if current is valid item { return myIndex >= myCount; } void FlexAccounts::Next() // postcondition: advance to next item (to become current) { } Info FlexAccounts::Current() // postcondition: return current item { return myList[myIndex]; } main() { FlexAccounts flex; flex.ReadData("ssn"); Info temp; for(flex.First(); ! flex.IsDone(); flex.Next()) { temp = flex.Current(); cout << temp.ssn << " " << temp.expenses << endl; } }

Part A: (2 points)

Complete the function FlexAccounts::Next() so that it works as specified. Write code for the body of the function above.

Part B: (8 points)

Complete the member function FlexAccounts::ReadData()} so that it processes all the data in the file specified by the parameter filename. Currently the function reads all the data, but doesn't store the data anywhere. Use the space on the previous page.

Part C: (2 points)

Describe what changes are necessary to print the list expenditures sorted in numerical order by social security. You do NOT need to write code, you just need to describe how to generate output in sorted order.