Date: December 17th, 2008
Time: 14:10-17:00
Open book; turn off computer & mobile phone
#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;
}
Check whether the following program has any syntax mistake. If yes, point out the mistakes and correct them; if no, predict the output of the
program.
#include <iostream>
using namespace std;
int main( void )
{ int *ptrA, *ptrB, tmp, A=5, B=6;
ptrA = &A;
ptrB = &B;
tmp = ptrA;
ptrA = ptrB;
ptrB = tmp;
cout << "A = " << *ptrA << ", B = " << *ptrB << endl;
return 0;
}
#include <iostream>
int main() // Understanding bitwise operators.
{
unsigned s = 2008;
int i = (s >> 3) ^ ~(~0 << 2);
std::cout << i << std::endl;
}
#include <iostream>
using namespace std;
int main( void )
{ int i=0, nums[10]={ 0 };
while( i<10 )
{
if( i==5 )
{
++i;
continue;
}
nums[i++]=i;
}
for (i=0;i<10;)
cout << nums[i++] << " ";
cout << endl;
return 0;
}
// bubble_sort.c
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int i, j, *temp;
int a[] = { 1, 9, 6, 3}; // data
const int K = sizeof(a) / sizeof(a[0]);
int *b[K]; // pdata
// Initialize pdata
for(i=0;i<K;i++)
b[i] = &a[i];
// Sort pdata by value which is pointed by pdata
for(i=0;i<K;i++)
for(j=K-1;j>i;j--)
if (b[j-1] > b[j])
{
temp = *(b[j-1]);
*(b[j-1]) = *(b[j]);
*(b[j]) = temp;
}
// print values of data array & values pointed by pdata
for(i=0;i<K;i++)
cout << a[i] << " " << *(b[i]) << endl;
return 0;
}
// bin2dec.c
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char b[] = "1100111100";
int n=0, i=0;
while (b[i])
n = (n<<1) + b[i++] - 48;
cout << n << endl;
return 0;
}
Check whether the following program has any syntax mistake. If yes, point out the mistakes and correct them; if no, predict the output of the
program.
#include <iostream>
int main()
{
for (int i = 6; i < 10; i--)
{
if (i==3)
break;
std::cout << i << std::endl;
}
}
#include <iostream>
using std::cout; using std::endl;
int main()
{
static int MAX = 5;
int i,j,k;
for(k=0; k<MAX; k++) ;
for(i=1; i<=MAX; i++)
{
for(j=MAX; j>0; j--)
if(i==j)
cout << "O";
else
cout << "X";
cout << endl;
}
return 0;
}
#include <iostream>If we want to obtain the following results, what expressions should be filled into the three blanks, respectively?
using std::cout; using std::cin; using std::endl;
int main()
{
int i,j, N;
cout << "N = ? ";
cin >> N;
for(i=1; i<=N; i++)
{
for(j=1; j<=N; j++)
if(i== (1) || i== (2) )
cout << "O";
else
cout << "X";
cout << endl;
}
return 0;
}
N = ? 5
OXXXO
XOXOX
XXOXX
XOXOX
OXXXO
N = ? 6
OXXXXO
XOXXOX
XXOOXX
XXOOXX
XOXXOX
OXXXXO