科目名稱:程式設計 | 開課系所:資訊工程 學系 | 考試日期 | 2014.4.22 | ||
系所別: |
年級: |
學號: |
姓名: |
考試時間 | 14:10-16:00 |
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).
// Pass-by-Reference (P.267)
#include <iostream>
void swap(int &i, int j)
{
int temp;
temp = i;
i = j;
j = temp;
return;
}
int main()
{
int a = 4;
int b = 19;
std::cout << a << b << std::endl;
swap(&a, b);
std::cout << a << b << std::endl;
return 0;
}
(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).
// Function Overloading (P.310)
#include <iostream>
using std::cout;
void star(int n, int c)
{
for (int i=0; i<n; i++)
cout << static_cast<char>(c);
}
void star(int n, char c)
{
for (int i=0; i<n; i++)
cout << c;
}
int main()
{
star(3, 50);
star(5, 'A');
return 0;
}
// Arrays of Pointers to Functions (P.301)
#include <iostream>
using std::cout;
int f0(int n)
{
return 1;
}
int f1(int x)
{
return x;
}
int f2(int x)
{
return x*x;
}
int f3(int x)
{
return x*x*x;
}
int main()
{
int a[] = { 1, 2, 3, 4, 5 };
int (*pfunc[4]) (int) = { f0, f1, f2, f3 };
int (*pf) (int) = *(pfunc + 2);
int sum = 0;
for (int i=0; i< sizeof(a)/sizeof(a[0]); i++)
sum += pf(a[i]);
std::cout << sum << std::endl;
return 0;
}