C语言程序设计课后习题答案(第四版)谭浩强 下载本文

}

第6章利用数组处理批量数据142 怎样定义和引用一维数组142 怎样定义一维数组143 怎样引用一维数组元素144 一维数组的初始化145 一维数组程序举例146

怎样定义和引用二维数组148 怎样定义二维数组149

怎样引用二维数组的元素150 二维数组的初始化151 二维数组程序举例152 字符数组154

怎样定义字符数组154 字符数组的初始化155

怎样引用字符数组中的元素155 字符串和字符串结束标志156 字符数组的输入输出159 使用字符串处理函数161 字符数组应用举例165 习题168 6-1

#include <> #include <> int main()

{int i,j,n,a[101];

for (i=1;i<=100;i++) a[i]=i; a[1]=0;

for (i=2;i

for (i=2,n=0;i<=100;i++) { if(a[i]!=0) {printf(\ n++; }

if(n==10)

{printf(\ n=0; } }

printf(\ return 0; } 6-2

#include <> int main()

{int i,j,min,temp,a[11]; printf(\ for (i=1;i<=10;i++) {printf(\ scanf(\ }

printf(\

printf(\ for (i=1;i<=10;i++) printf(\ printf(\

for (i=1;i<=9;i++) {min=i;

for (j=i+1;j<=10;j++) if (a[min]>a[j]) min=j; temp=a[i]; a[i]=a[min]; a[min]=temp; }

printf(\ for (i=1;i<=10;i++) printf(\ printf(\ return 0; } 6-3

#include <> int main() {

int a[3][3],sum=0;

int i,j;

printf(\ for (i=0;i<3;i++) for (j=0;j<3;j++)

scanf(\ for (i=0;i<3;i++) sum=sum+a[i][i];

printf(\ return 0; } 6-4

#include <> int main()

{ int a[11]={1,4,6,9,13,16,19,28,40,100}; int temp1,temp2,number,end,i,j; printf(\ for (i=0;i<10;i++) printf(\ printf(\

printf(\ scanf(\ end=a[9];

if (number>end) a[10]=number; else

{for (i=0;i<10;i++) {if (a[i]>number) {temp1=a[i]; a[i]=number; for (j=i+1;j<11;j++) {temp2=a[j]; a[j]=temp1; temp1=temp2; } break; } } }

printf(\ for (i=0;i<11;i++) printf(\ printf(\ return 0; }

6-5

#include <> #define N 5 int main()

{ int a[N],i,temp;

printf(\ for (i=0;i

for (i=0;i

#include <> int main()

{int i,j,upp,low,dig,spa,oth; char text[3][80];

upp=low=dig=spa=oth=0; for (i=0;i<3;i++)

{ printf(\ gets(text[i]);

for (j=0;j<80 && text[i][j]!='\\0';j++) {if (text[i][j]>='A'&& text[i][j]<='Z') upp++;

else if (text[i][j]>='a' && text[i][j]<='z') low++;

else if (text[i][j]>='0' && text[i][j]<='9') dig++;

else if (text[i][j]==' ') spa++; else oth++; }