科目名稱:程式設計 | 開課系所:資訊工程 學系 | 考試日期 | 2016.4.7 | ||
系所別: |
年級: |
學號: |
姓名: |
考試時間 | 08:30-10:30 |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 |
(10%) Determine whether the following code has syntax errors or not. If it is correct, predict its output. If it is incorrect, point out the mistake(s).
// Short-Circuit
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int i, j, n = 0, m = 0;
for (i=0; i<10; i++)
for (j=0; j<10; j++)
if (i<j && n++ > 0)
m++;
cout << n << m << endl;
return 0;
}
(10%) Determine whether the following code has syntax errors or not. If it is correct, predict its output. If it is incorrect, point out the mistake(s).
// Math Library
// Type Conversion (P.63)
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;
int main()
{
cout << (9/4)+1 << '\t'
<< floor(9/4)+1 << '\t'
<< (-9/4)+1 << '\t'
<< floor(-9/4)+1 << '\t'
<< (-9.0/4)+1 << '\t'
<< floor(-9.0/4)+1 << '\n';
return 0;
}
// Transposition Cipher
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
int main()
{
const unsigned short N = 4;
char matrix[N * N + 1];
char plaintext[] = "Glp oerFo iogAlolabo iunGlt m tspscsa khrblAkaaA";
char ciphertext[ sizeof(plaintext) ];
char *p = plaintext;
unsigned i, j;
while (p < plaintext + strlen(plaintext) )
{
for (i=0; i<N; i++)
for (j=0; j<N; j++)
{
matrix[i*N + j] = *p ? *p : 'A';
++p;
}
for (j=0; j<N; j++)
for (i=0; i<N; i++)
cout << matrix[i*N + j];
}
cout << endl;
return 0;
}