MTH4300 Home

MTH 4300: Midterm 2 Practice 5

Problem 1.

The user input consists of a positive integer \( n \), followed by the sequence \( x \) of \( n \) integers \( x_0 \), \( x_1 \), \( \dots \), \( x_{n-1} \), and the positive integer \( M \). Let \( p \) be the remainder that is obtained when \( M \) is divided by \( 100 \). Create the program that calculates the average \( \bar x \) of all the terms of the sequence \( x \) and on the standard output prints this average \( \bar x \) and the number of terms of the sequence \( x \) that are greater than or equal to \( \bar x \) and divisible by \( p \).

Example

Input:
7 8 6 15 2 30 12 9 203
Output:
11.7143 3

Explanation: The remainder when \( M=203 \) is divided by \( 100 \) is \( 3 \). The average of the sequence \( x \) is 11.7143. Observe that 15, 30, and 12 are the terms of the sequence \( x \) that are greater than or equal to the average and that are divisible by \( p=3 \).

Problem 2.

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

// threeFunctions.cpp
 // compile with
 // c++ -o threeFs threeFunctions.cpp -std=c++11
 // execute with
 // ./threeFs
 #include<iostream>
 int simpleAdd(int inValue){
    inValue = inValue + 15;
    return inValue;
 }
 int pointerAdd(int * inValue){
    *inValue = *inValue + 15;
    return *inValue;
 }
 int referenceAdd(int & inValue){
    inValue = inValue + 15;
    return inValue;
 }
 int main(){
    int inValue, outValue;
    inValue= 20;
    outValue=simpleAdd(inValue);
    std::cout<<"inValue="<<inValue<<" and outValue="<<outValue<<std::endl;
    inValue=20;
    outValue=pointerAdd(&inValue);
    std::cout<<"inValue="<<inValue<<" and outValue="<<outValue<<std::endl;
    inValue=20;
    outValue=referenceAdd(inValue);
    std::cout<<"inValue="<<inValue<<" and outValue="<<outValue<<std::endl;
    return 0;
 }

Problem 3.

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

// problem2.cpp
 // compile with
 // c++ -o p2 problem2.cpp -std=c++11
 // execute with
 // ./p2
 #include<iostream>
 
 int main(){
     int orange = 7;
     int& apple = orange;
     int* strawberry = &orange;
     *strawberry = 9;
     strawberry = new int;
     *strawberry = 11;
     std::cout<< *strawberry + orange + apple<<std::endl;
     delete strawberry;
     return 0;
 }

Problem 4.

Each object of the class HolidayBag should have two private attributes with the following declaration

int numberOfGifts;
 std::string* nameOfGifts;

The sequence nameOfGifts should always be of length numberOfGifts and represents the list of gifts in the bag.

Your task is to create the appropriate class and the methods described in the list below. You must use the keyword const whenever possible.

  • (a) getNumGifts should return the value of the attribute numberOfGifts;
  • (b) setNumGifts with one argument of type int. The method should change the value of the attribute numberOfGifts to be equal to the supplied argument. Notice that you may need to change the length of the sequence and maybe move the sequence to a different place in RAM.
  • (c) getGiftName with one argument i of type int. The method should return the \( i \)th element of the sequence nameOfGifts;
  • (d) setGiftName with two arguments i of type int and newName of type std::string. The function should set the \( i \)th element of the sequence nameOfGifts to be equal to newName.
  • (e) Default constructor (that creates an empty sequence with numberOfGifts =0), copy constructor, and copy assignment.
  • (f) Destructor that frees the memory taken by the sequence.

Problem 5.

An array of positive integers that ends in a single negative integer \(z\) is provided through the standard input. Create the program that accepts these numbers from the standard input and then prints those that are divisible by \((-z)\) to the standard output. The order of the numbers that are printed should be the same as the order in which they were received.

Example:

Input:
 57 47 45 57 83 87 17 27 37 45 -3
 Output:
 57 45 57 87 27 45

Problem 6.

A positive integer \(x\) is called consecutively factorable integer (CFI for short) if it can be written as a product of two consecutive positive integers. For example, the number \(30\) is CFI because it can be written as the product of consecutive integers \(5\) and \(6\).

Create the program whose input is a positive integer \(n\) and the output is the number of ways in which \(n\) can be expressed as a sum of distinct integers all of which are CFI.

Example:

Input:
 50
 Output:
 3

Explanation: There are \(3\) ways to express \(50\) as a sum of distinct CFIs. They are \begin{eqnarray*} 50&=&2+6+12+30=1\cdot 2+2\cdot 3+3\cdot 4+5\cdot 6;\newline 50&=&2+6+42=1\cdot 2+2\cdot 3+6\cdot 7;\;\mbox{ and}\newline 50&=&20+30=4\cdot 5+5\cdot 6. \end{eqnarray*}