- (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).
#include <iostream>
using std::cout;
using std::endl;
int main()
{
unsigned char n = 255;
cout << static_cast<unsigned short>(n++) << endl;
cout << static_cast<unsigned short>(n) << 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).
#include <iostream>
int main()
{
int i;
for (i=1; i<8; i++)
{
if (i>2)
{
if (i==4) continue;
else break;
}
std::cout << i << 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).
#include <iostream>
int main()
{
int n = 6;
while (true)
{
std::cout << n << ' ';
if (n==1) break;
if (n % 2)
n = 3 * n + 1;
else
n /= 2;
}
std::cout << std::endl;
return 0;
}