作者egggge (咪咪蛋)
看板NTUBIME102HW
標題Re: [轉錄][試題] 96上 林達德 計算機程式語言 期中考
時間Wed Nov 25 22:10:52 2009
※ 引述《w86083 (小可)》之銘言:
第一題:(10分,每一小題2分)
解釋名詞
(1)EOF (2)global variable (3)srand() (4)ASCII (5)UML
Answer
(1)EOF :EOF= End Of File
(2)global variable :全域變數,能在任一函式中使用的變數,通常寫於main之前
(3)srand() :亂數表 括弧內輸入亂數 如3,2,time()
(4)ASCII :編碼標準
(5)UML :Unified Modeling language,統一標準模塑語言,由標準的圖像表示程式的架構與資料
================================================================================================
第二題:(10分,每一格2分)
考慮以下程式片斷:
if(i == j)
cout<<"1"<<endl;
else if ((i % j)<3)
cout<<"2"<<endl;
else if (i<(j-1))
cout<<"3"<<endl;
else
cout<<"4"<<endl;
cout<<"5"<<endl;
若i是9而j是4時,輸出為何? Answer: 25
若i是4而j是9時,輸出為何? Answer: 35
若i是5而j是6時,輸出為何? Answer: 45
若i是5而j是9時,輸出為何? Answer: 35
若i是0而j是0時,輸出為何? Answer: 15
================================================================================================
第三題:(10分,每一格2分)
下列敘述執行後變數A,B,C,D,E之值分別為何?
int A=0, B=0, C=0, D=0, E=0;
while(B <= 20)
{
A = A + 2;
B = B + A;
C++;
D = B + C%2;
E *= 2.0;
}
Answer:
A=10 B=30 C=5 D=31 E=0
================================================================================================
第四題:(10分)
請將下列程式在個人電腦上執行後輸出仔細地填入右側答案欄中
#include<iostream>
using std::cout;
using std::endl;
int funct1(int a);
int funct2(int a);
int a=0,b=1;
main()
{
int count;
for(count = 1;count <= 5;++count)
{
b += funct(a+1) + 1;
cout<<"b = "<<b<<endl;
}
}
int funct1(int a)
{
b = funct2( a+1 ) + 1;
return b;
}
int funct2(int a)
{
return(b+a);
}
Answer:跑不出來(假設b += funct(a+1) + 1;這裡錯誤)
(更正成b += funct1(a+1) + 1;)
Answer:
b = 9
b = 25
b = 57
b = 121
b = 249
(更正成b += funct2(a+1) + 1;)
Answer:
b = 4
b = 10
b = 22
b = 46
b = 94
================================================================================================
第五題:(10分)
設計一個程式,在螢幕上顯示下列的輸出
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
Answer:
#include "stdafx.h" // 呼叫函式庫
#include "iostream" // 呼叫iostream;有顯示cout及輸入cin的功能
using namespace std; // 開啟所有stdafx的所有功能
int main()
{
int i,j;
for(i=1;i<7;i++)
{
for(j=0;j<i;j++)
{
cout<<j<<" ";
}
cout<<"\n";
}
system("pause"); //凍結畫面
return 0; //回傳一個值給main
}
================================================================================================
第六題:(10分)
請分別用 for,do while,while三種迴圈的程式語法撰寫程式,輸出下列序列:
1,4,7,10,13,16,19,22,25,28
Answer:
#include "stdafx.h" // 呼叫函式庫
#include "iostream" // 呼叫iostream;有顯示cout及輸入cin的功能
#include "iomanip" // 呼叫iomanip ;可以使用格式操作子
using namespace std; // 開啟所有stdafx的所有功能
int main()
{
int i;
//==for==============================
cout<<"==for=============================="<<endl;
for(i=1;i<=10;i++)
{
cout<<3*i-2<<" ";
}
cout<<endl;
//==do while==============================
cout<<"==do while=============================="<<endl;
int j=1;
do
{
cout<<3*j-2<<" ";
j++;
}while(j <= 10);
cout<<endl;
//==while==============================
cout<<"==while=============================="<<endl;
int k=1;
while(k<=10)
{
cout<<3*k-2<<" ";
k++;
}
cout<<endl;
//================================
system("pause"); //凍結畫面
return 0; //回傳一個值給main
}
================================================================================================
第七題:(10分)
指數函數e^x可以下式計算之
x^2 x^3 x^4
e^x = 1 + x + ╴╴╴ + ╴╴╴ + ╴╴╴ +......
2! 3! 4!
請你設計一個函式計算指數函數(至第20項),同時在主程式中呼叫此函式計算
e^1,e^2,e^3,e^4,.....,至e^10,並將結果輸出至螢幕上。
Answer:
#include "stdafx.h"
#include "iostream"
#include "cmath"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
double i,j=1,k=0,l,temp;
for(l=1;l<=20;l++)
{
for(i=1;i<=20; i++)
{
j=j*i;
k=k+pow(l,i)/j;
}
k=k+1;
temp=k;
cout<<temp<<endl;
k=0;
j=1;
}
system("pause");
return 0;
}
第八題:(10分)
請設計一個程式,其功能為找出整數1至N間的質數,整數N之值由使用者輸入,找到的質數
請輸出至螢幕上。
Answer:
第九題:(10分)
請設計一個名為Sphere的類別,此類別有三個內部資料(Private data number),分別為
radius、volume與s_area。此類別中另有三個成員函式(Public mumber function),
分別為CalculateVolume、CalculateSurfaceArea與PrintSphere,用來計算球體的體積、
表面積與輸出體積及表面積至電腦螢幕。
請寫出這個類別的完整內容,包含成員函式的定義程式碼。
[Hint:球體表面積公式為4πr^2,球體體積公式為(4/3)πr^3]
Answer:
第十題:(10分)
請寫出一個程式計算丟擲五枚銅板出現正面次數的機率。此程式必須以主程式main()呼叫
函式 int five_coins()的方式完成,而 int five_coins()函式所回傳給主程式的整數為
隨機模擬丟擲五枚銅板後出現正面的銅板數。主程式則需要將每次的節果累計,最後將模
擬丟擲50000次銅板後,出現銅板正面為四個以上的機率輸出到螢幕上。
[Hint:你將可能會使用到 srand()及rand()函式]
Answer:
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int five_coins();
int main()
{
srand(time(NULL));
int i;
float sum=0;
for(i=0;i<50000;i++)
{
if(five_coins() >= 4)
sum++;
}
cout << "機率為: " << setprecision(3) << fixed
<< 100*sum/50000.0 << "%\n";
system("pause");
return 0;
}
int five_coins()
{
int i,coin,tim;
tim = 0;
for(i=0;i<5;i++)
{
coin = rand()%2;
if(coin)
tim++;
}
return tim;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.36.226.196
推 sdg85732:謝囉! 對了!第九題我們還沒交吧 11/25 21:52
推 ceorl:類別是之後才會教的 理論上不會考 11/25 22:02
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.242.253
※ 編輯: egggge 來自: 140.112.242.253 (11/25 22:12)
推 lldavuull:感謝解答^^ 11/26 02:50