一、程序填空题
在此程序中,函数fun的功能是:将a所指4*3矩阵中第k行的元素与第0行元素交换。
例如,有下列矩阵:
若k=2,程序执行结果为:
#include <stdio.h>
#define N 3
#define M 4
/**********found**********/
void fun(int (*a)[N], int __1__)
{ int i,temp ;
/**********found**********/
for(i = 0 ; i < __2__ ; i++)
{ temp=a[0][i] ;
/**********found**********/
a[0][i] = __3__ ;
a[k][i] = temp ;
}
}
void main()
{ int x[M][N]={ {1,2,3},{4,5,6},{7,8,9},{10,11,12} },i,j;
printf("The array before moving:\n\n");
for(i=0; i<M; i++)
{ for(j=0; j<N; j++) printf("%3d",x[i][j]);
printf("\n\n");
}
fun(x,2);
printf("The array after moving:\n\n");
for(i=0; i<M; i++)
{ for(j=0; j<N; j++) printf("%3d",x[i][j]);
printf("\n\n");
}
}
答案:(1) k (2) N (3) a[k][i]
二、程序修改题
在此程序中,函数fun的功能是:读入一个字符串(长度<20),将该字符串中的所有字符按ASCII码值升序排序后输出。
例如,若输入”edcba”,则应输出”abcde”。
#include <string.h>
#include <stdio.h>
void fun(char t[])
{
char c;
int i,j;
/*************found**************/
for(i=strlen(t);i;i--)
for(j=0;j<i;j++)
/*************found**************/
if(t[j]<t[j+1])
{
c= t[j];
t[j]=t[j+1];
t[j+1]=c;
}
}
void main()
{
char s[81];
printf("\nPlease enter a character string :");
gets(s);
printf("\n\nBefore sorting :\n %s",s);
fun(s);
printf("\nAfter sorting decendingly:\n %s",s);
}
答案:(1) for(i=strlen(t)-1; i; i--) (2) if (t[j] > t[j+1])
三、程序设计题
在此程序中,编写一个函数fun,它的功能是:将ss所指字符串中所有下标为奇数位置的字母转换为大写(若该位置上不是字母,则不转换)。
例如,若输入”abc4Efg”,则应输出”aBc4EFg”。
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void fun(char *ss)
{
}
void main()
{
FILE *wf;
char tt[81],s[81]="abc4Efg";
system("CLS");
printf("\nPlease enter an string within 80 characters:\n");
gets(tt);
printf("\n\nAfter changing, the string\n %s",tt);
fun(tt);
printf("\nbecomes\n %s\n",tt);
/******************************/
wf=fopen("out.dat","w");
fun(s);
fprintf (wf,"%s",s);
fclose(wf);
/*****************************/
}
答案:
int i;
for(i=0;ss[i]!='\0';i++) /*将ss所指字符串中所有下标为奇数位置的字母转换为大写*/
if(i%2==1&&ss[i]>='a'&&ss[i]<='z')
ss[i]=ss[i]-32;