1、一. 程序分析,写出运行结果(30分)
1.试题程序
#include
2、));
}
/*** 答案:41, 50, 21*/
2.试题程序
#include
3、] = "abcdefghi";
int i;
FILE *fp;
fp = fopen( "file.dat", "w" );
for( i = 0; i < 9; i++ )
{
fputc( a[i], fp );
}
fclose( fp );
fun( );
}
/*** 答案:ABCDEFGHI*/
3.试题程序
#include
4、p < q; p++ ) { if( *p > *M ) { M = p; } else if( *p < *m ) { m = p; } } *p = t[0]; t[0] = *m; *m = *p; *p = t[9]; t[9] = *M; *M = *p; } main( ) { int i; int a[10] = {23, 12, 45, 56, 1, 78, 43, 58, 90, 13 }; mmx( a ); for( i = 0; i < 10; i++ ) {
5、
printf("%d, ", a[i] );
}
}
/*** 答案:1, 12, 45, 56, 23, 78, 43, 58, 13, 90,
* 找最大/最小,最小的和第一个位置对换,最大的和最后一个对换。*/
4.试题程序
#include
6、n2( float b[], float w[] ) { float sum = 0.0; int k, i; for( k = 0, i = 0; i < SIZE; i++ ) { sum += b[i]; if( ( i + 1 ) % 5 == 0 ) { w[k] = sum / 5; sum = 0.0; k++; } } } main( ) { float sum; int i; float w[SIZE]; fun1( a ); fun2( a, w ); for( i
7、 = 0; i < SIZE / 5; i++ )
{
printf("%.1f, ", w[i] );
}
}
/**
* 答案:6.0, 16.0, 26.0, 36.0, 46.0**/
5.试题程序
#include
8、
{
printf("\n%3d ", f[i]);
for( j = 0; j < f[i]; j++ )
{
printf("#" );
}
}
}
}
/**
* 答案:
* 1 #
* 5 #####
* 34 ##################################
* */
6.试题程序
#include
9、 y = x;
i = 0;
do
{
u = y/2;
a[i] = y % 2;
i++;
y = u;
}while( y >= 1 );
for( j = i-1; j >= 0; j-- )
{
printf("%d", a[j] );
}
printf("Yes");
}
/** * 答案:10010Yes * */
7.试题程序
#include
10、) + 4); printf("i = %d\n", i ); return i; } int reset( int i ) { i = i <= 2 ? 5:0; return i; } main( ) { int i = 5; reset( i / 2 ); printf("i=%d\n", i ); reset( i = i /2 ); printf("i=%d\n", i ); reset( i/2 ); printf("i=%d\n", i ); workover( i ); printf("i=%d\n", i )
11、
}
/**
* 答案:
* i = 5
* i = 2
* i = 2
* i = 0
* i = 2
*/
8.试题程序
说明下列程序的功能, 请写出程序的输出
#include
12、[6] = "-2345";
int n;
n = ( *s == '-' ) ? -fun( s + 1 ):fun( s );
printf("%d", n );
}
/**
* 答案: -2345
9.试题程序
#include
13、ue;
case 'B':
++k;
break;
default:
putchar( '*' );
case 'C':
putchar( '&' );
continue;
}
putchar('#');
}
}
/*** 答案:#&*&% **/
二. 程序填空(30分)
1. 以下fun函数的功能是:找出具有N个元素的一维数组中的最小值,并作为函数值返回。请填空。(设N已定义)
int fun(int x[N])
{int i,k=0;
for(i=0;i 14、
if(x[i]
return x[k];
}
答案:if(x[i]< x[k])k = i;
2. 以下fun函数的功能是在N行M列的整形二维数组中,选出一个最大值作为函数值返回,请填空。(设M,. N已定义)
int fun(int a[N][M))
{int i,j,row=0,col=0;
for(i=0;i 15、f(a[i][j]>a[row][col]){row=i;col=j;}
4. 以下程序的功能是:借助指针变量找出数组元素中最大值所在的位置并输出该最大值。请在输出语句中填写代表最大值的输出项。
#include<stdio.h>
main()
{int a〔10],*p,*s;
for(p=a;p-a<10;p++)scanf(”%d”,p);
for(p=a,s=a;p-a<10;p++)
printf("max=%d\n”,*s);
}
答案:if(*p>*s)s=p;
5. 以下程序打开新文件f.txt,并调 16、用字符输出函数将a数组中的字符写入其中,请填空。
#include<stdio.h>
main()
{
char a[5]={’1’,’2’,’3’,’4’,’5’},i;
fp=fopen(”f .txt”,”w”);
for(i=0;i<5;i++)fputc(a[i],fp);
fclose(fp);
}
答案:FILE *fp;
6. 以下程序的功能是:将值为三位正整数的变量x中的数值按照个位、十位、百位的顺序拆分并输出。请填空。
#include “stdio.h”
main()
{ int 17、x=256;
printf("%d-%d-%d\n", );
}
答案:x%10,x/10%10,x/100
7. 以下程序用以删除字符串所有的空格,请填空。
试题程序:
#include “stdio.h”
main()
{ char s[100]={"Our teacher teach C language!"};int i,j;
for(i=j=0;s[i]!=’\0’;i++)
s[j]= ‘\0’
printf(“%s\n”,s);
}
18、
答案:if(s[i]!= ' ') {s[j]=s[i];j++;}
8. 函数fun功能是:将两个两位数的正整数a、b合并形成一个整数放在c中。合并的方式是:将a的十位和个位数依次放在c的个位和百位上,b的十位和个位数依次放在c的十位和千位上。例如,当a=45, b=12调用该函数后,c=2514。请填空。
试题程序:
#include 19、b);
printf("a=%d b=%d\n",a,b);
fun(a,b,&c);
printf("The result is:%d\n\n",c);
}
答案:
#include 20、a中的值为20, b中的值为10。请填空。
试题程序:
#include 21、y;y=t;
return t;
}
三. 编写程序(40分)
1. (7分)编程实现:输入一个整数,判断它能否被3,5,7整除,并输出以下信息之一:
(1)能同时被3,5,7整除;
(2)能被其中两数(要指出哪两个)整除;
(3)能被其中一个数(要指出哪一个)整除;
(4)不能被3,5,7任何一个整除。
2. (8分)每个苹果0.8元,第一天买2个苹果,第二天开始,每天买前一天的2倍,直至购买苹果总个数达到不超过100这个最大值。编程求平均每天花多少钱?
3. (12分)定义一个含有30个整型元素的数组,按顺序分别赋予从2开始的偶数;然后按顺序每五个数求出一个平均值, 22、放在另一个数组中并输出。
4. (13分)从键盘输入一个字符串,然后按照下面要求得到一个新字符串。新串是在原串中,每相邻两个字符之间插入一个空格,如原串为abcdef,则新串为a b c d e f。然后,将新串逆序(不允许使用辅助数组),最后将得到的字符串输出。要求在函数insert中完成新串的产生;在函数inverse中完成新串的逆序;并在主函数中完成所有相应的输入和输出。
1. #include 23、printf("%d能同时被3,5,7整除\n",x);
else if ((x%3==0)&&(x%5==0))
printf("%d能被3和5整除\n",x);
else if ((x%3==0)&&(x%7==0))
printf("%d能被3和7整除\n",x);
else if ((x%7==0)&&(x%5==0))
printf("%d能被7和5整除\n",x);
else if ((x%3==0))
printf("%d能被3整除\n",x);
else if ((x%5==0))
printf("%d能被5整除\n",x);
24、
else if ((x%7==0))
printf("%d能被7整除\n",x);
else
printf("%d不能被3,5,7任何一个整除\n",x);
}
2. #include 25、h>
#define SIZE 30
void main()
{
float b[SIZE/5],sum;
int a[SIZE],i,j,k;
for(k=2,i=0;i 26、 for(i=0;i






