精華區beta NTU-Exam 關於我們 聯絡資訊
課程名稱︰計算機概論 課程性質︰必修 課程教師︰陳敏璋 開課學院:工學院 開課系所︰材料系 考試日期(年月日)︰101.6.15 考試時限(分鐘):180 分鐘 是否需發放獎勵金:是 (如未明確表示,則不予發放) 試題 : 1. (5%) do while 迴圈 #include <iostream> #include <cstdlib> using namespace std; int main(void) { int i=0; do{ if(i%2==1) cout << i << "is an odd number" << endl; i++; }while(i<10); system("pause"); return 0; } 請寫出此程式輸出的結果 2. (5%) 指標 #include <iostream> #include <cstdlib> using namespace std; int main(void) { int x=1,y=2,z=3; int *ip; ip=&x; z=*ip; *ip=y; ip=&y; *ip=z; cout << "*ip=" << *ip << endl; system("pause"); return 0; } 請寫出此程式輸出的結果 3. (5%) 條件運算子 #include <iostream> #include <cstdlib> using namespace std; int main(void) { int a=1,b=11,z; z=(a>b)?a:b; cout << z << endl; system("pause"); return 0; } 請寫出此程式輸出的結果 4. (5%) 指標與陣列 #include <iostream> #include <ctsdlib> using namespace std; int main(void) { int a[5],i,f; for(i=0;i<=4;i++) a[i]=5-i; f=*a+a[1]+*(a+2)+(*a+3); cout << f << endl; system("pause"); return 0; } 請寫出此程式輸出的結果 5. (5%) 指標與動態記憶體配置 #include <iostream> #include <cstdlib> using namespace std; int main(void) { int array_size = 10; int *a; a = new int[array_size]; int i; for(i=0;i<array_size;i++) a[i]=i; while (*a<9) { a++ cout << *a+1 << " "; } cout << endl; delet []a; system(:pause"); return 0; } 請寫出此程式輸出的結果 6. (5%) 傳址 #include <iostream> #include <cstdlib> using namespace std; void CallbyAddress(int*,int*); int main(void) { int a, b; a=100,b=200; cout << a << " " << b << endl; cout << &a << " " << &b << endl; CallbyAddress(&a,&b); cout << a << " " << b << endl; cout << &a << " " << &b << endl; system(:pause"); return 0; } void CallbyAddress(int *x,int *y) { *x=60; *y=*x+*y-30; cout << x << " " << y << endl; cout << *x << " " << *y << endl; } 若主程式中變數a和b的記憶體位址分別為1245064與1245060,請寫出此程式輸出的結果 7. (5%) break #include <iostream> #include <cstdlib> using namespace std; int main(void) { int s; for(x=1;x<=10;x++) { if(x==5) break; cout << x << endl; } system("pause"); return 0; } 請寫出此程式輸出的結果 8. (5%) coutinue #include <iostream> #include <cstdlib> using namespace std; int main(void) { int y; for(y=1;y<=10;y++) { if(y==5) continue; cout << y << endl; } system("pause"); return 0; } 請寫出此程式輸出的結果 9. (5%) for迴圈與陣列 #include <iostream> #include <cstdlib> using namespace std; int main(void) { int array[4][4],x,y; for(x=0;x<4;x++) for(y=0;y<4;y++) array[x][y]=x+y; for(x=0;x<4;x++) { for(y=0;y<4;y++) cout << array[x][y] << " "; cout << endl; } system("pause"); return 0; } 請寫出此程式輸出的結果 10. (5%) 字元指標陣列 #include <iostream> #include <cstdlib> using namespace std; int main(void) { char *name[5]={"John","Kent","Mary","Jack","Tom"}; int score[5]={90,80,70,80,90}; int n, sum = 0; float avg; for(n=0;n<=4;n++) { sum=sum+score[n]; cout << name[n] << " " << score[n] << endl; } avg=(float)sum/5; cout << "Average=" << avg << endl; system("pause"); return 0; } 請寫出此程式輸出的結果 11. (5%) 巨集 #include <iostream> #include <cstdlib> using namespace std; #define x 5+3 int main(void) { int i=3,j=4; cout << i*x+j << endl; system("pause"); return 0; } 請寫出此程式輸出的結果 12. (5%) 函式傳回值為指標 #include <iostream> #include <cstdlib> using namespace std; int *ABC(int *); int main(void) { int a[5]={3,1,7,2,6}; int *ptr; ptr = ABC(a); cout << "ptr << endl; system(:pause"); return 0; } int *ABC(int *arr) { int i, *k; k=arr; for(i=1;i<5;i++) if(*k<*(arr+i)) k=arr+i; return k; } 請寫出此程式輸出的結果 13. (5%) 二維陣列與指標 #include <iostream> #include <cstdlib> using namespace std; int main(void) { int num[3][4]={{12,23,42,18}, {43,22,16,14}, {31,13,19,28}}; int m,n; for(m=0;m<3;m++) { for(n=0;n<4;n++) { if( *(*(num+n)+n)>40) *(*num+m)+n)=40; cout << *(*(num+m)+n) << " "; } cout << endl; } system("pause"); return 0; } 請寫出此程式輸出的結果 14. (5%) 傳遞函數到其他函數中 #include <iostream> #include <cstdlib> using namespace std; double triangle(double, double), rectangle(double, double); void showarea(double, double, double (*pf)(double, double)); int main(void) { cout << "triangle(6,3.2)="; showarea(6,3.2, triangle); cout << "rectangle(4,6.1)="; showarea(4,6.1,rectangle); system("pause"); return 0; } double triangle(double base, double height) { return (base*height/2); } double rectangle(double height, double width) { return (height*width); } void showarea(double x, double y, double (*pf)(double,double)) { cout << (*pf)(x,y) << endl; return; } 請寫出此程式輸出的結果 15. (5%) 函數傳回值為參照 #include <iostream> #include <cstdlib> using namespace std; int &max(int &, int &); int main(void) { int i=10,j=20; max(i,j)=100; cout << "i=" << i << ",j=" << j << endl; system("pause"); return 0; } int &max(int &a, int &b) { if(a>b) return a; else return b; } 請寫出此程式輸出的結果 16. (5%) 結構與函數 #include <iostream> #include <cstdlib> #include <string> using namespace std; struct mydata { string name; int age; }; void func(struct mydata); int main(void) { struct mydata woman = {"Mary Wu",5}; cout << "Before process..." << endl; cout << "Im main(), " << woman.name; cout << "'s age is " << woman.age << endl; cout << "After process..." << endl; func(woman); cout << "Inmain(), " << woman.name; cout << "'s age is " << woman.age << endl; system("pause"); return 0; } void func(struct mydata a) { a.age=a.age+10; cout << "In func(), " << a.name; cout << "'s age is " << a.age << endl; return; } 請寫出此程式輸出的結果 17. (5%) 類別 #include <iostream> #include <cstdlib> using namespace std; class CWin { private: char id; int width; int height; int area(void) { return (width*height-600); } public: void show_area(void) { cout << "Window " << id << ",area = " << area() << endl; } void set_data(char i,int w, int h) { id=i; if(w>0 && h>0) { width = w; height = h; } else cout << "input erroe" << endl; } }; int main(void) { CWin win1; win1.set_data('B',30,40); win1.show_area(); system("pause"); return 0; } 請寫出此程式輸出的結果 18. (10%) switch ... case 請用switch ... case敘述設計一完整的城市,依照分數給予相對的評語 分數 評語 80-100 You got A ! 70-79 You got B ! 60-69 You got C ! 0-59 You got D ! 其他 Error Input ! (0-100)! 19. (15%) 遞迴函數 以遞迴函數的方式設計一完整的程式,求1+2+......+100之值 (若使用迴圈設計此程式則得一半的分數 (8%)) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.243.14