[試題] 101下 劉邦鋒 高等程式設計 期中考+部分解答

作者: rod24574575 (天然呆)   2016-04-23 18:45:18
課程名稱︰高等程式設計
課程性質︰選修
課程教師:劉邦鋒
開課學院:電資學院
開課系所︰資工所、網媒所
考試日期(年月日)︰2013.04.16
考試時限(分鐘):
試題 :
Advanced Computer Programming 2013
Midterm Examination 04/16/2013
The first part of the test is a written test. Please answer the following
problem with sufficient details. (50%)
1. Explain the functionalities of gcc compiler options -c, -o , -E, -g, and -O.
Also please describe in what situation you need to use them. (10%)
Ans: Each option for [2%]
○ -c: Compile or assemble the source file but not link, so the output
will be an object file.
○ -o: Specify the name of output.
○ -E: Stop after preprocessing stage.
○ -g: Produce debug information in the operating system's native
format.
○ -O: Optimize.
2. What is the output of the following program? Please explain in the answer
in details. (5%)
#include <iostream>
using namespace std;
int main()
{
int i, j, *temp;
int a[5] = {3, 5, 1, 2, 4};
int *ptr[5] = {&(a[0]), &(a[1]), &(a[2]), &(a[3]), &(a[4])};
int ref0 = a[0];
int ref1 = a[1];
int ref2 = a[2];
int ref3 = a[3];
int ref4 = a[4];
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (*(ptr[j]) > *(ptr[j + 1])) {
temp = ptr[j];
ptr[j] = ptr[j + 1];
ptr[j + 1] = temp;
}
for (i = 0; i < 5; i++)
cout << a[i] << ' ' << *(ptr[i]) << endl;
cout << ref0 << endl;
cout << ref1 << endl;
cout << ref2 << endl;
cout << ref3 << endl;
cout << ref4 << endl;
}
Ans: ○ Each kind of output (a[i], ptr[i], refs) for [1%]*
3 1
5 2
1 3
2 4
4 5
3
5
1
2
4
○ Explanation. [2%]
Sort the pointers by its dereferenced value. The value in array a
is not changed, so as the references.
3. In 1968 Professor Edsger W. Dijkstra wrote the following. Please describe
his argument why he is against the use of goto. (10%)
My second remark is that our intellectual powers are rather geared to
master static relations and that our powers to visualize processes
evolving in time are relatively poorly developed. For that reason we
should do (as wise programmers aware of our limitations) our utmost to
shorten the conceptual gap between the static program and the dynamic
process, to make the correspondence between the program (spread out in
text space) and the process (spread out in time) as trivial as possible.
Ans: We can tread a block structure as a unit of code, making programmer
easier to maintain. Using goto will break such block structure.
4. Describe the reason why we need to write the short statement in the then
part. (5%)
Ans: See section 3.4.5 in "programming-style.pdf" [5%]
// Ahbong's comment
I think that we should also keep "else" part short for the same reason.
There are two acceptable style:
○ Use guard condition, then there's no "else" part.
○ "if" part should be the normal case and "else" part should be the
abnormal case. For switch statement the most frequent condition
should be at the top, and so on for other conditions.
5. Describe the caching behavior and performance difference in the following
code when compiled with and without XY. What kind of tool can you use to
confirm the caching behavior? (10%)
#include <stdio.h>
#define DIMX 1000
#define DIMY 100000
int array[DIMX][DIMY];
int main()
{
int x, y;
#ifdef XY
for (x = 0; x < DIMX; x++)
for (y = 0; y < DIMY; y++)
array[x][y] = x + y;
#else
for (y = 0; y < DIMY; y++)
for (x = 0; x < DIMX; x++)
array[x][y] = x + y;
#endif
return 0;
}
Ans: ○ State that array in C/C++ is row-major order [4%]*
○ Code compiled with defining XY has less cache miss and thus has
better performance. [4%]*
○ valgrind [2%]*
6. What is the output of the following program if we run it in a UNIX
environment? Please explain the answer in details. (10%)
#include <stdio.h>
#include <assert.h>
void dump_file(char *filename, char *mode)
{
int c;
int count = 0;
FILE *fp = fopen(filename, mode);
assert(fp != NULL);
while ((c = fgetc(fp)) != EOF) {
printf("%02x ", c);
count++;
if (count % 8 == 0)
putchar('\n');
}
fclose(fp);
printf("\nThere are %d bytes in file %s\n", count, filename);
return;
}
int main(void)
{
FILE *fp;
char c;
fp = fopen("binary", "wb");
assert(fp != NULL);
fputs("hello\n", fp);
fclose(fp);
fp = fopen("text", "wt");
assert(fp != NULL);
fputs("hello\n", fp);
fclose(fp);
dump_file("binary", "rb");
dump_file("text", "rb");
dump_file("text", "rt");
return 0;
}
Ans: ○ "printf("%02x", c)" prints the ASCII code in hex format of each
character in file. [2%]*
○ Each of the output ("binary" in "rb", "text" in "rb", "text" in
"rt") for [2%]*
All of the three dump_file say that "There are 6 bytes in file ***"
○ Explanation [2%]*
The treatment of binary and text file are the same in UNIX
environment.
The second part of the test is the programming part. (50%)
1. Please download the problem description and the buggy code. Then fix the
buggy code and upload to judgegirl. (15%)
2. Please enhance the BasicLinkedList class with a new method called insert.
The insert function will insert a key i into the list before the first key
that is no less than it. For example, if the list is now 1, 2, and 3. Then
after inserting 3 then list will become 1, 2, 3, and 3. Note that if we
keep inserting using insert, the list will be sorted all the time. The
prototype of insert is as follows.
void insert(const int i);
You should use public inheritance to build the class. (15%)
3. Please build a class Date, The class must support the following member
functions. (20%)
a. Date::Date();
A default constructor that initializes the date to 01/01/1970.
b. Date::Date(const int y, const int m, const d);
A constructor to initialize it to year, month, and date. You may assume
that all input are valid.
c. void Date::add(const int n);
Add n days to the current date. For example, if date is 02/28/2000, then
after adding one day, i.e. date.add(1), date becomes 02/29/2000. Note
that n could be negative, but it is between -1000000 and 1000000. Also
note that the year will never become non-positive even when the n is
negative. We will use the standard rule to determine whether a year is
a leap year.
d. int between(const Date &d) const;
Return the number of days between two Date, For example, it should
return 366 between 01/01/2000 and 01/01/2001.
e. void Date::setmode(DateMode m);
This is a class method that controls the printing format. If the mode
has been set using Date::setmode(dayfirst), the date will be printed as
day first, like 29/02/2000. If the mode has been set using
Date::setmode(monthfirst) then the date will be printed as 02/29/2000.
If the mode has been set as Date::setmode(text), the output should be
February 29, 2013. The default mode is monthfirst. monthfirst, dayfirst,
and text should be defined as an enum type like this.
enum DateMode {dayfirst, monthfirst, text};
f. void print() const;
The following is an example on how to control the output.
#include <iostream>
#include "date.h"
using namespace std;
int main()
{
Date date(2000, 2, 29);
date.print();
Date::setmode(dayfirst);
date.print();
Date::setmode(text);
date.print();
}
The output is as follows.
2/29/2000
29/2/2000
February 29, 2000

Links booklink

Contact Us: admin [ a t ] ucptt.com