MTH4300 Home

MTH 4300: Final Practice 2

Problem 1.

For each of the following statements write one of the letters T or F. Write T if the proposition is true, and F if the proposition is false. Your answer should be a word of length exactly 5 consisting of letters T and F only.

  • (a) If X is a name of a class and the name Y has not been declared yet, then Y * X; is a valid command in C++.
  • (b) If Y is a name of a class and the name X has not been declared yet, then Y * X = new Y[20]; is a valid command in C++.
  • (c) It is known that Y is a name of a class that has a public method with name Z. The method Z accepts one argument of type int. If X is an object of type Y, then X.Z(20); is a valid command in C++.
  • (d) It is known that Y is a name of a class that has a public method with name Z. The method Z accepts one argument of type int. If the name X has not been declared yet, then Y * X; X.Z(20); is a valid command in C++.
  • (e) If Y is a name of a class, and the name X has not been declared yet, then Y ** X = new Y*; is a valid command in C++.

Problem 2.

The user input consists of a sequence of non-negative integers. A negative number is the sign that the input is over. Create a program that determines whether all positive elements from the user input are equal. If all of them are equal, then the program should print yes; otherwise, the program should print no.

Examples:
Input 1: 7 7 7 2 7 7 -1 
Output 1: no
Input 2: 7 7 7 7 7 7 -1
Output 2: yes

Problem 3. Assume that real numbers in a computer are represented using 16 bits: \(1\) for sign, \(6\) for exponent, and \(9\) for mantissa. Determine the representation of the number \(-23.125\) in this computer.

Problem 4. Determine the size of the memory leak created by the function memoryLeak() listed below. The "size" of the memory leak is defined as the number of locations of type long that were created but not deleted. Assume that the program is compiled with the flag -fno-elide-constructors.
 
#include<iostream>
class Spider{
private:
     long* a;
public:
     Spider();
     Spider(const Spider&);
     Spider(Spider&&);
     void operator=(const Spider&);
     void operator=(Spider&&);
     ~Spider();
};
Spider::Spider(){
     a=new long; a=new long; a=new long; a=new long; a=new long;
}
Spider::Spider(const Spider& m){
     a=new long; a=new long; a=new long;
}
Spider::Spider(Spider&& m){
     a=new long; a=new long; a=new long; a=new long;
}
void Spider::operator=(const Spider& m){
     a=new long;a=new long;
}
void Spider::operator=(Spider&& m){
    a=new long; a=new long; a=new long;
}
Spider::~Spider(){
     a=new long; a=new long;
}
Spider sleep(Spider & m){
     Spider n=m;
     return n;
}
int memoryLeak(){
     Spider m,p;
     p=m;
     for(int i=0;i<50;++i){
          p=sleep(m);
     }
     return 0;
}
WARNING: Do not implement the codes from this problem. The memory leak may crash or damage your computer.

Problem 5.

Create a program that determines the winner in an election. The result of the election are provided through the user input. The input consists of sequence of words that terminates with the word "end". Each word is a name of a politician, and represents one vote for that politician. Create a program that prints the name of the winner of the election. If one or more candidates have equally many votes, then the winner is the candidate who was the first one to collect the winning votes.

Example:
Input: Dog Cat Owl Cat Owl Owl Cat Dog end
Output: Owl
Explanation: The candidates Cat and Owl have three votes each. However, the candidate Owl was the first to collect the three votes, and this is what makes Owl the winner.

Problem 6.

Each product in the store has name (of type std::string) and price (of type double). Create a program called Store Manager that performs the following four operations. The complexity of each of the operations should be at most \(O(\log n)\), where \(n\) is the total number of products in the store.

  • \(1\): Insert. The user provides a string x and a real number z. The program adds the product with name x and the price z to the store. If the product with name x already exists in the system, then the program changes the price from the old value to z.
  • \(2\): Delete. The user provides a string x and the program deletes the product with name x.
  • \(3\): Search. The user provides a string x and the program returns the price of the product with name x, or a message that the product does not exist.
  • \(4\): Inflation. The user provides a real number M and the program increases the price of every product by M%.
The program should repeatedly ask the user to select one of the operations \(1 - 4\) (or \(0\) to exit). After the operation is selected, the program should ask for additional input that the operation requires. Once the input is received, the program should perform the required operation.

Example:
Input:
1 apple 100
1 watermelon 800
4 20
4 50
1 watermelon 300

If we now ask for the price of apple, we should receive the answer \(180\) (it started from \(100\) , the first inflation increased it to \(100\cdot 1.2=120\); the second inflation increased it to \(120\cdot 1.5=180\)). However, if we ask for the price of watermelong, we should receive the answer \(300\). Although there were two inflation events that increased the original price \(800\) by a lot, the command 1 watermelon 300 modified the price to \(300\) and reversed the effects of the inflations.