Date: January 13th, 2009
Time: 14:10-17:00
Open book; turn off computer & mobile phone
#include <iostream>
using std::cout;
using std::endl;
int main()
{
cout << 3 << 3 << 3 << endl;
cout << (3 << 3 << 3) << endl;
cout << 3 << (3 << 3) << endl;
cout << (3 << (3 << 3)) << endl;
}
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int i, j;
for (i=0; i<6; i++)
for (j=0; j<6; j++);
cout << "A";
cout << endl;
return 0;
}
#include <iostream>
using namespace std;
int main( void )
{ int i=0, nums[10]={ 1, 8, 9, 5 };
while( i<10 )
{
if( i%2 )
{
++i;
continue;
}
nums[i++]=i;
}
for (i=0;i<10;)
cout << nums[i++] << " ";
cout << endl;
return 0;
}
// pointer to function
#include <iostream>
using std::cout;
using std::endl;
int sum(int a, int b)
{ return a+b;
}
int product(int a, int b)
{ return a*b;
}
int main()
{
int *pfun(int, int) = sum;
cout << pfun(4, 8) << endl;
pfun = product;
cout << pfun(4, 8) << endl;
}
Show the output of the following program.
#include <iostream>
#include <windows.h>
int main()
{
RECT aRect = { 0, 0, 100, 100 };
RECT* pRect = &aRect;
pRect.top += 10;
std::cout << pRect.top;
}
// bin2dec.c
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char b[] = "11100110000";
int n=0, i=0;
while (b[i])
n = (n<<1) + b[i++] - '0';
cout << n << endl;
return 0;
}
#include <iostream>
using std::endl;
using std::cout;
int main()
{
char a[]="HELLO, WORLD!";
const int k = sizeof(a);
int sum = 0;
for(int i=0;i<k;i++)
sum += a[i];
cout << "k= " << k << endl;
cout << "sum= " << sum << endl;
return 0;
}
#include <iostream>
#include <windows.h>
struct ListElement
{
int value;
ListElement* pNext;
}
int main()
{
ListElement L1 = { 1 };
L1.pNext = NULL;
std::cout << L1.value << std::endl;
}
// Pass-by-value
#include <iostream>
using std::cout;
using std::endl;
void test(char p[])
{ cout << p ;
}
int main()
{
char *name = "####";
while (*name)
test(name++);
cout << endl;
}
// Pass-by-reference
#include <iostream>
using std::cout;
using std::endl;
void print(int i, int j)
{
cout << "i = " << i << "\t j = " << j << endl;
}
void swap1(int a, int b)
{ int temp;
temp = a; a = b; b = temp;
}
void swap2(int &a, int &b)
{ int temp;
temp = a; a = b; b = temp;
}
void swap3(int &a, int b)
{ int temp;
temp = a; a = b; b = temp;
}
int main()
{
int i=2, j=1;
swap3(i,j); print(i,j);
swap2(i,j); print(i,j);
swap1(i,j); print(i,j);
}