- (10%)
Consider
the following code.
If
the input is
1 5 3 5
23 59 0 34
21 33 21 10
0 0 0 0
What will be the output?
// 11677 - Alarm Clock
#include
<iostream>
using std::cin;
using std::cout;
using std::endl;
struct TIME {
unsigned short h;
unsigned short m;
};
short diff (TIME t1, TIME t2) {
short result = (t2.h*60+t2.m) - (t1.h*60+t1.m);
if (result < 0)
result += 24*60;
return result;
}
int main() {
TIME t1, t2;
while (true) {
cin >> t1.h
>> t1.m >> t2.h >> t2.m;
if (t1.h*t1.h + t1.m*t1.m +
t2.h*t2.h + t2.m*t2.m == 0)
break;
cout << diff(t1, t2)
<< endl;
}
return 0;
}
- (10%) Determine
whether the syntax of the following code is correct or not. If it
is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Bitwise Operator & ASCII code
#include
<iostream>
int main()
{
char d = 'T';
std::cout << d;
d |= 'k';
d &=
0x57; std::cout
<< d;
std::cout << std::endl;
return 0;
}
- (10%)
Determine
whether the syntax of the following code is correct or not. If it
is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// switch case
#include
<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
char s[] = "UNIVERSITY";
short odd = 0, even = 0;
for (int i=0; i<sizeof(s); ++i)
switch (s[i] % 2) {
case
0:
++even;
break;
case
1:
++odd;
break;
}
cout << odd << even << endl;
return 0;
}
- (10%) Determine
whether the syntax of the following code is correct or not. If it
is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// cout starts printing until \0
#include
<iostream>
using std::cout;
using std::endl;
int main()
{
char s[] = "Lannister always pays his debts";
for (char* i=s; *i; ++i)
if (*i==' ')
*i = '\0';
void* p = s;
cout << static_cast<char*>(p) << endl;
p = static_cast<double*>(p) + 3;
cout << static_cast<char*>(p) << endl;
return 0;
}
- (10%)
What will the following code display?
// Static Variables
#include
<iostream>
using std::cout;
using std::endl;
void g()
{
static int a = 2020;
static int b = 109;
int c = a % b;
a = b; b = c;
std::cout << a << ' ';
}
int main()
{
for (int i=0; i<6; ++i)
g();
return 0;
}
- (10%)
Consider
the following code. If for input text "WrgdbLvPrqgdb", key values
-3, 23, 75 can all translate the text to
"TodayIsMonday", what should be the expression (6) that
you should supply in the enc()
function?
#include
<iostream>
#include <cstring>
using std::cout;
using std::cin;
using std::endl;
int enc(char c, int k)
{
int n;
if (65 <= c && c <=90)
{
n = c - 65;
n = _____(6)_____
while (n < 0)
n +=
26;
c = n + 65;
}
if (97<=c && c<=122)
{
n = c - 97;
n = _____(6)_____
while (n < 0)
n +=
26;
c = n + 97;
}
return c;
}
int main()
{
char msg[40];
int key;
cout << "Plaintext? ";
cin >> msg;
cout << "key? ";
cin >> key;
for (int i=0; i<strlen(msg); ++i)
cout <<
static_cast<char>( enc(msg[i], key) );
cout << endl;
return 0;
}
- (10%)
Determine
whether the syntax of the following code is correct or not. If it
is correct, predict its output. If it is incorrect, point out the
mistake(s).
// name[i] = *(name + i)
#include
<iostream>
using std::cout;
using std::endl;
int main()
{
int a[3][3] = { {8, 1, 6}, {3, 5, 7}, {4, 9, 2} };
int* b[3] = { a[0], a[1], a[2] };
cout << *(a[0 + 1]) << *(a[0] + 1)
<< *(a[0]) + 1 << endl;
cout << b[1][0]
<< b[0][1] << b[0][0] + 1 <<
endl;
return 0;
}
- (10%)
What will the following code display?
//
Recursive Function
#include
<iostream>
using std::cout;
using std::endl;
void d2h(int n, int b)
{
char c = n % b;
if (n >= b)
d2h(n / b, b);
c = c<10?c+48:c+55;
cout << c;
}
int main()
{
d2h(88, 8); cout << endl;
d2h(255, 16); cout << endl;
d2h(288, 17); cout << endl;
return 0;
}
- (10%)
Determine
whether the syntax of the following code is correct or not. If it
is correct, predict its output. If it is incorrect, point out the
mistake(s).
// Pointer to Pointer
#include
<iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int a[3][3] = { {8, 1, 6}, {3, 5, 7}, {4, 9, 2} };
int* p1 = a[0];
int** p3 = &p1;
cout << (*p1)+1 << *(p1+1) <<
*(*p3+2) << endl;
return 0;
}
- (10%)
What will the following code display?
// Tree
#include
<iostream>
struct TREE
{
TREE* left;
char ch;
TREE* right;
};
void print(TREE* p)
{
if (p != NULL)
{
print(p->left);
std::cout << p->ch;
print(p->right);
}
}
TREE* createTree()
{
TREE temp = {NULL, 'G', NULL};
TREE* g = new TREE( temp );
TREE* c = new TREE; c->left = NULL; c->ch='C'; c->right
= g;
TREE* a = new TREE; a->left = NULL; a->ch='A'; a->right
= c;
return a;
}
unsigned max(unsigned a, unsigned b)
{ return (a>b)?a:b; }
unsigned h(TREE* p)
{
if (p == NULL)
return 0;
else
return max(h(p->left),
h(p->right)) + 1;
}
int main()
{
TREE* p = NULL;
p = createTree();
std::cout << h(p);
p = p->right; std::cout << h(p);
p = p->right; std::cout << h(p) <<
std::endl;
return 0;
}