MTH4300 Home

## MTH 4300: Midterm 1 Practice 2

Problem 1. What happens when the following program is executed? Provide a rigorous justification for your answer.
// myCode.cpp
// compile with
// c++ myCode.cpp -o myProgam -std=c++11
// execute with
// ./myProgram
#include<iostream>
int main(){
int X=10;
int *Y=&X;
int &Z=*Y;
X=X - 7;
Z=Z * 8;
std::cout<<X + Z<<std::endl;
return 0;
}


Problem 2.
• (a) Create the function funSequence whose input consists of three arguments: pointerBegin, left, and right and whose output is a real number. The argument pointerBegin is the pointer to the beginning of a sequence X of real numbers, and the arguments left and right are two positive integers. The output of the function should be $\mbox{output}=X[\mbox{left}]+\frac{11}{73}\cdot X[\mbox{right}].$
• (b) Create a program that first reads three integers numTerms, alpha, and beta from the standard input. After these integers, the program receives a sequence userSequence of numTerms real numbers. The program should call the function funSequence created in part (a) with the arguments userSequence, alpha, and beta. In the end, the program should print the output of the function.

Problem 3. The user input consists of a positive integer $$n$$. Create the program that prints $$n$$ lines, each of which consists of $$n$$ numbers. For each $$i\in \{1$$, $$2$$, $$\dots$$, $$n\}$$ the $$i$$-th line starts with $$n-i$$ zeroes that are followed by the sequence $$1$$, $$7$$, $$1$$, $$7$$, $$\dots$$ This additional sequence contains another $$i$$ terms which means that each line has $$n$$ terms.
 Example:
Input:
5
Output:
0 0 0 0 1
0 0 0 1 7
0 0 1 7 1
0 1 7 1 7
1 7 1 7 1


Problem 4. In a given list find all terms $$x$$ that are divisible by 5 and replace them by $$x-1$$. Your code should replace $$\mbox{/* ??? 1 ??? */}$$ and $$\mbox{/* ??? 2 ??? */}$$ in the text below in such a way that the obtained c++ source file accomplishes the described task.
// listMod.cpp
// compile with
// c++ listMod.cpp -o lMod -std=c++11
// execute with
// ./lMod

#include<iostream>
#include "mth4300_list_basics.cpp"

void changeDivisibleBy5(/* ??? 1 ??? */){
/* ??? 2 ??? */
}
int main(){
return 0;
}


Problem 5.

The sequence $$x$$ initially contains 100 terms. They alternate between $$1$$ and $$0$$, i.e. $$x_0=1$$, $$x_1=0$$, $$x_2=1$$, $$x_3=0$$, $$x_4=1$$, $$x_5=0$$, $$\dots$$, $$x_{98}=1$$, $$x_{99}=0$$.

The sequence $$x$$ is repeatedly extended by the user who provides positive integers smaller than $$100$$ through the standard input. When the user provides $$0$$, then the input is over and no more numbers are expected from the user.

The first term of the user input $$k_0$$ adds one more member $$x_{100}$$ to the sequence in the following way: The sum of the last $$k_0$$ terms of $$x$$ is calculated, and the obtained sum is added as the term $$x_{100}$$. In other words, $$x_{100}$$ is defined as $x_{100}=x_{99}+x_{98}+\cdots+x_{100-k_0}.$ The next number $$k_1$$ that the user provides is used to determine the element $$x_{101}$$ as the sum of the last $$k_1$$ terms of $$x$$: $x_{101}=x_{100}+x_{99}+x_{98}+\cdots+x_{101-k_1}.$ This procedure continues until the user inserts 0.

Create the program that reads the user input properly and prints on the standard output the final length and the last term of the sequence $$x$$.

Example:

Input:
4 3 5 3 0
Output:
The final length of the sequence is 104.
The last term of the sequence is 11.


Explanation: The number $$4$$ is the first value that the user supplies. It defines $$x_{100}$$ as \begin{eqnarray*}x_{100}&=&x_{99}+x_{98}+ x_{97}+x_{96}\newline&=&0+1+0+1=2.\end{eqnarray*} The next number $$3$$ determines $$x_{101}$$ as $$x_{101}=x_{100}+x_{99}+x_{98}=2+0+1=3$$. The next number $$5$$ defines $$x_{102}=x_{101}+x_{100}+x_{99}+x_{98}+x_{97}=3+2+0+1+0=6$$. The value $$3$$ gives us $$x_{103}=x_{102}+x_{101}+x_{100}=6+3+2=11$$. Thus the program prints that there are $$104$$ terms of the sequence and that the last one is 11.

Problem 6.

The user input consists of a positive integer $$n$$. There are $$n$$ bags of candies. The first bag contains $$1$$ candy, the second bag has $$2$$ candies, the third one has $$3$$, and so on. The last, $$n$$th bag, contains $$n$$ candies. Two children $$A$$ and $$B$$ want to fairly distribute these bags without opening them. Either both kids have to get the same number of candies or $$A$$ can get one candy more than $$B$$. Create the program that outputs the division of bags among the children.

Example:

Input:
6
Output:
A gets:
2, 3, 6
B gets: 1, 4, 5