精華區beta NTU-Exam 關於我們 聯絡資訊
課程名稱︰計算機程式 課程性質︰大一必修 課程教師︰魏宏宇 開課學院:物理系 開課系所︰理學院 考試日期(年月日)︰96.1.16 考試時限(分鐘):100mins 是否需發放獎勵金:是 (如未明確表示,則不予發放) 試題 : 1.(32%)[Selection Sort] (a)Declare a constant integer variable ArraySize and set the size of an array equals to 5.(4%) (b)Implement a function swap(double*,double*) that swaps two constant data elements.(12%) (c)Use "recursive" method to implement selection sorting with function selectionSort(int[],int) and swap(double*,double*).Hint:the selection sort in the textbook is NOT recursive (it's iterative).(16%) Hint:About selection sort ●1st iteration ■Find the smallest among N elements ■Swap it with the 1st element ●2nd iteration ■Find the smallest among (N-1)elements(exclude the 1st one) ■Swap it with 2nd element ●i-th iteration ■Find the smallest among (N-i)element and swap it with the i-th element 2.(31%)Write C++ statements to do the following tasks: ●You can assume that required standard library are include ●You can assume that variables are declared ●You can assume that we already have 'using' statement (a)Create a function to randomly return a value from {2,4,6}.The probability of randomly generating 2 is 25%.The probability of randomly generating 4 is 25%.The probability of randomly generating 6 is 50%.(7%) (b)Create a function int find_min(double*Y,int sizeY) to find the "index" of the minimum value of a double array Y[size Y](7%) (c)Append the first n characters of string s2 to the string s1.(5%) (d)Enumerate the 7 days of a week(Mon=1,Tue=2,Wed=3,Thur=4,Fri=5,Sat=6,Sun=7) (5%) (e)Two functions are implemented to compute addition and subtration for x1 and x2:double F_add(double x1,double x2) and double F_minus (double x1, double x2).Use function pointer to create a menus system in which users choose to add if input=1 and to minus if input=2 in the menu.Display the computation result of (z1+z2) or (z1-z2) on the screen.(6%) ... int input; double z1=1.3; double z2=3.5; cout<<"Enter a number 1 for adding or 2 for minus"; cin>>input; [Write yours source codes from here] ... 3.(24%)What are the outputs of the following programs?If you think there is error,just write 'ERROR' in your answer.If cout display an unknown address, please answer with "address of variable X" (a)(11%) ... int x=7,y=19; inline void F1(void) { x=x+2; cout<<"(F1)x="<<x<<",y="<<y<<endl; } void F2(int y) { static int x=20; ++x; y--; cout<<"(F2)x="<<x<<",y="<<y<<endl; } void F3(int &y) { auto int x=3; x*=2; y=y-2; cout<<"(F3)x="<<x<<",y="<<y<<endl; } int main() { int y=10; int &x=y; cout<<"(a1)x="<<x<<",y="<<y<<endl; F1(); F3(y); F2(y); cout<<"(a2)x="<<x<<",y="<<y<<endl; { int x=12; cout<<"(a3)x="<<x<<",y="<<y<<endl; } F3(y); cout<<"(a4)x="<<x<<",y="<<y<<endl; F2(y); F1(); cout<<"(a5)x="<<x<<",y="<<y<<endl; } (b)Assume the address of x is 600000.The size to store an address is 4bytes. The size to store an integer is 4 bytes.(8%) int x[]={1,3,5,7}; int *y=x; int i=2; cout<<"\n(b1)="<<&x[1]; cout<<"\n(b2)="<<y; cout<<"\n(b3)="<<*x+1; cout<<"\n(b4)="<<*(y+2); *(y++)=2; x[2]=4; x[3]=0; cout<<"\n(b5)="<<x[i]; cout<<"\n(b6)="<<x; cout<<"\n(b7)="<<*(x+2); cout<<"\n(b8)="<<*(*(&y)); (c)Assume the address of N is 500000.The size to store an address is 4 bytes. The size to store a character is 4 bytes.(5%) char *N[4]={"Seven","Eight","Night","Ten"}; cout<<"(c1)="<<N[1][2]<<endl; cout<<"(c2)="<<N<<endl; cout<<"(c3)="<<N[3]<<endl; cout<<"(c4)="<<*N[2]<<endl; cout<<"(c5)="<<&N[1]<<endl; 4.(13%)A function has input integer x,y and output z(z=x/y).Implement the function with (a)(5%)Pass-by-reference with reference arguments(&) (b)(5%)Pass-by-reference with pointer (c)(3%)Continue from (b).Set the input and/or output of the function as constant.You should determine what should be set as constant.Write the function prototype. -- ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆ ★☆ http://www.wretch.cc/album/treva ☆★ ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.175.226.100