MTH4300 Home

MTH 4300: Midterm 1 Practice 1

Problem 1. It is known that xyz is a function with two arguments. The first argument of the function has the type int. The second argument has the type double. The function returns a value of the type double. Which of the following blocks of code is correct?
  • (A) double z; double y; int x; std::cin >> y >> x >> z; z=xyz(x,y);
  • (B) double z; double y; int x; std::cin >> y >> z >> x; double=xyz(int,double);
  • (C) double z; double y; int x; std::cin >> z >> x >> y; double=xyz(int x,double y);
  • (D) double z; double y; int x; std::cin >> z >> x >> z; double z=xyz(y,x);
  • (E) double z; double y; int x; std::cin >> z >> x >> z; (y,x)=xyz(double z);

Problem 2. The user input consists of a positive integer \(m\). Create a program that prints all pairs of integers \((x,y)\) such that \(0 < x < m \), \(0 < y < m \), and \(x\) is divisible by \(y\).

Problem 3. The implementation of the function modifySequence is:
int modifySequence(int * aA){ 
   if( (*aA) == 26 ){
      *aA=(*aA)*2; 
   }
   else{
      *aA=(*aA)*10; 
   }
   return *(aA+50)+(*aA+50);
} 
Assume that aX contains the address of the first term of the sequence of integers. The elements of the sequence are \(1\), \(6\), \(11\), \(16\), \(\dots\). Every two consecutive terms differ by \(5\) and there are \(1000\) terms in the sequence. What is the content of the variable total after the following block of code?
int total;
total=modifySequence(aX+5)+modifySequence(aX+15);
total+=aX[5]+aX[15];

Problem 4. The user input consists of a positive integer \(T\) greater than or equal to \(3\) and a sequence of \(T\) real numbers that represent temperatures that were measured during \(T\) consecutive days. Create the program that calculates the maximal change in temperature between two consecutive days.
Example:
Input: 
7
35.9 51.7 21.5 33.2 37.5 35.7 40.5
Output:
30.2
Explanation: The temperature on day 0 is \(t_0=35.9\). The temperature on day 1 is \(t_1=51.7\). The temperature on day 2 is \(t_2=21.5\), and so on. The days \(1\) and \(2\) are consecutive. The difference between the temperatures \(t_1\) and \(t_2\) is \(|t_2-t_1|=30.2\) and this is the maximal of all of the differences on consecutive days.

Problem 5.

Warning: Do not implement the codes from this problem on a standard computer. A damage can occur.

The implementation of the function spider is given below.

long spider(long x, long y, long z){
   if(x < 75){
      return y*75+z;
   }
   if(y > 70000){
      return spider(x-1,8,z);
   }
   if(z > 30000){
      return spider(x,y+89,59);
   }
   return spider(x,y,z+19);
}
What is the content of the variable crawl after the following code is executed on a computer with sufficiently fast CPU and sufficiently big memory?
long crawl;
crawl=spider(50000,28,23);

Problem 6.

The user input consists of three integers \(a\), \(b\), and \(c\). Make a program that creates a sequence \(y\) of \(a+b+c\) terms for which both of the following two conditions hold:

  • Condition 1. Exactly \(a\) terms of \(y\) are equal to \(1\), exactly \(b\) terms are equal to \(2\), and exactly \(c\) terms are equal to \(3\).
  • Condition 2. No two consecutive terms of \(y\) are equal. In other words, \(y[i]\neq y[i+1]\) for every \(i\in\{0,1,\dots, a+b+c-1\}\).

If there is no such sequence \(y\), then the program should print the message

The sequence y does not exist.

Example 1: 
Input: 
1 50 1
Output:
Sequence y does not exist.

Example 2: 
Input: 
3 5 4
Output:
2 3 2 3 2 1 2 1 2 3 1 3