Program Assignment

apap8.1 - vectors

 

     

 

1.         Download AquaFish.cpp, AquaFish.h and AquaMain.cpp and edit the files accordingly:

     Add a new data member, myPosCount, to the AquaFish class, an apvector that records the number of times the fish has been at each position in the tank.

     Modify the AquaFish constructor that initializes the new data member appropriately.

     Modify the AquaFish member function Swim to update these counts after each move.

     Add an AquaFish member function to return the number of times the fish as visited a position.

     Modify the program in AquaMain so that it uses the modified AquaFish class and, after completing 1000 steps, prints a histogram of the number of times each position in the tank bas been visited.  The tank size should be set to 20.  The histogram should look like:

 

Size

1            xxx

2            xxxx

3            xxxxx

.

.

.

20         xxx

 

The location containing the most counts should print out 20 x’s, while the other locations should print out a proportional number of x’s for that location’s count.

 

 

   2.      Write a C++ program to read in a line of text from the keyboard using the getline statement.  Output how many a - z's were entered.  Upper and lower case letters can be entered; count little a and capital A as the same.  For example:

 

Enter a line of text below:

I sure hope this WORKS!!!

 

e's = 2

h's = 2

i's = 2

k's = 1

o's = 2

p's = 1

r's = 2

s's = 3

t's = 1

u's = 1

w's = 1

 

 

   3.      Write a C++ program that inputs integer test scores from an external file, and prints out the average of the scores and the score that is nearest to the average.  Use the file tests.txt when turning in the assignment.  Since you do not know how many scores you will find in the file, you will need resize the vector if the number of elements in the file exceeds the size of the vector you initially started with.  You should always double the size of the vector when resizing.  Once completed entering all the elements into the vector, resize the vector to exactly the number of elements needed.


   4.      This problem involves manipulating an unordered vector of N integers that does not contain duplicate elements.

 

(a)     Write a function PosOfMax that returns the index of the largest element in the vector.  For example, if array A is 15, 16, 17, -1, 0, then the expression PosOfMax(A) evaluates to 2.

 

(b)    Write a function PosOf2nd that returns the index of the second largest element in the vector.  In writing PosOf2nd you must call PosOfMax of part (a).

 

(c)     Write a procedure ZeroBetween that, given a vector, replaces every element positioned between the largest and second largest elements with zero.  In writing ZeroBetween you must call functions PosOfMax and PosOf2nd.

 

You will also have to write functions to enter the scores into the vector and to display the vector.  Make your program menu driven with the following choices:

1.   Enter values into vector TEST TO MAKE SURE THERE ARE NO DUPLICATES!!!

2.   Zero between largest and second largest

3.   Display vector

4.   Quit

 

 

   5.      Given a list of scores stored in an vector, a final score is to be computed by ignoring the lowest score and averaging the other scores.  If the lowest score appears more than once in the list, only one occurrences is to ignored.

 

EXAMPLE

 

vector                          final score

3 2 4 2 5                      14 / 4 = 3.5

           

Write a function FinalScore that accepts a vector and that computes the corresponding score in the manner indicated above.

 

Assume that there are at least two elements in the vector.

 

You will also have to write a function to enter the scores into the vector.  Make your program menu driven with the following choices:

1.   Enter Scores

2.   Compute average and determine lowest score to be dropped

3.   Display average and lowest score dropped

4.   Quit

 

 

   6.      Write the following functions:

 

(a)     Write a function ArraySum whose header is given below.  ArraySum returns the sum of the first n values in its vector parameter.

 

         For example, if the vector A is 4, 2, 10, 3, 8 then the value of ArraySum(A, 3) is 16 and the value of ArraySum(A, 1) is 4.

 

int ArraySum(const apvector<int> & a, int n);

// precondition:  1 ≤ n ≤ a.length()

// postcondition:  returns a[0] + ... + a[n-1]

 

(b)    Write a function ComputePartialSums whose header is given below.  ComputePartialSums assigns values to a vector B such that b[k] = a[0] + a[1] + ... + a[k-1] for all values k such that 0 ≤ k ≤ a.length(); i.e., each entry in B has a value equal to the sum of all entries with equal or lower indexes in A.

 

For example, if the vector A is 4, 2, 10, 3, 8 then after the call ComputePartialSums(A, B) the array B should have values 4, 6, 16, 19, 27.

 

void ComputePartialSums(const apvector<int> & a, apvector<int> & b)

// precondition:  vector a is initialized

// postcondition:  b[k] = a[0] + ... + A[k-1] for all k such that 0 ≤ k ≤ a.length()

 

You will also have to write functions to enter the scores into the array and display either vector (only one function is needed, it  will display any vector passed!).  Make your program menu driven with the following choices:

1.   Enter values into vector

2.   Compute Partial Sums

3.   Display original vector

4.   Display partial sum vector

5.   Quit