当前位置:文档之家› 河南理工大学C语言课后习题答案精解第六章

河南理工大学C语言课后习题答案精解第六章


a[1][1] a[1][2] a[1][3] 1 0 -1
a[2][1] a[2][2] a[2][3] 2 1 0
a[3][1] a[3][2] a[3][3]
0 1 2
0 1
0
第六章 数组
2. 写出程序的运行结果
(4) 程序四:
#include <stdio.h> main( ) { int i, s; i的ASCII码为105 char s1[100], s2[100]; printf(“input string1:\n”); n的ASCII码为110 gets(s1); printf(“input string2:\n”); gets(s2); s=-5 i=0; while((s1[i]==s2[i]) && (s1[i]!=„\0‟)) i++; if((s1[i] ==„\0‟) && (s2[i]==„\0‟)) s=0; 输入数据: aid else s=s1[i]-s2[i]; printf(“%d\n”,s); and }
第六章 数组
2. 写出程序的运行结果 (1) 程序一: 1 0 3 1 5 2 main( ) a[0][0] a[0][1] a[0][2] { int a[3][3]={1,3,5,7,9,11,13,15,17}; 7 1 9 2 11 3
int sum=0,i,j; a[1][0] a[1][1] a[1][2] for(i=0;i<3;i++) 13 2 15 3 17 4 for(j=0;j<3;j++) a[2][0] a[2][1] a[2][2] { a[ i ][ j ]=i+j; if(i==j) sum=sum+a[ i ][ j ]; } sum=6 printf(“sum=%d”,sum);
第六章 数组
3. 程序填空 (1) 用“两路合并法”把 两个已按升序排列的数 组合并成一个升序数 组. a 15 34 48 98
main( ) { int a[4]={15,34,48,98} int b[5]={12,32,55,67,78}; int c[10]; int i, j, k;
i=0,j=0,k=0;
while(i<4 && j<5) if(a[i]<b[j]) { c[k]=a[i]; k++; i++;} else {c[k]=b[j]; k++; j++;} while( i<4 ) { c[k]=a[i]; i++; k++;} while( j<5 ) { c[k]=b[j]; k++; j++;} for(i=0; i<k; i++) printf(“%5d”,c[i]); printf(“\n”); }
b
c
12
12 55
32
55
67
78
48
15 32 34 67 78 98
第六章 数组
3. 程序填空 (2)求能整除k且是偶数 #include <coino.h> #include <stdio.h>
的数,把这些数保
存在数组中,并按 从大到小 输出。
main() { int i,j=0,k,a[100]; scanf ("%d",&k); for( i=2 ;i<=k;i++) if(k%i==0 && i%2==0) a[j++]=i; printf("\n\n "); for(i= j-1 ;i>=0;i--) printf("%d",a[i]); }
#include <string.h> 第六章 数组 #include <stdio.h> 4.#define 改错题: M 80 (3) 计算一个字符串中包含指定子字符串的数目 . void main() { char s1[M],s2[M]; int num,i,j=0; gets(s1); /*输入字符串*/ gets(s2); /*输入子字符串*/ num=0; /******found******/ for(i=0;s1[i]<>'\0';i++) <> → != { if(s2[j]==s1[i]) { j++; if(s2[j]=='\0') { num++; /******found******/ j=i; j=0; } } } printf("\nThe result is : m=%d\n",num); }
第六章 数组
1. 选择题 (1) C语言中一维数组的定义方式为:类型说明符 数组名 A. [整型常量] B. [整型表达式] C
C. [整型常量]或[整型常量表达式] D. [变量表达式] (2) C语言中引用数组元素时,下标表达式的类型为 A.单精度型 B.双精度型 C.整型 D.指针型
C

(3) 若有说明 “int a[3][4];”,则对a数组元素的非法引用是 D A. a[0][3*1] B. a[2][3] C. a[1+1][0] D. a[0][4]
第六章 数组 #include <stdio.h> 4. void 改错题: main() (4){将大于整数m且紧靠m的k个素数存入数组xx。例如,若输入17(m), /******found******/ 5(k),则应输出 19,23,29,31,37. int m,k,xx[100],cnt,i; cnt → cnt=0; scanf("%d%d",&m,&k); while(cnt<k) { m++; /******found******/ for(i=2;i<m;i++) if!(m%i)break; if(i==m) xx[cnt++]=m; if(!(m%i))break; } /******found******/ for(i=0;i<=cnt;i++) i<=cnt → i<cnt; printf("%d ",xx[i]); }
printf(“The longest string is: \n%s\n”,str);
}
#include <ctype.h> 第六章 数组 #include <string.h> <stdio.h>/****found****/下面的语句中都有一处错误, 4.#include 改错题:每个 main() { 请将错误的地方改正。注意:不得增行或删行,也不得更 char str[81]; 改程序的结构。 int i=0,j,n; gets(str); (1) 读入一个英文文本行,将其中每个单词的第一个字母改成 /******found******/ n=stringlen(str); n=strlen(str) 大写,然后输出此文本行(这里的“单词”是指由空格隔 for(j=0;j<n;j++) 开的字符串。 if(i) { /******found******/ if(s[i]==' ') i=0; if(str[j]==' ') i=0; } else { /******found******/ if(str[j]!= " "); if(str[j]!=' ') { i=1; str[j]=toupper(str[j]); } } printf("\nAfter changing:\n %s\n",str); }
B.a[a[4]]
C. a[a[3]]
D. a[a[5]]
第六章 数组
1. 选择题 (6) 要求定义包含8个int类型元素的一维数组,以下错误的定义 语句是 A A. int N=8; B. #define N 3 int a[N]; int a[2*N+2]; C. int a[ ]={0,1,2,3,4,5,6,7} D. int a[1+7]={0}
字符串,并倒序保
存在字符数组s中。 例如:当n=123 时,s=“321”。
第六章 数组
3. 程序填空 (4)找出三个字符串中的最大者 #include <string.h> main( ) { char str[20],s[3][20]; int i; for(i=0; i<3; i++) gets(s[i]); if(strcmp( s[0],s[1] )>0) strcpy(str,s[0]); else strcpy( str,s[1] ); if(strcmp( s[2],str )>0) strcpy(str,s[2]);
abcdef”;
第六章 数组
1. 选择题 (9) 下面程序段的运行结果是 B char c[ ]=“\t\v\\\0will\n”; printf(“%d”,strlen(c)); A. 14 B. 3 C.9 D. 字符串中有非法字符,输出值不确定 (10) 判断字符串s1是否等于字符串s2,应当使用 D A. if(s1==s2) B. if(s1=s2) C. if(strcpy(s1,s2)) D. if(strcmp(s1,s2))==0)
}
第六章 数组
2. 写出程序的运行结果 (2) 程序二: main( ) { int i,j,row,col,max; int a[3][4]={{1,2,3,4},{9,8,7,6},{-1,-2,0,5}}; max=a[0][0]; row=0; col=0; for(i=0; i<3; i++) max=9 for(j=0; j<4; j++) if(a[i][j]>max) row=1 { max=a[i][j]; col=0 row=i; col=j; } printf(“max=%d, row=%d, col=%d\n”,max,row,col); }
相关主题