Date: February 24th, 2012
Time: 09:10-11:00
Open book; turn off computer & mobile phone
// character pointer
#include <iostream>
using std::cout;
int main()
{
char *name = "Europe";
cout << sizeof(name) << '\n';
return 0;
}
// Precedence (P.77)
#include<iostream>
using std::cout;
using std::endl;
int main()
{
int a=1, n=3;
int sum=0;
while ( --n >= 1)
{
sum+=a;
a = (a * 10) + a % 10;
}
cout << sum << endl;
return 0;
}
// ASCII code of a character
#include <iostream>
void bar(short n)
{
for (short i=0; i<n; i++)
printf("%c", 73); // 'A' == 65
printf("\n");
return;
}
int main()
{
short b[] = { 5, 4, 3, 5, 4, 3 };
int i;
for (i=0; i<= 4; i++)
{
printf("%2d ", b[i]);
bar(b[i]);
}
return 0;
}
// nested for-loop
#include <iostream>
using std::cout;
int main()
{
bool prime;
int i, j;
cout << "These are prime numbers:\n2 ";
for (i=3; i < 20; i+=2)
{
prime = true;
for (j=3; j < i; j++)
if (i / j * j == i) // Is i dividable by j?
prime = false;
if (prime)
cout << i << ' ';
}
return 0;
}
// array
#include <iostream>
using std::cout;
int main()
{
const int MAX = 100;
bool prime[MAX] = { false, false };
int i, j, count = 0;
for (i=2; i < MAX; i++)
prime[i] = true;
for (i=2; i < MAX; i++)
for (j=i*2; j < MAX; j+=i)
prime[j] = false;
for (i=2; i < MAX; i++)
{
if (prime[i])
{
cout << i << '\t';
if (count++ % 5 == 0) cout << '\n';
}
}
return 0;
}
#include <iostream>
using std::cout;
void swap(int& i, int &j)
{
j ^= i; i ^= j; j ^= i; // eXclusive OR
}
int main()
{
int data[] = { 2, 4, 6, 1, 3, 5, 7 };
int i, j;
for (i=0; i<6; i++)
for (j=i+1; j<7; j++)
if (data[i] > data[j])
swap(data[i], data[j]);
for (i=0; i<7; i++)
cout << data[i];
return 0;
}
// Linked-list
#include <iostream>
using std::cout;
struct Node
{
int value;
Node* next;
};
int main()
{
int a[] = { 3, 5, 1, 7, 2, 4, 6};
Node* p;
Node* current;
Node* previous;
Node* head = new Node;
head->value = a[0]; head->next = NULL;
int i, n;
n = sizeof(a) / sizeof(a[0]);
for (i=1; i < n; i++)
{
p = new Node;
p->value = a[i];
if (head->value < a[i])
{
p->next = head;
head = p;
continue;
}
current = previous = head;
while (current != NULL && current->value > a[i])
{
previous = current;
current = current->next;
}
if (current == NULL)
{
previous->next = p;
p->next = NULL;
}
else
{
previous->next = p;
p->next = current;
}
}
p = head;
while (p)
{
cout << p->value;
p = p->next;
}
cout << '\n';
return 0;
}
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
int main()
{
int n = 24;
int odd = 0;
int even = 0;
while (n > 0)
{
switch (n % 2)
{
case 0:
even++;
break;
default:
odd++;
}
n >>= 1;
}
cout << "even=" << even << endl;
cout << "odd=" << odd << endl;
return 0;
}
// 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=24;
swap1(i,j); print(i,j);
swap2(i,j); print(i,j);
swap3(i,j); print(i,j);
}