| 科目名稱:
程式設計 |
第一次期中考 | 開課系所:資工系 | 考試日期 | 2019.2.27 | |
| 系所別: |
年級: |
學號: |
姓名: |
考試時間 | 08:30-10:30 |
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
(10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is incorrect, point out the mistake(s).
// Recursion: ex06_45.cpp
#include <iostream>
using std::cout;
using std::endl;
int mystery(int, int); // function prototype
int main()
{
int x = 20, y = 19;
cout << "The result is " << mystery(x, y) << endl;
return 0;
}
int mystery(int a, int b)
{
if (b==1) // base case
return a;
else // recursion step
return a + mystery(a, b-1);
}
(10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is incorrect, point out the mistake(s).
// Recursion
#include <iostream>
using std::cout;
void f(int a[], int i)
{
if (a[i] > 0)
{
f(a, i*2);
cout << a[i] << ' ';
f(a, i*2+1);
}
}
int main()
{
int a[32] = {15, 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 };
f(a, 1);
cout << std::endl;
return 0;
}