- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Two's Complement
#include <iostream>
int main()
{
signed short m = 1<<15;
std::cout << m << std::endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Increment operator
#include <iostream>
int main()
{
int i;
int sum = 0;
for (i=1; i<6; i++)
sum += i++;
std::cout << sum << std::endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Bitwise-operation
#include <iostream>
int main()
{
signed short n = ~0;
std::cout << n << std::endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Counting letters in a string
#include <iostream>
int main()
{
char str[] = "The Teeth of the Tiger";
char c;
int i, count(0);
for (i=0; i < sizeof(str); i++)
{
c = str[i];
if ('a' <= c && c <= 'z')
count++;
}
std::cout << count << std::endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Nested-loop
#include <iostream>
using std::cout;
using std::endl;
void Print(int n)
{
int i,j;
for (i=0; i<n; i++) cout << '*'; cout << endl;
for (i=1; i<n-1; i++)
{
cout << '*';
for (j=1; j<n-1; j++) cout << ' ';
cout << '*' << endl;
}
for (i=0; i<n; i++) cout << '*'; cout << endl;
}
int main()
{ Print(3);
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Two-dimensional array
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int i, j;
int A[3][3] = { { 1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int B[9] = { 0 };
for (i=0; i<3; i++)
for (j=0; j<3; j++)
B[j * 3 + i] = A[i][j];
for (i=0; i<9; i++)
cout << B[i];
cout << endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Default values of function parameters (P.302)
#include <iostream>
int pdo_it(int a, int b, int c = 30)
{ return a + b + c; }
int pdo_it(int a, int b)
{ return a * b; }
int main()
{
std::cout << pdo_it(12, 18) << std::endl;
std::cout << pdo_it(2012, 12, 18) << std::endl;
std::cout << pdo_it( pdo_it(20,12), 12, 18 )
<< std::endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Pointers to Functions
#include <iostream>
int sum(int a, int b)
{ return a + b; }
int product(int a, int b)
{ return a * b; }
int main()
{
int (*pfun)[2](int, int) = { sum, product };
std::cout << pfun[1](2, 3) << std::endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Traverse a linked-list
#include <iostream>
struct ListElement
{
int value;
ListElement* pNext;
};
void Print(ListElement p)
{
std::cout << p.value;
if (p.pNext != NULL)
Print(*p.pNext);
// Note the precedence of these two operators
}
int main()
{
ListElement LE1 = { 1, NULL };
ListElement LE2 = { 3, &LE1 };
ListElement LE3 = { 2, &LE2 };
ListElement LE4 = { 4, &LE3 };
Print(LE4);
std::cout << std::endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Representing a polynomial with a linked-list
#include <iostream>
struct ListElement
{
int value;
ListElement* pNext;
};
void Print(ListElement* p)
{
int i;
for (i = 0; p != NULL; p = p->pNext, i++)
{
std::cout << "+" << p->value;
if (i > 0)
{
std::cout << "X";
if (i > 1)
std::cout << "^" << i;
}
}
std::cout << std::endl;
}
int main()
{
ListElement LE4 = { 1, NULL };
ListElement LE3 = { 4, &LE4 };
ListElement LE2 = { 3, &LE3 };
ListElement LE1 = { 2, &LE2 };
Print(&LE1);
return 0;
}