MTH4300 Home

# MTH 4300: Midterm 2 Practice 9

Problem 1. Create a program in C++ that uses pointers to allocate a block of consecutive memory locations for $$50$$ integers, then stores the numbers $$1^2$$, $$2^2$$, $$\dots$$, $$50^2$$ in those $$50$$ blocks, and then returns the memory to the operating system to prevent a memory leak.

Problem 2. Assume that the variables a, b, and c of type long are properly declared in the main function and that the following command will be given
re_order(a,  &b, &c);
Write the implementation of the function re_order that replaces the characters // ??? below
void re_order(long& x, long* y, long* z){
// ???
}
and that re-orders the contents of the variables a, b, and c in such a way that the following holds:
a <= b <= c.

Problem 3.
• (a) What is stack? Provide the declaration of the stack node whose elements are of type long. The name of the structure for the node should be SN, the name of the attribute that keeps the data in the node should be cont, and the name of the pointer to the next node should be aNext. Create an implementation of the function pop.
• (b) The function erasingFunctionWith5 is supposed to identify the first (from the top) node that contains the value $$5$$ and delete it together with all the nodes that are below it. The function is called with the command a=erasingFunctionWith5(a); for an object a of type SN*. The following implementation of the function has a bug in one of the lines. All other lines are correct.
SN* erasingFunctionWith5(SN* a){
if(a==nullptr){return nullptr;}
if((*a).cont==5){
while(a!=nullptr){a=pop(a);}
return nullptr;
}
SN* run=a;
while((run!=nullptr)&&((*run).aNext!=nullptr)&&((*((*run).aNext)).cont!=5)){
run=(*run).aNext;
}
while(run!=nullptr){run=pop(run);}
return a;
}
Determine the line that contains the bug and determine the correction that should replace that line with the bug (it is acceptable if your correction takes more than one line of code). Write down the new corrected implementation of erasingFunctionWith5.

Problem 4.

Important notice: In this problem you will be asked to declare a class and implement only few of its methods. Do not implement all of the methods. You would spend a lot of time doing so and you will not earn extra credit for that!

You must use the keyword const whenever possible.

The class StarSystem should have the private attribute aListPlanets which is the pointer to the head of a list. Each list node should contain the attribute nameOfPlanet of type std::string and a pointer to another list that contains satellites of the planet.

The public methods should include: addPlanet that adds the planet to the star system; addSatelliteToPlanet that adds the satellite to the planet (the names of satellite and planet should be supplied as arguments); printAllSatellitesOfThePlanet that prints all satellites of the specified planet (the name of the planet should be an argument of the method); printAll that prints all planets and all satellites.

In addtion, provide the declarations for the default constructor, copy constructor, move constructor, copy assignment, move assignment and the destructor.

Create implementations for the copy constructor, move assignment, and destructor.

Problem 5. The user input consists of a positive integer $$n$$ and a sequence $$x$$, $$x$$, $$\dots$$, $$x[n-1]$$ of integers. (The sequence $$x$$ can have positive elements, negative elements, and elements that are equal to $$0$$). Create a program of complexity $$O(n)$$ that calculates the maximal possible sum of consecutive elements of $$x$$. In other words, determine the maximum of
x[i]+x[i+1]+...+x[j-1]
where $$i \leq j$$ are two numbers from the set $$\{0$$, $$1$$, $$\dots$$, $$n-1\}$$.

Problem 6.

The user input consists of an integer $$n$$ and a sequence of $$n$$ positive integers $$x$$, $$x$$, $$\dots$$, $$x[n-1]$$. Create a program that reads the numbers from the input and then re-arranges the terms of the sequence $$x$$ in the following way: Between every pair of numbers $$3$$, the orders of the elements of the sequence $$x$$ is reversed from the original order. You are allowed to assume that the first and the last term of the sequence $$x$$ is $$3$$. If any of the first and last term is not $$3$$, then your program should change them to $$3$$.

Example: Input:

15
3 5 2 3 7 8 9 3 3 9 8 6 5 6 3

Output:
3 2 5 3 9 8 7 3 3 6 5 6 8 9 3