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/cn.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