MTH4300 Home

MTH 4300: Midterm 2 Practice 4

Problem 1.

The user first enters a positive integer \( n \), followed by the sequence \( x \) of \( n \) integers \( x_0 \), \( x_1 \), \( \dots \), \( x_{n-1} \), and the positive integer \( c \). Create a program that on the standard output prints the number of terms of the sequence \( x \) that are equal to \( c \).


 7 8 6 8 4 8 7 9 8

Explanation: There are 3 terms of the sequence \( x \) that are equal to \( c=8 \).

Problem 2.

What happens when the following code is executed? Provide a rigorous justification for your answer.

// compile with 
// c++ -o problem2 problem2.cpp -std=c++11
// execute with
// ./problem2
int main(){
     int dog, *cat, *gremlin;
     gremlin=new int;
     *gremlin=dog * (*cat);
     delete gremlin;
     return 0;

Problem 3.

Which line of the code will cause the compiler to report an error? Provide a rigorous justification for your answer.

1    // compile with 
2    // c++ -o powerfulProgram powerfulProgram.cpp -std=c++11
3    // execute with
4    // ./powerfulProgram
5    #include<iostream>
6    void powerfulFunction(int** x){
7         std::cout<<"I am full of power"<<std::endl;
8    }
9    int main(){
10       int ***powerfulPointer;
11       powerfulFunction(&powerfulPointer);
12       powerfulFunction(*powerfulPointer);
13       return 0;
14   }

Problem 4.

Each object of the class Zoo should have two private attributes with the following declaration

int numberOfAnimals;
std::string* animals;
The sequence animals should always be of length numberOfAnimals and represents the list of animals in the zoo.

Your task is to create the appropriate class and the methods described in the list below. You must use the keyword const whenever possible.

  • (a) getNumAnimals should return the value of the attribute numberOfAnimals;

  • (b) setNumAnimals with one argument of type int. The method should change the value of the attribute numberOfAnimals to be equal to the supplied argument. Notice that you may need to change the length of the sequence and maybe move the sequence to a different place in RAM.

  • (c) getAnimal with one argument i of type int. The method should return the \( i \)th element of the sequence animals;

  • (d) setAnimal with two arguments: i of type int and newAnName of type std::string. The function should set the \( i \)th element of the sequence animals to be equal to newAnName.

  • (e) Default constructor (that creates an empty sequence with numberOfAnimals=0, copy constructor, copy assignment, move constructor, and move assignment. The move constructor and move assignment should not allocate additional memory for the sequence, but instead should use the resources of the supplied argument.

  • (f) Destructor that frees the memory taken by the sequence.

Problem 5.

Create the class StackDouble that performs the functions of a stack whose elements are real numbers of type double. The class should have constructor, destructor, standard push and pop methods, and additional method isEmpty() that returns 1 if the stack is empty and 0 otherwise.

Using the created class, solve the following problem: The people stand in a single line and their heights are positive real numbers. The heights are provided from left to right through the standard input and a negative number represents the end of the input. The ending negative number is not a height of any of the people. After inserting these numbers, the user provides a positive integer \(k\). Create the program that calculates the sum of the heights of the right-most \(k\) people in this line. If \(k\) is larger than the total number of people, then the program should calculate the sum of all heights. Print the resulting sum to the standard output.

Example 1:
 Input: 8.3 2.5 7.1 -8.2 2
 Output: 9.6
 Example 2:
 Input: 8.3 2.5 7.1 -8.2 7
 Output: 17.9

Problem 6.

Two pirates \(A\) and \(B\) have found \(n\) boxes with gold coins in each of them. The boxes are placed in a perfect line from left to right and the numbers of coins in them are \(c[0]\), \(c[1]\), \(\dots\), \(c[n-1]\). The pirate \(A\) decides on a number \(k\) such that \(0\leq k < n\). Then the pirate \(B\) decides whether he/she wants to take boxes \(c[0]\), \(c[1]\), \(\dots\), \(c[k-1]\) (the leftmost \(k\) boxes), or boxes \(c[k]\), \(c[k+1]\), \(c[n-1]\) (the rightmost \(n-k\) boxes).

Each pirate wants to take as many coins as possible. Write the program to determine the number \(k\) that \(A\) should choose to maximize his/her gain.

The user inputs the number \(n\) and the sequence \(c[0]\), \(c[1]\), \(\dots\), \(c[n-1]\). Your program should output the number \(k\).


3 7 8 1 5 7 2 8 5

Explanation: In this example we have \(n=9\) and the sequence \(c\) is \(c=(3, 7, 8, 1, 5, 7, 2, 8, 5)\).

When \(k=5\), the total number of coins in boxes 0, 1, 2, 3, and 4 is: \(3+7+8+1+5=24\) while the number of coins in boxes 5, 6, 7, and 8 is: \(7+2+8+5=22\).

With this division, the pirate \(A\) is guaranteed to take 22 coins.

Any other choice for \(k\) would result in \(A\) having fewer than 22 coins.