課程名稱︰計算機程式
課程性質︰半學期
課程教師︰陳銘憲
開課系所︰電機系
考試時間︰11/16 2005
試題 :共110分(超過以100計算)
1.(10%)
(a)Use cin and cout to input two integer values from
the keyboard into integer valueables a,b and output
the sum of these two variables.
(b)Declare and initialize a five-element integer array
"arr1" whose values consist of consecutive integer
values starting from 5.
2.(10%)
(a)What is the output of code 2,and what does the code 2 do?
(b)Write a program with the same function without using the
variable 'c'.(i.e.only using variables a and b)
3~11.(10%)What are the output of the following program?
//Code 2 //Code3
#include <iostream> #include <iostream>
using namespace std; using namespace std;
void main() void main()
{ int a=3,b=5; { int counter=0;
int c; int i=8,j=2;
c=a; for(i--;i-->=0;i--){
a=b; j=2;
b=c; for(j++;j++<=8;j++){
cout<<a<<endl<<b; if(j==6) continue;
} if(i==6) break;
counter++;
}
}
cout<<counter;
}
//Code4
#include <iostream>
using namespace std;
void main()
{ int x,*k=&x,&y=*k,z,*p=&z;
x=4,y=3,z=2,*p=1;
cout<<"x="<<x<<",y="<<y<<endl<<"z="<<z<<",*p="<<*p<<endl;
x--;
y+=2;
z%=3;
*p*=4;
cout<<"x="<<x<<",y="<<y<<endl<<"z="<<z<<",*p="<<*p<<endl;
}
//Code5
#include <iostream>
using namespace std;
void test (int a,int &b){
b+=a++;
a*=--b;
cout<<"from test a="<<a<<",b="<<b<<endl;
}
void main(){
int a=7,b=4;
test(a,b);
a+=a++;
test(b,a);
cout<<"from test a="<<a<<",b="<<b<<endl;
}
//Code6
#include <iostream>
using namespace std;
int mystery(int a,int *b)
{
*b=(*b)++a%7-a;
return *b>0?a+2:a-2;
}
void main(){
int x=10,y=8;
x=mystery(x,&y);
cout<<"x="<<x<<",y="<<y<<endl;
y=mystery(y,&x);
cout<<"x="<<x<<",y="<<y<<endl;
}
//Code7
#include <iostream>
using namespace std;
int f(int arr﹝﹞,int n)
{ if(n==0)
return arr﹝0﹞;
return arr﹝n-1﹞*f(arr,arr﹝n-1﹞-1)+1;
}
void main()
{
int array﹝6﹞=﹝1,2,3,4,5,6﹞;
cout<<f(array,array﹝5﹞);
}
//Code8
#include <iostream>
using namespace std;
int fun1(void);
int fun2(void);
int fun3(int&);
int val=15;
void main(){
int val=01;
for(int i=0;i<3;i++){
((val+=fun1())+=fun2())+=fun3(val);
::val=++val;
}
cout<<"val-a="<<val<<endl;
cout<<"val-b="<<::val<<endl;
}
int fun1(void){
static int val=2;
::val=val++;
return val+=2;
}
int fun2(void){
return val+=3;
}
int fun2(int &num){
int& cnt=val;
num+=(cnt<=num?cnt:num);
return val;
}
//Code9
#include <iostream>
using namespace std;
void main(){
int arr﹝﹞={34,56,4,10,77,51,93,30,5,52};
int i=0;
do{
int b=arr﹝i﹞,c=i;
while(c>0 && arr﹝c-1﹞>b){
arr﹝c﹞=﹝c-1﹞;
c--;
}
arr﹝c﹞=b;
i++;
}while(i<10);
for(i=0;i<10;i++)
cout<<arr﹝i﹞<<" ";
}
//Code10
#include <iostream>
using namespace std;
int get(int u)
{
int v=0;
for(int x=u;x!=1;cout<<(x=x%2==0?x2:3*x+1)<<' ')
if (v<x) v=x;
return v++;
}
void main()
{
int a,&b=a,c,&d=b,&e=c;
a=5;b=4;c=3;d=2;e=1;
cout<<"a="<<a<<",b="<<b<<",c="<<c<<",d="
<<d<<",e="<<e<<endl;
d=get(11);
e+=d;
cout<<endl
<<"a="<<a<<",b="<<b<<",c="<<c
<<",d="<<d<<",e="<<e<<endl;
}
//Code11
#include <iostream>
using namespace std;
void test(int head﹝﹞,int i)
{
if(i>=10)
return;
test(head,(i<<1)+1);
cout<<head﹝i﹞<<",";
test(head,(i<<1)+2);
}
int main()
{
int a﹝10﹞,i;
for(i=0;i<10;++i)
a﹝i﹞=i;
test(a,0);
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.115.99.135