该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
if(n%i==0) break;
return i>m;
}
void main()
{ int num,i;
num=20;
for(i=2;i<=num;i++){
while(prime(i)&&(num%i==0)){
printf("%d ",i);
num/=i;
}
}
}
4. The output of the following program is _____.
#include
void main( )
{ int j, k;
char c, s1[80]=” a-b-e8-9”, s2[80];
j=k=0;
while((c=s1[j++])!=’\0’)
if(s1[j]==’-‘&&s1[j+1]>=c){
j++;
while(c
s2[k++]=c++;
}
else
s2[k++]=c;
s2[k]=’\0’;
printf(“%s”, s2);
}
5. If the following data are stored in text file a.txt
One23Two45
The output of the following program is _____.
# include
# include
void main()
{ char ch;
FILE *fp;
if ((fp = fopen("a.txt","r")) == NULL){
printf("Can't open file : %s\n", "a.txt");
exit(0);
}
while (!feof(fp)){
ch=fgetc(fp);
if(ch>=’0’ && ch<=’9’)
putchar(ch-‘0’+’A’);
}
fclose(fp);
}
Section 4: According to the specification, complete each program (2 mark for each blank, total: 20 marks)
1.The program written below prints out the numbers between 1 and 1000, which equal the sum of the cubes of the digits. For example, one of the numbers printed out could be 153, because 153=1*1*1+5*5*5+3*3*3.
#include
void main( )
{ int digit, k, m, s;
for (k=1; k<=1000; k++){
(1) ;
m=k;
while(m!=0){
digit=m%10;
s=s+digit*digit*digit;
(2) ;
}
if( (3) ) printf("%d ", s);
}
}
2.The function written below is a binary search one which decides if a particular value x occurs in the sorted array v. The elements of v are in increasing order. The function returns the position(a number between 0 and n-1) if x occurs in v, and –1 if not.
int binasearch(int x, int v[], int n)
{ int low, high, mid;
low=0;
high=n-1;
while( (4) ){
mid=(low+high)/2;
if( (5) )
high=mid-1;
else if( (6) )
low=mid+1;
else
return mid;
}
(7) ;
}
3.The definition of function f2 ( ) is equivalent to the definition of function f1 ().
int f1(char s[ ], char t[ ])
{ int j, k;
j=k=0;
while(s[j]!='\0')
j++;
while((s[j++]=t[k++])!='\0')
;
} int f2(char *s, char *t)
{
while( (8) )
(9) ++;
while( (10) )
;
}
Section 5:Read the following program and answer question (10 marks)
The output of the following program is _____.
# include
# include
#define LEN sizeof(struct line)
struct line
{ int num ;
struct line *next;
};
void main( )
{ int k ;
struct line *p , *head ;
head=NULL;
for(k=1; k<10; k++){
p=(struct line *) malloc (LEN) ;
p->num=k;
p->next=head;
head=p;
}
while((p=p->next)!=NULL){
printf("%d, ", p->num) ;
p=p->next;
}
}
《C Programming》ANSWER
Time: 8:30-10:30 am. June 20, 2011
Section 1: Single Choice (2 mark for each item, total 20 marks)
1 D 2 D 3 A 4 A 5 B
6 B 7 C 8 C 9 A 10 C
Section 2: Fill in the blanks (2 mark for each item, total 30 marks)
1 x>y ? x : y 2 2
3 0 4 128
5 5 6 4.0
7 1,3#1,3 8 3
9 16 10 12
11 3#3#3#3# 12 2, 3
13 m 14 a=1, b=4
15
三、Section 3: Read each of the following programs and answer questions (6 marks for each item, total marks: 30)
1 1 2 3#2 3 1#3 1 2 2 1#3#2#1#1#3
3 2 2 5 4 abcde89
5 CDEF 6
Section 4: According to the specification, complete each program (2 mark for each blank, total: 20 marks)
(1) s=0 (2) m=m/10
(3) k==s (4) low
(5) xv[mid]
(7) return -1 (8) *s != ‘\0’
(9) s (10) *s++ = *t++
(11) (12)
(13) (14)
(15) (16)
Section 5:Read each of the following programs and answer questions (10 marks)
8, 6, 4, 2,