NTU-Exam 板


LINE

課程名稱︰高等程式設計 課程性質︰選修 課程教師:劉邦鋒 開課學院:電資學院 開課系所︰資工所、網媒所 考試日期(年月日)︰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 --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.231.132.45
※ 文章網址: https://webptt.com/m.aspx?n=bbs/NTU-Exam/M.1461408321.A.B99.html
1F:→ rod24574575 : 已收資訊系! 04/23 18:46







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:WOW站內搜尋

TOP