第6章习题参考答案
1、选择题:
1-5 BBDDA 6-10 CCABD 11-15 CBCDA 16 A 2、填空题
(1) 常量,变量(2) 指针(3)2(4)12,12(5) a[0],a[3]
3、阅读程序,写出下面程序的运行结果 (1)5 (2)6
(3) abcdefglkjih (4) 976531 (5)5,9
(6)2,4,5,7,6,0,11,9,7,3, (7)string_a=I am a teacher.
string_b=You are a student. string_a=I am a teacher. string_b=I am a teacher.
4、程序填空
(1) *p != '\\0', *p-'0', j— (2) i 5、编程题 (1) #include int a=3,b=7,c=2; int *ptra = &a,*ptrb = &b,*ptrc = &c; int temp; printf(\ printf(\ if ( *ptra > *ptrb ) { 16 temp = *ptra; *ptra = *ptrb; 1 *ptrb = temp; } if ( *ptra > *ptrc ) { temp = *ptra; *ptra = *ptrc; *ptrc = temp; } if ( *ptrb > *ptrc ) { temp = *ptrb; *ptrb = *ptrc; *ptrc = temp; } printf(\ printf(\ return 0; } (2) #include void inputdata(int a[],int n); void process(int a[],int n); void outputdata(int a[],int n); int main() { int a[10],n=10; inputdata(a,n); outputdata(a,n); process(a,n); outputdata(a,n); system(\ return 0; } void inputdata(int a[],int n) //0~99之间的数 { 17 int i; for( i = 0; i < n; i++ ) scanf(\} void outputdata(int *ptr,int n) { int i; 2 printf(\ for( i = 0; i < n; i++ ) printf(\ printf(\void process(int *ptr,int n) { int i,maxi,mini; int max=-1,min=101; for( i = 0; i < n; i++ ) { if ( max < *(ptr+i) ) { max = *(ptr+i); maxi= i; } if ( min > *(ptr+i) ) { min = ptr[i]; mini= i; } } if ( mini != 0) { i = ptr[0]; ptr[0] = ptr[mini]; ptr[mini] = i; } if (maxi != n-1) { i = ptr[n-1]; ptr[n-1] = ptr[maxi]; 18 ptr[maxi] = i; } if (maxi == 0 && mini == n-1) { i = ptr[maxi]; ptr[maxi] = ptr[mini]; ptr[mini] = i; } } (3) #include 3int strLength (char *); int main() { static char str[81]; gets(str); printf(\ system(\ return 0; } int strLength (char *s) { char *p=s; while (*p) p++ ; return (p-s) ; } (4) #include void Transpose (int (*a)[N],int (*T)[M]); int main( ) { static int a[M][N],T[N][M]; int i,j; for( i = 0; i < M; i++ ) for( j = 0; j < N; j++ ) scanf( \ 19 printf(\ for( i = 0; i < M; i ++ ) { for( j = 0; j < N; j++ ) printf( \ printf(\ } printf(\ Transpose(a, T); printf(\ for( i = 0; i < N; i ++ ) { for( j = 0; j < M; j++ ) printf( \ printf(\ } 4 printf(\ return 0; } void Transpose (int (*a)[N],int (*T)[M]) { int i,j; for( i = 0; i < N; i++ ) for( j =0; j < M; j++ ) T[i][j] = a[j][i]; } (5) #include static char source[81],target[81]; int result; gets(source); gets(target); result = Strcmp(source,target); if (result) { if( result < 0 ) 20