] > midterm exam

exit midterm exam

NAME¯

 
CSE 230: Mid Term Exam
 
Tu, April 29, 3:30—4:48
Open Notes, Open Books
The exam consists of five¯ problems
Answers not appearing in designated spaces WILL NOT be graded

Problem #1 (10 points)
Show the output of the following program.
#include <iostream> 
using namespace std; 
 
void clear(){ 
   cout << ”x1b[2J”; 
   cout << ”x1b[0;0H”; 
} 
void moveTo( double x, double y ){ 
   cout << ”x1b[” << y 
        << ”;” << x << ”H”; 
} 
 
int main() 
{ 
  clear(); 
  for(int i=1; i<9; i++){ 
     for(int j=1; j<i; j++){ 
       moveTo(i,j); 
       cout << i; 
   } } 
  for(int i=1; i<9; i++){ 
     for(int j=1; j<i; j++){ 
       moveTo(j+10,i); 
       cout << i; 
   } } 
  cout << endl; 
  return 0; 
}

Problem #2 (10 points)
Show the output of the following program.
#include <iostream> 
using namespace std; 
 
double c2F(int); 
void bottles(int); 
void convert(int n, int base); 
 
int main () 
{ 
  forint i=3; i>0; i ){ 
    bottles(i); 
  } 
  cout << c2F(0) 
       << ”;  ” 
       << c2F(100) 
       << endl << endl; 
  forint i=0; i<11; i++ ){ 
      convert(10,i); 
  } 
  return 0; 
} 
double c2F(int deg) 
{ 
   cout << ”Celsius(” << deg << ”) = ”; 
   return  1.8  deg + 32; 
} 
void bottles(int n) 
{ 
   cout << n << ” bottles of beer on the wall.” << endl 
        << ”Take one down and pass it around.” << endl 
        << n << ” bottles of beer on the wall.” 
        << endl << endl; 
} 
void convert(int n, int base) 
{ 
   if( (n >= 0) && (base > 1) && (base < 10) ){ 
     int result = 0, 
            pos = 1; 
     cout << n << ” = ”; 
     while (n > 0) { 
        result += pos  (n % base); 
        n = n / base; 
        pos = 10; 
     } 
     cout << result << ” base ” << base << endl; 
   } 
}

Problem #3 (10 points)

Provide the definition of the missing procedure so that program below will produce well formatted multiplication tables. For instance, on input value 9 the program should produce the following outcome.

Please provide an integer in the range of 1 to 9: 9  
     1    2    3    4    5    6    7    8    9  
  +----+----+----+----+----+----+----+----+----+  
1 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 |  
  +----+----+----+----+----+----+----+----+----+  
2 |  2 |  4 |  6 |  8 | 10 | 12 | 14 | 16 | 18 |  
  +----+----+----+----+----+----+----+----+----+  
3 |  3 |  6 |  9 | 12 | 15 | 18 | 21 | 24 | 27 |  
  +----+----+----+----+----+----+----+----+----+  
4 |  4 |  8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 |  
  +----+----+----+----+----+----+----+----+----+  
5 |  5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 |  
  +----+----+----+----+----+----+----+----+----+  
6 |  6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 |  
  +----+----+----+----+----+----+----+----+----+  
7 |  7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 |  
  +----+----+----+----+----+----+----+----+----+  
8 |  8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 |  
  +----+----+----+----+----+----+----+----+----+  
9 |  9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 |  
  +----+----+----+----+----+----+----+----+----+

#include <iostream> 
using namespace std; 
void line( int ); 
void rows( int ); 
 
int main () 
{ 
  int n; 
  cout << ”Please provide an integer in the range of 1 to 9: ”; 
  cin >> n; 
  if( (n < 1) || (n > 9)){ return 1; } 
 
  cout << ”    ”; 
  forint i=1; i<=n; i++ ){ 
     if( i <10 ){ cout << ” ” ; } 
     cout << i << ”   ”; 
  } 
  cout << endl; 
  line( n ); 
  rows(n); 
  return 0; 
} 
void line( int n ){ 
  cout << ”  ”; 
  forint i=1; i<=n; i++ ){ 
     cout << ”+”; 
  } 
  cout << ”+” << endl; 
}

Problem #4 (10 points)
Provide the necessary definitions so that the program below will produce the following output. Your definitions should include no more than three functions, and at most one function may use the write operator <<.
1 2.3 abc  
1 2.3 abc  
1 2.3

#include <iostream> 
using namespace std; 
//missing definitions 
int main(){ 
  sort(  1,   2.3, ”abc”); 
  sort(2.3, ”abc”,     1); 
  sort(2.3,     1       ); 
  return 0; 
}

Problem #5 (10 points)
For each of the following cases, show the output of the given segment of code.
  1. int main(){ 
      int  x1 = 5; 
      int  x2 = 3; 
      int &y1 = x2; 
      int y2 = &x1; 
      y1 = x2  y2  x1; 
      y2 = &x1; 
      cout <<             y1 << ’n’ 
           <<  x2  y2  x1 << ’n’; 
      return 0; 
    }
  2. void wrt(int a, int b){ 
      cout << a << ’ ’ << b << ’n’; 
    } 
    void f(int &a, int b){ 
      a = a  b; 
      b = a + b; 
      wrt(a,b); 
    } 
    int main(){ 
        int  x = 3, y = x; 
      f(x,&y);  wrt(x,y); 
      f(y,&x);  wrt(x,y); 
      return 0; 
    }