MTH 4300: Final Practice 2
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
Xis a name of a class and the nameYhas not been declared yet, thenY * X;is a valid command in C++. - (b)
If
Yis a name of a class and the nameXhas not been declared yet, thenY * X = new Y[20];is a valid command in C++. - (c)
It is known that
Yis a name of a class that has a public method with nameZ. The methodZaccepts one argument of typeint. IfXis an object of typeY, thenX.Z(20);is a valid command in C++. - (d)
It is known that
Yis a name of a class that has a public method with nameZ. The methodZaccepts one argument of typeint. If the nameXhas not been declared yet, thenY * X; X.Z(20);is a valid command in C++. - (e)
If
Yis a name of a class, and the nameXhas not been declared yet, thenY ** X = new Y*;is a valid command in C++.
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
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.
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: OwlExplanation: 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.
The user input consists of positive integers. A negative integer or 0 is a sign that the input is over. Denote by \(x_0\), \(x_1\), \(\dots\), \(x_{n-1}\) the positive elements from the user input. Create a program that counts the total number of all primes \(p\) for which \(p^2\) is a divisor of the product \(X=x_0x_1\cdots x_{n-1}\). Your program should not calculate the product \(X\), as this number can be too big for CPU to handle.
Example: Input: 18 36 34 41 14 28 -1 Output: 3Explanation: \(2\), \(3\), \(7\), \(17\), and \(41\) are the prime factors of \(X\). The numbers \(2^2\), \(3^2\), and \(7^2\) are factors of \(X\), while \(17^2\) and \(41^2\) are not. Hence, the answer is \(3\).
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
xand a real numberz. The program adds the product with namexand the pricezto the store. If the product with namexalready exists in the system, then the program changes the price from the old value toz. - \(2\): Delete. The user provides a string
xand the program deletes the product with namex. - \(3\): Search. The user provides a string
xand the program returns the price of the product with namex, or a message that the product does not exist. - \(4\): Inflation. The user provides a real number
Mand the program increases the price of every product byM%.
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 watermelon, 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.