一、单项选择题:(10分,每题2分)
1.char *p[10];该语句声明了一个: c 。
A)指向含有10个元素的一维字符型数组的指针变量p
B)指向长度不超过10的字符串的指针变量p
C)有10个元素的指针数组p,每个元素可以指向一个字符串
D) 有10个元素的指针数组p,每个元素存放一个字符串
2.若int x;且有下面的程序片断,则输出结果为: D 。
for (x=3; x<6; x++)
{
printf((x%2) ? "##%d" : "**%d\n", x);
}
A) ##3B) **3C) **3D)##3**4
**4 ##4 ##4**5 ##5
##5 **5
3.在while(!x)语句中的!x与下面条件表达式等价的是: D 。
A) x!=0 B) x==1 C) x!=1 D) x==0
4.已知
struct point
{
int x;
int y;
};
struct rect
{
struct point pt1;
struct point pt2;
};
struct rect rt;
struct rect *rp = &rt;
则下面哪一种引用是不正确的__D______。
A) rt.pt1.x B) (*rp).pt1.x
C) rp->pt1.x D) rt->pt1.x
5.若二维数组a有m行n列,则下面能够正确引用元素a[i][j]的为:C 。
A) *(a+j*n+i) B) *(a+i*n+j)
C) *(*(a+i)+j) D) *(*a+i)+j
CDDDC
二、分析程序并写出运行结果。(25分,每题5分)
1.
#include
main()
{
int n;
static char *monthName[]=
{"Illegal month", "January", "February",
"March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
for (n=1; n<=12; n++)
{
printf("%s\n", monthName[n]);
}
}
运行结果是:
January
February
March
April
May
June
July
August
September
October
November
December
2.
#include
#define ARR_SIZE 7
void YH(int a[][ARR_SIZE], int n);
void PrintYH(int a[][ARR_SIZE], int n);
main()
{
int a[ARR_SIZE][ARR_SIZE];
YH(a, ARR_SIZE-1);
PrintYH(a, ARR_SIZE-1);
}
void YH(int a[][ARR_SIZE], int n)
{
int i, j;
for (i=1; i<=n; i++)
{
for (j=1; j<=i; j++)
{
if (j==1 || i==j)
{
a[i][j] = 1;
}
else
{
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
}
}
void PrintYH(int a[][ARR_SIZE], int n) {
int i , j ;
for (i=1; i<=n; i++)
{
for (j=1; j<=i; j++)
{
printf("%4d", a[i][j]);
}
printf("\n");
}
}
运行结果是:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
3.
#include
main()
{
int i, n;
for (i=1; i<=5; i++)
{
printf("Please enter n:");
scanf("%d", &n);
if (n <= 0) continue;
printf("n = %d\n", n);
}
printf("Program is over!\n");
}
程序运行时输入:1 -2 3 -4 5↙
运行结果是:
n = 1
Please enter n: Please enter n:n = 3
Please enter n: Please enter n:n = 5
Program is over!
4.
#include
void Func(int n);
main()
{
int i;
for (i = 0; i < 2; i++)
{
Func(i);
}
}
void Func(int n)
static int t = 1;
printf("t=%d\n", t++);
}
运行结果是:
t=1
t=2
5.
#include
int Func(int i);
main()
{
int i;
for (i=3; i<5; i++)
{
printf(" %d", Func(i));
}
printf("\n");
}
int Func(int i)
{
static int k = 10;
for (; i>0; i--)
{
k++;
}
return (k);
}
运行结果是:
13 17
三、阅读并完成程序,在标有下划线的空白处填入适当的表达式或语句,使程序完整并符合题目要求。(20分,每空2分)
1. 下面程序用于读入10个字符串,然后找出最大的字符串并打印。
#include
#include
main()
{
char str[10][80],maxstring[80];
int i;
printf("Enter ten strings:\n");
for (i=0; i<10; i++)
{
scanf("%s", str[i] );
}
strcpy(maxstring, str[0]);
for (i=1; i<10; i++)
{
if (strcmp(maxstring, str[i])<0)或strcmp(str[i], maxstring)>0 {
strcpy(maxstring, str[i]);
}
printf("The max string is:%s\n",maxstring);
}
2. 下面这个程序用于交换两个数组的对应元素的值。
#include
#define ARRAY_SIZE 10
void Swap(int *x, int *y);
void Exchange(int a[], int b[], int n);
void InputArray(int a[],int n);
void PrintArray(int a[],int n);
main()
{
int a[ARRAY_SIZE], b[ARRAY_SIZE], n;
printf("Input array lenth n<=10: ");
scanf("%d", &n);
printf("Input array a:\n");
InputArray(a, n);
printf("Input array b:\n");
InputArray(b, n);
Exchange(a,b,n);
printf("After swap:\n");
printf("Array a:\n");
PrintArray(a, n);
printf("Array b:\n");
PrintArray(b, n);
}
void Swap(int *x, int *y)
{
int temp;
temp=*x ;
*x=*y ;
*y=temp ;
}
void Exchange(int a[], int b[], int n)
{
int i;
for (i = 0; i < n; i++)
{
Swap( &a[i],&b[i] );
}
}
void InputArray(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
{
scanf("%d ", &a[i]) ;
}
void PrintArray(int a[], int n)
{
int i;
for (i = 0;i < n;i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
3. 从键盘任意输入一个年号,判断它是否是闰年。若是闰年,输出"Yes",否则输出"No"。已知符合下列条件之一者是闰年:
能被4整除,但不能被100整除。
能被400整除。
#include
main()
{
int year, flag;
printf("Enter year:");
scanf("%d",&year );
if ( year%4==0&&year%100!=0||year%400==0 )
{
flag = 1;
}
else
{
flag = 0;
}
if ( flag )
{
printf("Yes!\n");
}
else
{
printf("No!\n");
}
}
四、在下面给出的4个程序中,共有15处错误(包括语法错误和逻辑错误),请找出其中的错误,并改正之。(在错误的代码下边画一根横线,把正确的代码写在旁边;漏掉的代码正确插入合适的位置。30分,每找对1个错误,加1分,每修改正确1个错误,再加1分。)1.编程计算矩阵相乘之积。
#include
#define ROW 2
#define COL 3
// void MultiplyMatrix(int a[ROW][COL],int b[COL][ROW], int c[ROW][ROW]) ;
main()
{
int a[ROW][COL], b[COL][ROW], c[ROW][ROW], i, j;
printf("Input array a:\n");
for (i=0; i { for (j=0; j { scanf("d", &a[i][j]); // %d } } printf("Input array b:\n"); for (i=0; i { for (j=0; j { scanf("d", &b[i][j]); } } MultiplyMatrix(a, b, c); printf("Results:\n"); for (i=0; i { for (j=0; j { printf("%6d", &c[i][j]);// c[i][j] } printf("\n"); } } void MultiplyMatrix(int a[ROW][COL],int b[COL][ROW], int c[ROW][ROW]) { int i, j, k; for (i=0; i { for (j=0; j { for (k=0; k {//c[i][j] = 0; c[i][j] = c[i][j] + a[i][k] * b[j][k];// b[k][j] } } } } 2.编程输入10个数,找出其中的最大值及其所在的数组下标位置。 #include int FindMax(int num[], int n, int *pMaxPos); main() { int num[10], maxValue, maxPos, minValue, minPos, i //; printf("Input 10 numbers:\n "); for (i=0; i<10; i++) { scanf("%d", num[i]); //& num[i] maxValue = FindMax(num, 10, *maxPos); // &maxPos printf("Max=%d, Position=%d\n",maxValue, maxPos); } int FindMax(int num[], int n, int *pMaxPos) { int i, max; max = num[0]; for (i = 1, i < n, i++)// i = 1; i < n; { if (num[i] > max) { max = num[i]; *pMaxPos = i; } } return max; } 3.韩信点兵。韩信有一队兵,他想知道有多少人,便让士兵排队报数:按从1至5报数,最末一个士兵报的数为1;按从1至6报数,最末一个士兵报的数为5;按从1至7报数,最末一个士兵报的数为4;最后再按从1至11报数,最末一个士兵报的数为10。你知道韩信至少有多少兵吗? #include main() { int x;//x=0 while (1) { if (x%5=1 && x%6=5 && x%7=4 && x%11=10) // x%5==1 && x%6==5 && x%7==4 && x%11==10 { break; } x++//x++; } printf(" x = %d\n", x); } 4.下面函数Squeez()的功能是删除字符串s中所出现的与变量c相同的字符。 void Squeeze(char s[], char c);//void Squeeze(char s[], char c) int i,j; //j=0; for (i=0; s[i]!='\0'; i++) { if (s[i] != 'c') //c { s[j] = s[i]; j++; } s[i]='\0'; // s[j]='\0'; } 或// s[j]='\0'; } 五、编程(15分) 从键盘任意输入某班10个学生的成绩,调用save函数把数据存入到以你的学号命名的磁盘文件中,然后打开该文件读出数据,对成绩进行由高到低的排序,并打印成绩不及格的学生人数。最后再次调用save函数把数据存入到原磁盘文件中。要求按照如下给出的函数原型进行编程: void save(float score[],int n); void Sort(float score[],int n); int Fail(float score[],int n); 要求用一维数组做函数参数编程实现成绩排序和统计成绩不及格的学生人数,然后在主函数中打印这些结果,不能使用全局变量编程。 参考程序如下: void load(float score[],int n) //3分 {FILE *fp; int i; if((fp=fopen(02406101","rb"))==NULL) {printf("cannot open infile\n"); return;} for(i=0;i if(fread(&score[i],sizeof(float),1,fp)!=1) {if(feof(fp)) {fclose(fp); return;} printf("file read error\n"); } fclose (fp); } void save(float score[],int n) //3分 {FILE *fp; int i; if((fp=fopen("02406101","wb"))==NULL) {printf("cannot open file\n"); return; } for(i=0;i if(fwrite(&score[i],sizeof(float),1,fp)!=1) printf("file write error\n"); fclose(fp); } void sort(float score[],int n) //3分 {int i,j,k; float t; for(i=0;i {k=i; for(j=i+1;j if(score[j] k=j; t= score[k]; score[k]= score[i]; score[i]=t;} } int Fail(float score[],int n)//3分{int i,k=0; for(i=0;i if(score[i]<60) k++; return k; } void main() //3分 { int i; float score[10]; for(i=0;i<10;i++) scanf("%f",&score[i]); save(score,10); load(score,10); sort(score,10); printf("The Fail number is %d",Fail(score,10)); save(score,10); }