DOC PREVIEW
Duke CPS 100E - Test 1

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CPS 100E, Fall 1995, Test 1Owen Astrachan and Susan RodgerOctober 10, 1995 Vector a la Mode (10 points)Part A(4 points) Write the function WordCount whose header is given below. WordCount returns thenumber of occurrences of word in Vector list. For example, the callWordCount(list,"dog",10) should evaluate to 2 since "dog" appears twice in list asshown 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 listPart B(6 points) Write the function ModalValue whose header is shown below. ModalValue returns thestring that occurs most frequently in its parameter list. If there are ties, i.e., more thanone string occurs most frequently, return any maximally occurring string. For example, iflist is the array shown on the previous problem, the call ModalValue(list,10) shouldreturn "bird" since bird occurs more often than any other string in list. In writingModalValue you can call function WordCount from part A. Assume that WordCountworks 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 listMatrices (20 points)For this question you will write four functions. The matrix mat diagrammed below willbe used to illustrate the functions (this matrix might come from the WordPuzzle class wediscussed in class, but doesn’t have to). . . . . . . . H I . J . . . . . O . . . . . E . . M Y . . . . . . . A T Part A (4 points) Write the function RowSpaceCount that determines the number of spaces (indicated bythe character ’.’) in the row specified by parameter row. For example, the callRowSpaceCount(mat,0,6) should return 6 since there are six spaces (’.’) in the rowwith index 0 and the call RowSpaceCount(mat,2,6) should return 5 since there are fivespaces 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 amatrix. The call TotalSpaceCount(mat,6,6) should return 27 since there are 27 spacesin the matrix shown above. In writing TotalSpaceCount you can call the functionRowSpaceCount from part A. Assume that RowSpaceCount works as specified regardlessof what you wrote in part A. int TotalSpaceCount(const Matrix<char> & mat, int numRows, int numCol // 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 thetop-left row and column of the diagonal one of which must be 0. A down-right diagonalis reflected in the up-left main diagonal / shown in the diagram below on the left as adotted line. For example, given the matrix on the left below, the callReflectDiagonal(mat,0,1) yields the matrix shown in the middle and the call {\ttReflectDiagonal(mat,0,0)} yields the matrix on the right. The down-right diagonalsreflected are shown in each matrix. void ReflectDiagonal(Matrix<char> & mat, int row, int col) // precondition: mat is square with mat.NumRows() rows and columns // 0 <= row < mat.NumRows() and 0 <= col < mat.NumRows // 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 withindex 0 or the column with index 0. For example, the call ReflectAll(mat) where matis shown on the left yields the matrix on the right. In writing ReflectAll you may callfunction ReflectDiagonal from part B. Assume that ReflectDiagonal works asspecified regardless of what you wrote for Part C. void ReflectAll(Matrix<char> & mat) // precondition: matrix is square with mat.NumRows() rows and colum // postcondition: all diagonals reflectedExpand ( 10 points )Write a function Expand that replaces each occurrence of a number n with n occurrencesof the number. For example, 4 is replaced by four occurrences of 4. The vector below onthe left should be transformed into the vector on the right. 2 1 3 4 2 2 1 3 3 3 4 4 4 4You 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 occurrenceFlexing Muscles (12 points)In this problem you will write part of a program that processes data for Flex purchases oncampus. The data is stored as social security numbers and purchase amounts as shownbelow; the output is a list of the total amount spent for each social security number. Thedata file ssn is reproduced below. 054-133-1234 3.25123-199-6660 2.55054-133-1234 2.75123-199-6660 5.55939-939-9339 0.15123-199-6660 1.00054-133-1234 2.75When run the output should be a list of social security numbers with the total flexexpenses for each number. 054-133-1234 8.75123-199-6660 9.10939-939-9339 0.15Consider the class FlexAccounts shown below and the client/user program given inmain to process all flex accounts in the file ssn. The program is complete except forthe member functions ReadData and Next which you will write. Account informationis stored in a Vector {\tt myList} of structs, where each struct stores a social securitynumber 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 pro void


View Full Document

Duke CPS 100E - Test 1

Documents in this Course
Topics

Topics

9 pages

Lecture

Lecture

3 pages

Notes

Notes

2 pages

Hashing

Hashing

19 pages

Lecture

Lecture

59 pages

Lecture

Lecture

6 pages

Lecture

Lecture

4 pages

Lecture

Lecture

20 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

7 pages

Lecture

Lecture

8 pages

Lecture

Lecture

10 pages

Lecture

Lecture

4 pages

Notes

Notes

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Lecture

Lecture

13 pages

Lecture

Lecture

6 pages

Lecture

Lecture

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

5 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

10 pages

Sets

Sets

14 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Load more
Download Test 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Test 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Test 1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?