MTH4300 Home

MTH 4300: Midterm 2 Practice 3

Problem 1

The user provides integers through the standard input. The first of them is the number bound, and is assumed to be positive. After this number, the user provides a sequence of non-negative integers, followed by a single negative integer which indicates that the user will not provide any more numbers. Create a program that properly reads the numbers provided by the user, ignores all the numbers bigger than or equal to bound, and calculates the sum of those numbers smaller than bound.

Print the obtained sum to the standard output. If there are no numbers to be summed, then the program should print 0.

Problem 2

Use recursion to create a program that calculates \(x[n]\) if \(x\) is the sequence defined as \[ x[0]=a, \quad\quad x[n+1]=\Big(x[n]\cdot(x[n]+1)+8\Big)\%p,\mbox{for }n\geq 0.\] The user input includes the numbers \(n\), \(p\), and \(a\). The output should be \(x[n]\).

Problem 3

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

// pointer_arithmetic.cpp
 // compile with
 // c++ -o p_a pointer_arithmetic.cpp -std=c++11
 // execute with
 // ./p_a
 int main(){
  int *x;
  int *y;
  x= new int[3];
  delete[] x;
  return 0;

Problem 4

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

// mysterious_function.cpp
 // compile with
 // c++ -o mFun mysterious_function.cpp -std=c++11
 // execute with
 // ./mFun
 int mystery(int *x, int n){
  int result=0;
  return result;
 int main(){
  int* seq;
  seq=new int[5];
  delete[] seq;
  return 0;

Problem 5

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

// move_example.cpp
 // compile with
 // c++ -o m_ex move_example.cpp -std=c++11 -fno-elide-constructors
 // execute with
 // ./m_ex
 class Basket{
  Basket(const Basket& );
  Basket(Basket&& );
  void operator=(const Basket& );
  void operator=(Basket&& );
 Basket::Basket(const Basket& copyFrom){std::cout<<"2";}
 Basket::Basket(Basket&& moveFrom){std::cout<<"3";}
 void Basket::operator=(const Basket& copyFrom){std::cout<<"4";}
 void Basket::operator=(Basket&& moveFrom){std::cout<<"5";}
 Basket doSomething(Basket B){
  Basket helper;
  return helper;
 void printResults(){
  Basket greenBasket;
  Basket redBasket(greenBasket);
 int main(){
  return 0;

Problem 6

Find the sum of all elements of a given list.

Your code should replace // ??? in the text below in such a way that the obtained c++ source file accomplishes the described task.

// list_summation.cpp
// compile with
// c++ -o list_sum list_summation.cpp -std=c++11
// execute with
// ./list_sum


int listSum(ListNode *head){
  // ???

int main(){
  ListNode *head;
  return 0;

Problem 7

A sequence whose terms are from \(\{1,2,\dots, k\}\) is called a no-repetition sequence if no two consecutive terms are the same. For example, for \( k=3\) the sequences \((1, 2, 1, 2, 3, 1)\), \((1, 2, 1, 2, 1)\), and \((2, 3, 2, 1, 3)\) are no-repetition sequences, while \((1, 2, 2, 1, 3)\) and \((3, 1, 2, 1, 1)\) are not. Create a program that reads \(n\) and \(k\) from the standard input, and prints all no-repetition sequences of length \(n\) whose components are from \(\{1, 2, \dots, k\}\).