- (10%) Determine whether the following code has
syntax erros or not.  If it is correct, predict its output. 
If it is incorrect, point out the mistake(s).
    
    // Conditional Operator (P.133)
    #include <iostream>
using std::cout;
using std::endl;
    
int main()
{
    int a = 3;
    cout << (--a ? a : a=2);
    cout << --a << endl;
    return 0;
}
    
     
  - (10%) Determine whether the following code has
syntax erros or not.  If it is correct, predict its output. 
If it is incorrect, point out the mistake(s).
    
// Operator Precedence (P.77)
#include <iostream>
using std::cout;
using std::endl;
    
int main()
{
    int a = 3;
    cout << (a-=2 ? a : a=2);
    cout << --a << endl;
    return 0;
}
    
      
  - (10%) Determine whether the following code has
syntax erros or not.  If it is correct, predict its output. 
If it is incorrect, point out the mistake(s).
    
    // Bitwise Operators (P.82)
// std::hex (P.148)
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
using std::hex;
    
int main()
{
    int a = (((16 << 8) | 20 ) << 8 | 32 ) << 8 | 36;
    cout << hex << a << endl;
    return 0;
}
    
   
  - (10%) Determine whether the following code has
syntax erros or not.  If it is correct, predict its output. 
If it is incorrect, point out the mistake(s).
    
    //
strlen vs. sizeof (P.190)
    #include <iostream>
#include <cstring>
using std::cout;
using std::endl;
    
int main()
{
    char str[] = "National Chi Nan University";
    str[16] = '\0';
    char* str1 = &str[9];
    cout << sizeof(str) << '\t'
        << strlen(str1) << endl;
    return 0;
}
    
   
  - (10%) Determine whether the following code has
syntax erros or not.  If it is correct, predict its output. 
If it is incorrect, point out the mistake(s).
    
    //
Type
Conversion (P.78)
// switch (P.135)
    #include <iostream>
using std::cout;
    
int main()
{
    int even = 0, odd = 0;
    char str[] = "NCNU"; // 'A' == 65, 'a' == 97
    for (int i=0; i<strlen(str); i++)
        switch ( str[i] % 2 )
        {
            case 0:
                even++;
                break;
            case 1:
                odd++;
                break;
        }
    cout << "odd = " << odd << '\n'
         << "even = " << even << '\n';
    return 0;
}
    
    
   
  - (10%) Determine whether the following code has
syntax erros or not.  If it is correct, predict its output. 
If it is incorrect, point out the mistake(s).
    
    // Pointers and Arrays (P.194)
    #include <iostream>
using std::cout;
    
int main()
{
    int data[] = { 1, 2, 3, 2, 5};
    int* pdata = data;
    cout << *pdata <<  *(pdata + 2) << endl;;
    return 0;
}
    
     
  - (10%) Determine
whether the following code has
syntax erros or not.  If it is correct, predict its output. 
If it is incorrect, point out the mistake(s).
    
    // Recursive Function Calls (P.285)
    #include <iostream>
using std::cout;
    
void octal(int n)
{
    if (n >= 8 )
       octal(n / 8);
    cout << n % 8;
}
    
int main()
{
    octal(20);
    return 0;
}
     
  - 
    
(10%) Determine whether the following code has syntax erros or
    not.  If it is correct, predict its output.  If it is
    incorrect, point out the
    mistake(s).
// Pointers as Arguments to a Function (P.262)
#include <iostream>
void swap(int* i, int j)
{
    int temp;
    temp = *i;
    *i = j;
    j = temp;
    return;
}
int main()
{
    int a = 4;
    int b = 19;
    std::cout << a << b << std::endl;
    swap(&a, b);
    std::cout << a << b << std::endl;
    return 0;
}
   
  - 
    
(10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    
    // Function Overloading (P.310)
#include <iostream>
using std::cout;
void star(int n)
{
    for (int i=0; i<n; i++)
        cout << '*';
}
void star(int n, char c)
{
    for (int i=0; i<n; i++)
        cout << c;
}
int main()
{
    star(3);
    star(5, 'A');
    return 0;
}
   
  - (10%)
    Determine
whether the following code has
syntax erros or not.  If it is correct, predict its output. 
If it is incorrect, point out the mistake(s).
    
// Arrays of Pointers to Functions (P.301)
#include <iostream>
using std::cout;
int f0(int n)
{
    return 1;
}
int f1(int x)
{
    return x;
}
int f2(int x)
{
    return x*x;
}
int f3(int x)
{
    return x*x*x;
}
int main()
{
    int a[] = { 1, 2, 3, 4, 5 };
    int (*pfunc[4]) (int) = { f0, f1, f2, f3 };
    int (*pf) (int) = pfunc[2];
    int sum = 0;
    for (int i=0; i< sizeof(a)/sizeof(a[0]); i++)
        sum += pf(a[i]);
    std::cout << sum << std::endl;
    return 0;
}