c语言第八章课后题答案剖析
c语言-C程序设计(第四版)谭浩强_课后习题答案第8章
C程序设计(第四版)谭浩强_课后习题答案第8章第8章善于利用指针2208.1指针是什么2208.2指针变量2228.2.1使用指针变量的例子2228.2.2怎样定义指针变量2238.2.3怎样引用指针变量2248.2.4指针变量作为函数参数2268.3通过指针引用数组2308.3.1数组元素的指针2308.3.2在引用数组元素时指针的运算2318.3.3通过指针引用数组元素2338.3.4用数组名作函数参数2378.3.5通过指针引用多维数组2458.4通过指针引用字符串2558.4.1字符串的引用方式2558.4.2字符指针作函数参数2598.4.3使用字符指针变量和字符数组的比较2638.5指向函数的指针2668.5.1什么是函数指针2668.5.2用函数指针变量调用函数2668.5.3怎样定义和使用指向函数的指针变量2688.5.4用指向函数的指针作函数参数2708.6返回指针值的函数2748.7指针数组和多重指针2778.7.1什么是指针数组2778.7.2指向指针数据的指针2808.7.3指针数组作main函数的形参2828.8动态内存分配与指向它的指针变量2858.8.1什么是内存的动态分配2858.8.2怎样建立内存的动态分配2858.8.3void指针类型2878.9有关指针的小结288习题2918-1#include <stdio.h>int main(){ void swap(int *p1,int *p2);int n1,n2,n3;int *p1,*p2,*p3;printf("input three integer n1,n2,n3:");scanf("%d,%d,%d",&n1,&n2,&n3);p1=&n1;p2=&n2;p3=&n3;if(n1>n2) swap(p1,p2);if(n1>n3) swap(p1,p3);if(n2>n3) swap(p2,p3);printf("Now,the order is:%d,%d,%d\n",n1,n2,n3); return 0;}void swap(int *p1,int *p2){int p;p=*p1; *p1=*p2; *p2=p;}#include <stdio.h>#include <string.h>int main(){void swap(char *,char *);char str1[20],str2[20],str3[20];printf("input three line:\n");gets(str1);gets(str2);gets(str3);if(strcmp(str1,str2)>0) swap(str1,str2);if(strcmp(str1,str3)>0) swap(str1,str3);if(strcmp(str2,str3)>0) swap(str2,str3);printf("Now,the order is:\n");printf("%s\n%s\n%s\n",str1,str2,str3);return 0;}void swap(char *p1,char *p2){char p[20];strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);}8-3#include <stdio.h>int main(){ void input(int *);void max_min_value(int *);void output(int *);int number[10];input(number);max_min_value(number);output(number);return 0;}void input(int *number){int i;printf("input 10 numbers:");for (i=0;i<10;i++)scanf("%d",&number[i]);}void max_min_value(int *number){ int *max,*min,*p,temp;max=min=number;for (p=number+1;p<number+10;p++)if (*p>*max) max=p;else if (*p<*min) min=p;temp=number[0];number[0]=*min;*min=temp;if(max==number) max=min;temp=number[9];number[9]=*max;*max=temp; }void output(int *number){int *p;printf("Now,they are: ");for (p=number;p<number+10;p++)printf("%d ",*p);printf("\n");}8-4#include <stdio.h>int main(){void move(int [20],int,int);int number[20],n,m,i;printf("how many numbers?");scanf("%d",&n);printf("input %d numbers:\n",n);for (i=0;i<n;i++)scanf("%d",&number[i]);printf("how many place you want move?"); scanf("%d",&m);move(number,n,m);printf("Now,they are:\n");for (i=0;i<n;i++)printf("%d ",number[i]);printf("\n");return 0;}void move(int array[20],int n,int m){int *p,array_end;array_end=*(array+n-1);for (p=array+n-1;p>array;p--)*p=*(p-1);*array=array_end;m--;if (m>0) move(array,n,m);}8-5#include <stdio.h>int main(){int i,k,m,n,num[50],*p;printf("\ninput number of person: n="); scanf("%d",&n);p=num;for (i=0;i<n;i++)*(p+i)=i+1;i=0;k=0;m=0;while (m<n-1){if (*(p+i)!=0) k++;if (k==3){*(p+i)=0;k=0;m++;}i++;if (i==n) i=0;}while(*p==0) p++;printf("The last one is NO.%d\n",*p); return 0;}8-6#include <stdio.h>int main(){int length(char *p);int len;char str[20];printf("input string: ");scanf("%s",str);len=length(str);printf("The length of string is %d.\n",len); return 0;}int length(char *p){int n;n=0;while (*p!='\0'){n++;p++;}return(n);}8-7#include <stdio.h>#include <string.h>int main(){void copystr(char *,char *,int);int m;char str1[20],str2[20];printf("input string:");gets(str1);printf("which character that begin to copy?"); scanf("%d",&m);if (strlen(str1)<m)printf("input error!");else{copystr(str1,str2,m);printf("result:%s\n",str2);}return 0;}void copystr(char *p1,char *p2,int m){int n;n=0;while (n<m-1){n++;p1++;}while (*p1!='\0'){*p2=*p1;p1++;p2++;}*p2='\0';}8-8#include <stdio.h>int main(){int upper=0,lower=0,digit=0,space=0,other=0,i=0; char *p,s[20];printf("input string: ");while ((s[i]=getchar())!='\n') i++;p=&s[0];while (*p!='\n'){if (('A'<=*p) && (*p<='Z'))++upper;else if (('a'<=*p) && (*p<='z'))++lower;else if (*p==' ')++space;else if ((*p<='9') && (*p>='0'))++digit;else++other;p++;}printf("upper case:%d lower case:%d",upper,lower);printf(" space:%d digit:%d other:%d\n",space,digit,other); return 0;}8-9#include <stdio.h>int main(){void move(int *pointer);int a[3][3],*p,i;printf("input matrix:\n");for (i=0;i<3;i++)scanf("%d %d %d",&a[i][0],&a[i][1],&a[i][2]);p=&a[0][0];move(p);printf("Now,matrix:\n");for (i=0;i<3;i++)printf("%d %d %d\n",a[i][0],a[i][1],a[i][2]);return 0;}void move(int *pointer){int i,j,t;for (i=0;i<3;i++)for (j=i;j<3;j++){t=*(pointer+3*i+j);*(pointer+3*i+j)=*(pointer+3*j+i);*(pointer+3*j+i)=t;}}8-10-1#include <stdio.h>int main(){void change(int *p);int a[5][5],*p,i,j;printf("input matrix:\n");for (i=0;i<5;i++)for (j=0;j<5;j++)scanf("%d",&a[i][j]);p=&a[0][0];change(p);printf("Now,matrix:\n");for (i=0;i<5;i++){for (j=0;j<5;j++)printf("%d ",a[i][j]);printf("\n");}return 0;}void change(int *p){int i,j,temp;int *pmax,*pmin;pmax=p;pmin=p;for (i=0;i<5;i++)for (j=i;j<5;j++){if (*pmax<*(p+5*i+j)) pmax=p+5*i+j;if (*pmin>*(p+5*i+j)) pmin=p+5*i+j;}temp=*(p+12);*(p+12)=*pmax;*pmax=temp;temp=*p;*p=*pmin;*pmin=temp;pmin=p+1;for (i=0;i<5;i++)for (j=0;j<5;j++)if (((p+5*i+j)!=p) && (*pmin>*(p+5*i+j))) pmin=p+5*i+j;temp=*pmin;*pmin=*(p+4);*(p+4)=temp;pmin=p+1;for (i=0;i<5;i++)for (j=0;j<5;j++)if (((p+5*i+j)!=(p+4))&&((p+5*i+j)!=p)&&(*pmin>*(p+5*i+j)))pmin=p+5*i+j;temp=*pmin;*pmin=*(p+20);*(p+20)=temp;pmin=p+1;for (i=0;i<5;i++)for (j=0;j<5;j++)if (((p+5*i+j)!=p) && ((p+5*i+j)!=(p+4)) && ((p+5*i+j)!=(p+20)) && (*pmin>*(p+5*i+j)))pmin=p+5*i+j;temp=*pmin;*pmin=*(p+24);*(p+24)=temp;}8-10-2#include <stdio.h>int main(){void change(int *p);int a[5][5],*p,i,j;printf("input matrix:\n");for (i=0;i<5;i++)for (j=0;j<5;j++)scanf("%d",&a[i][j]);p=&a[0][0];change(p);printf("Now,matrix:\n");for (i=0;i<5;i++){for (j=0;j<5;j++)printf("%d ",a[i][j]);printf("\n");}return 0;}void change(int *p) //交换函数{int i,j,temp;int *pmax,*pmin;pmax=p;pmin=p;for (i=0;i<5;i++) //找最大值和最小值的地址,并赋给pmax,pmin for (j=i;j<5;j++){if (*pmax<*(p+5*i+j)) pmax=p+5*i+j;if (*pmin>*(p+5*i+j)) pmin=p+5*i+j;}temp=*(p+12); //将最大值与中心元素互换*(p+12)=*pmax;*pmax=temp;temp=*p; //将最小值与左上角元素互换*p=*pmin;*pmin=temp;pmin=p+1;//将a[0][1]的地址赋给pmin,从该位置开始找最小的元素for (i=0;i<5;i++) //找第二最小值的地址赋给pminfor (j=0;j<5;j++){if(i==0 && j==0) continue;if (*pmin > *(p+5*i+j)) pmin=p+5*i+j;}temp=*pmin; //将第二最小值与右上角元素互换*pmin=*(p+4);*(p+4)=temp;pmin=p+1;for (i=0;i<5;i++) //找第三最小值的地址赋给pminfor (j=0;j<5;j++){if((i==0 && j==0) ||(i==0 && j==4)) continue;if(*pmin>*(p+5*i+j)) pmin=p+5*i+j;}temp=*pmin; // 将第三最小值与左下角元素互换*pmin=*(p+20);*(p+20)=temp;pmin=p+1;for (i=0;i<5;i++) // 找第四最小值的地址赋给pminfor (j=0;j<5;j++){if ((i==0 && j==0) ||(i==0 && j==4)||(i==4 && j==0)) continue;if (*pmin>*(p+5*i+j)) pmin=p+5*i+j;}temp=*pmin; //将第四最小值与右下角元素互换*pmin=*(p+24);*(p+24)=temp;}8-11-1#include <stdio.h>#include <string.h>int main(){void sort(char s[][6]);int i;char str[10][6];printf("input 10 strings:\n");for (i=0;i<10;i++)scanf("%s",str[i]);sort(str);printf("Now,the sequence is:\n"); for (i=0;i<10;i++)printf("%s\n",str[i]);return 0;}void sort(char s[10][6]){int i,j;char *p,temp[10];p=temp;for (i=0;i<9;i++)for (j=0;j<9-i;j++)if (strcmp(s[j],s[j+1])>0){strcpy(p,s[j]);strcpy(s[j],s[+j+1]);strcpy(s[j+1],p);}}8-11-2#include <stdio.h>#include <string.h>int main(){void sort(char (*p)[6]);int i;char str[10][6];char (*p)[6];printf("input 10 strings:\n");for (i=0;i<10;i++)scanf("%s",str[i]);p=str;sort(p);printf("Now,the sequence is:\n"); for (i=0;i<10;i++)printf("%s\n",str[i]);return 0;}void sort(char (*s)[6]){int i,j;char temp[6],*t=temp;for (i=0;i<9;i++)for (j=0;j<9-i;j++)if (strcmp(s[j],s[j+1])>0){strcpy(t,s[j]);strcpy(s[j],s[+j+1]);strcpy(s[j+1],t);}}8-12#include <stdio.h>#include <string.h>int main(){void sort(char *[]);int i;char *p[10],str[10][20];for (i=0;i<10;i++)p[i]=str[i];printf("input 10 strings:\n");for (i=0;i<10;i++)scanf("%s",p[i]);sort(p);printf("Now,the sequence is:\n"); for (i=0;i<10;i++)printf("%s\n",p[i]);return 0;}void sort(char *s[]){int i,j;char *temp;for (i=0;i<9;i++)for (j=0;j<9-i;j++)if (strcmp(*(s+j),*(s+j+1))>0){temp=*(s+j);*(s+j)=*(s+j+1);*(s+j+1)=temp;}}8-13#include<stdio.h>#include<math.h>int main(){float integral(float(*)(float),float,float,int);//对integarl函数的声明float fsin(float); //对fsin函数的声明float fcos(float); //对fcos函数的声明float fexp(float); //对fexp函数的声明float a1,b1,a2,b2,a3,b3,c,(*p)(float);int n=20;printf("input a1,b1:");scanf("%f,%f",&a1,&b1);printf("input a2,b2:");scanf("%f,%f",&a2,&b2);printf("input a3,b3:");scanf("%f,%f",&a3,&b3);p=fsin;c=integral(p,a1,b1,n);printf("The integral of sin(x) is:%f\n",c);p=fcos;c=integral(p,a2,b2,n);printf("The integral of cos(x) is:%f\n",c);p=fexp;c=integral(p,a3,b3,n);printf("The integral of exp(x) is:%f\n",c);return 0;}float integral(float(*p)(float),float a,float b,int n){int i;float x,h,s;h=(b-a)/n;x=a;s=0;for(i=1;i<=n;i++){x=x+h;s=s+(*p)(x)*h;}return(s);}float fsin(float x){return sin(x);}float fcos(float x){return cos(x);}float fexp(float x){return exp(x);}8-14#include <stdio.h>int main(){void sort (char *p,int m);int i,n;char *p,num[20];printf("input n:");scanf("%d",&n);printf("please input these numbers:\n");for (i=0;i<n;i++)scanf("%d",&num[i]);p=&num[0];sort(p,n);printf("Now,the sequence is:\n");for (i=0;i<n;i++)printf("%d ",num[i]);printf("\n");return 0;}void sort (char *p,int m) // 将n个数逆序排列函数{int i;char temp, *p1,*p2;for (i=0;i<m/2;i++){p1=p+i;p2=p+(m-1-i);temp=*p1;*p1=*p2;*p2=temp;}}8-15#include <stdio.h>int main(){void avsco(float *,float *);void avcour1(char (*)[10],float *);void fali2(char course[5][10],int num[],float *pscore,float aver[4]); void good(char course[5][10],int num[4],float *pscore,float aver[4]); int i,j,*pnum,num[4];float score[4][5],aver[4],*pscore,*paver;char course[5][10],(*pcourse)[10];printf("input course:\n");pcourse=course;for (i=0;i<5;i++)scanf("%s",course[i]);printf("input NO. and scores:\n");printf("NO.");for (i=0;i<5;i++)printf(",%s",course[i]);printf("\n");pscore=&score[0][0];pnum=&num[0];for (i=0;i<4;i++){scanf("%d",pnum+i);for (j=0;j<5;j++)scanf("%f",pscore+5*i+j);}paver=&aver[0];printf("\n\n");avsco(pscore,paver); // 求出每个学生的平均成绩avcour1(pcourse,pscore); // 求出第一门课的平均成绩printf("\n\n");fali2(pcourse,pnum,pscore,paver); // 找出2门课不及格的学生printf("\n\n");good(pcourse,pnum,pscore,paver); // 找出成绩好的学生return 0;}void avsco(float *pscore,float *paver) // 求每个学生的平均成绩的函数{int i,j;float sum,average;for (i=0;i<4;i++){sum=0.0;for (j=0;j<5;j++)sum=sum+(*(pscore+5*i+j)); //累计每个学生的各科成绩average=sum/5; //计算平均成绩*(paver+i)=average;}}void avcour1(char (*pcourse)[10],float *pscore) // 求第一课程的平均成绩的函数{int i;float sum,average1;sum=0.0;for (i=0;i<4;i++)sum=sum+(*(pscore+5*i)); //累计每个学生的得分average1=sum/4; //计算平均成绩printf("course 1:%s average score:%7.2f\n",*pcourse,average1);}void fali2(char course[5][10],int num[],float *pscore,float aver[4])// 找两门以上课程不及格的学生的函数{int i,j,k,labe1;printf(" ==========Student who is fail in two courses======= \n"); printf("NO. ");for (i=0;i<5;i++)printf("%11s",course[i]);printf(" average\n");for (i=0;i<4;i++){labe1=0;for (j=0;j<5;j++)if (*(pscore+5*i+j)<60.0) labe1++;if (labe1>=2){printf("%d",num[i]);for (k=0;k<5;k++)printf("%11.2f",*(pscore+5*i+k));printf("%11.2f\n",aver[i]);}}}void good(char course[5][10],int num[4],float *pscore,float aver[4]) // 找成绩优秀学生(各门85以上或平均90分以上)的函数{int i,j,k,n;printf(" ======Students whose score is good======\n");printf("NO. ");for (i=0;i<5;i++)printf("%11s",course[i]);printf(" average\n");for (i=0;i<4;i++){n=0;for (j=0;j<5;j++)if (*(pscore+5*i+j)>85.0) n++;if ((n==5)||(aver[i]>=90)){printf("%d",num[i]);for (k=0;k<5;k++)printf("%11.2f",*(pscore+5*i+k));printf("%11.2f\n",aver[i]);}}}8-16#include <stdio.h>int main(){char str[50],*pstr;int i,j,k,m,e10,digit,ndigit,a[10],*pa;printf("input a string:\n");gets(str);pstr=&str[0]; /*字符指针pstr置于数组str 首地址*/pa=&a[0]; /*指针pa置于a数组首地址*/ndigit=0; /*ndigit代表有多少个整数*/i=0; /*代表字符串中的第几个字符*/j=0;while(*(pstr+i)!='\0'){if((*(pstr+i)>='0') && (*(pstr+i)<='9'))j++;else{if (j>0){digit=*(pstr+i-1)-48; /*将个数位赋予digit*/k=1;while (k<j) /*将含有两位以上数的其它位的数值累计于digit*/{e10=1;for (m=1;m<=k;m++)e10=e10*10; /*e10代表该位数所应乘的因子*/digit=digit+(*(pstr+i-1-k)-48)*e10; /*将该位数的数值\累加于digit*/k++; /*位数K自增*/}*pa=digit; /*将数值赋予数组a*/ndigit++;pa++; /*指针pa指向a数组下一元素*/j=0;}}i++;}if (j>0) /*以数字结尾字符串的最后一个数据*/ {digit=*(pstr+i-1)-48; /*将个数位赋予digit*/k=1;while (k<j) /* 将含有两位以上数的其它位的数值累加于digit*/{e10=1;for (m=1;m<=k;m++)e10=e10*10; /*e10代表位数所应乘的因子*/digit=digit+(*(pstr+i-1-k)-48)*e10; /*将该位数的数值累加于digit*/k++; /*位数K自增*/}*pa=digit; /*将数值赋予数组a*/ndigit++;j=0;}printf("There are %d numbers in this line, they are:\n",ndigit);j=0;pa=&a[0];for (j=0;j<ndigit;j++) /*打印数据*/printf("%d ",*(pa+j));printf("\n");return 0;}8-17#include<stdio.h>int main(){int strcmp(char *p1,char *p2);int m;char str1[20],str2[20],*p1,*p2;printf("input two strings:\n");scanf("%s",str1);scanf("%s",str2);p1=&str1[0];p2=&str2[0];m=strcmp(p1,p2);printf("result:%d,\n",m);return 0;}int strcmp(char *p1,char *p2) //两个字符串比较函数{int i;i=0;while(*(p1+i)==*(p2+i))if (*(p1+i++)=='\0') return(0); //相等时返回结果0return(*(p1+i)-*(p2+i)); //不等时返回结果为第一个不等字符ASCII码的差值}8-18#include <stdio.h>int main(){char *month_name[13]={"illegal month","January","February","March","April", "May","June","july","August","September","October", "November","December"};int n;printf("input month:\n");scanf("%d",&n);if ((n<=12) && (n>=1))printf("It is %s.\n",*(month_name+n));elseprintf("It is wrong.\n");return 0;}8-19-1#include <stdio.h>#define NEWSIZE 1000 //指定开辟存区的最大容量char newbuf[NEWSIZE]; //定义字符数组newbufchar *newp=newbuf; //定义指针变量newp,指向可存区的始端char *new(int n) //定义开辟存区的函数new,开辟存储区后返回指针{if (newp+n<=newbuf+NEWSIZE) // 开辟区未超过newbuf数组的大小{newp+=n; // newp指向存储区的末尾return(newp-n); // 返回一个指针,它指向存区的开始位置}elsereturn(NULL); // 当存区不够分配时,返回一个空指针}8-19-2#include <stdio.h>#define NEWSIZE 1000char newbuf[NEWSIZE];char *newp=newbuf;void free(char *p) //释放存区函数{if (p>=newbuf && p< newbuf + NEWSIZE)newp=p;}8-20#define LINEMAX 20 /*定义字符串的最大长度*/int main(){int i;char **p,*pstr[5],str[5][LINEMAX];for (i=0;i<5;i++)pstr[i]=str[i]; /*将第i个字符串的首地址赋予指针数组pstr 的第i个元素*/ printf("input 5 strings:\n");for (i=0;i<5;i++)scanf("%s",pstr[i]);p=pstr;sort(p);printf("strings sorted:\n");for (i=0;i<5;i++)printf("%s\n",pstr[i]);}sort(char **p) /*冒泡法对5个字符串排序函数*/{int i,j;char *temp;for (i=0;i<5;i++){for (j=i+1;j<5;j++){if (strcmp(*(p+i),*(p+j))>0) /*比较后交换字符串地址*/{temp=*(p+i);*(p+i)=*(p+j);*(p+j)=temp;}}}return 0;}8-21#include<stdio.h>int main(){void sort(int **p,int n);int i,n,data[20],**p,*pstr[20];printf("input n:\n");scanf("%d",&n);for (i=0;i<n;i++)pstr[i]=&data[i]; //将第i个整数的地址赋予指针数组pstr 的第i个元素printf("input %d integer numbers:",n);for (i=0;i<n;i++)scanf("%d",pstr[i]);p=pstr;sort(p,n);printf("Now,the sequence is:\n");for (i=0;i<n;i++)printf("%d ",*pstr[i]);printf("\n");return 0;}void sort(int **p,int n){int i,j,*temp;for (i=0;i<n-1;i++){for (j=i+1;j<n;j++){if (**(p+i)>**(p+j)) //比较后交换整数地址{temp=*(p+i);*(p+i)=*(p+j);*(p+j)=temp;}}}}。
C语言第8章习题及答案
第八章用一个数组存放图书信息,每本书是一个结构,包括下列几项信息:书名、作者、出版年月、借出否,试写出描述这些信息的说明,并编写一个程序,读入若干本书的信息,然后打印出以上信息。
#include <>typedef struct{char Name[20];char Author[20];int Date_Year;int Date_Month;int loaned;} BOOK;#define N 10void main(){BOOK books[N];int i;for (i=0;i<N;i++){printf("Input Book's Name:");gets(books[i].Name);printf("Input Book's Author:");gets(books[i].Author);printf("Input Book's Year of Publishing:");scanf("%d",&books[i].Date_Year);printf("Input Book's Month of Publishing:");scanf("%d",&books[i].Date_Month);printf("Input Book's Status, 1-Loaned, 2-Keepin:");scanf("%d",&books[i].loaned);}for (i=0;i<N;i++){printf("Book: %s, Author: %s, Publishing:%d-%d, Status:%d\n", books[i].Name, books[i].Author, books[i].Date_Year, books[i].Date_Month, books[i].loaned);}}编写一个函数,统计并打印所输入的正文中的各个英文单词出现的次数,并按次数的递减顺序输出。
C语言程序设计(谭浩强)第八章详细课后答案
#if 0 //8.1#include<stdio.h>#include<math.h>void main(){int Common_divisor(int a,int b);int Common_mutiple(int m,int n);int result_1,result_2;int number_1,number_2;printf("Enter number_1 and number_2 value:");scanf("%d,%d",&number_1,&number_2);result_1 = Common_divisor(number_1,number_2);result_2 = Common_mutiple(number_1,number_2);printf("最大公约数:%d\n最小公倍数:%d\n",result_1,result_2); }int Common_divisor(int a,int b){int r = 1;while(r != 0){if(a>b){r = a%b;a = b;b = r;if(r == 0){return a;break;}}else{r = b%a;b = a;if(r == 0){return b;break;}a = r;}}}int Common_mutiple(int m,int n){int Common_divisor(int a,int b);int temp = 0,result = 0;temp = Common_divisor(m,n);result = (m*n)/temp;return result;}#endif#if 0 //8.2#include<stdio.h>#include<math.h>float result_1 =0.0,result_2 = 0.0;void main(){void Funvtion_1(float a,float b,float temp);void Function_2(float a,float b);float a,b,c;float temp;printf("Enter a,b,c value:");scanf("%f,%f,%f",&a,&b,&c);temp = b*b-4*a*c;if(temp > 0){Funvtion_1(a,b,temp);printf("x1 = %.3f,x2 = %.3f\n",result_1,result_2);}else if(temp < 0){printf("此函数没有根!\n");}else{Function_2(a,b);printf("x1 = x2 = %.3f\n",result_1);}}void Funvtion_1(float a,float b,float temp){result_1 = (-b+temp)/(2*a);result_2 = (-b-temp)/(2*a); }void Function_2(float a,float b) {result_1 = (-b)/(2*a);}#endif#if 0 //8.3#include<stdio.h>#include<math.h>void main(){void Prime(int n);int m;printf("Enter m value:");scanf("%d",&m);Prime(m);}void Prime(int n){int i = 0 ,k;k = (int)sqrt(n);for(i = 2; i <= k;i++){if(n%i == 0){break;}}if(i > k){printf("The number %d is a prime!\n",n);}else{printf("The number %d isn't a prime!\n",n);}}#endif#if 0#include<stdio.h>#include<math.h>int c[3][3];void main(){void Function(int b[3][3]);int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};int i,j;// int d[3][3];int k = sizeof(a[2])/sizeof(int); //列数int n = sizeof(a)/sizeof(int);for(i = 0; i < n/k;i++){for(j = 0;j < k;j++){printf("%d ",a[i][j]);}printf("\n");}Function(a);for(i = 0; i < k;i++){for(j = 0;j < n/k;j++){printf("%d ",c[i][j]);}printf("\n");}}void Function(int b[3][3]){int i = 0,j = 0;int k = sizeof(b[3])/sizeof(int);int n = sizeof(b)/sizeof(int);for(i = 0;i < k ;++i){for(j = 0;j < k; ++j){c[j][i] = b[i][j];}}}#endif#if 0#include<stdio.h>#include<math.h>#include<string.h>void main(){void Function(char a[100],char b[100]);char a[100];extern int b[100];int n = sizeof(a);printf("Enter a Array:");gets(a);Function(a,b);}void Function(char a[],char b[]){int i = 0;int n = sizeof(a)/sizeof(char);int m = strlen(a);for(i = 0; i < m;i++){b[i] = a[m-1-i];}//printf("put b Array:");//puts(b);for(i = 0;i < m;i++){putchar(b[i]);}}#endif#if 0 //8.6#include<stdio.h>#include<math.h>void main(){char a[20];char b[10];printf("Enter string a value:");gets(a);printf("Enter string b value:");gets(b);strcat(a,b);puts(a);}#endif#if 0 //8.6#include<stdio.h>#include<math.h>#include<string.h>int a[20];void main(){void Connect(char a[],char b[]);char b[10];printf("Enter string a value:");gets(a);printf("Enter string b value:");gets(b);Connect(a,b);// puts(a);printf("%s",a);}void Connect(char a[],char b[]) {int i = 0;int j = 0;while(a[i] != '\0'){i++;}while(b[j] != '\0'){a[i++] = b[j++];}// a[i] = '\0';}#endif#if 0//8.7#include<stdio.h>#include<math.h>#include<string.h>char b[100];void main(){void Research_vowel(char a[]);char a[50];printf("Enter string a value:");gets(a);Research_vowel(a);puts(b);}void Research_vowel(char a[]){int i = 0,j = 0;for(; i < strlen(a);i++){if(a[i] == 'a'|| a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u'){b[j] = a[i];j++;}}#endif#if 0//8.8#include<stdio.h>#include<math.h>char a[4];void main(){void Divide(int n);int n = 0,i = 0;printf("Enter n value(<10000):");scanf("%d",&n);Divide(n);for(;i<4;i++){printf("%d ",a[i]);}printf("\n");}void Divide(int n)int i = 0,c = 0,b = 0,m = 1000;while(m >= 1){c = n/m;b = n%m;m = m/10;a[i] = c;n = b;i++;}}#endif#if 0//8.9#include<stdio.h>#include<math.h>#include<string.h>int m = 0,n = 0,r = 0,l = 0;void main(){void Research_number(char a[]);char a[50];int i = 0;printf("enter string value:");gets(a);/*for(;i<strlen(a);i++){printf("%c",a[i]);}*/puts(a);puts("");Research_number(a);printf("字母个数是:%d,数字个数是:%d,空格个数是:%d,其它:%d\n",m,n,r,l);}void Research_number(char a[]){int i = 0;for(i = 0;i<strlen(a);i++){if((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z')){m++;}else if(a[i] > 47 && a[i] < 58){n++;}else if(a[i] == 32){r++;}else{l++;}}}#endif#if 0 //8.10#include<stdio.h>#include<math.h>#include<string.h>void main(){void Array(char a[]);char b[20];printf("Enter b string(<20):");gets(b);printf("Longest word is : ");Array(b);}void Array(char a[]){int i = 0,place = 0,point = 0,length = 1; //point表示最长单词的开始位置place表示最长单词最后的一个位置int max = 0,j = 0;int flag = 0; //为0 时是空格为1时是单词max = length;for(i = 0;i<=strlen(a);i++){if(a[i] != ' ' && a[i] != '\0'){if(0 == flag){j = i;flag = 1;}elselength++; //这个题让我做的非常的难过以后要好好的复习}else{flag = 0;if(max < length){max = length;point = j;place = i;}length = 1;}}for(i = point;i <= place;i++){printf("%c",a[i]);}printf("\n");}/*int judge_words(char b[20]){int i = 0,flag = 1;for(i = 0;i < strlen(b); i++){if((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z')){flag = 1;}else{flag = 0;}}}*/#if 0 //8.11#include<stdio.h>#include<math.h>void main(){void bubbling(char a[]);char a[10];printf("enter string a value:");gets(a);bubbling(a);puts(a);}void bubbling(char a[]){int i = 0,j = 0;char temp;for(i = 0;i<strlen(a) - 1;i++){for( j = 0;j<strlen(a)-1-i;j++)if(a[j] > a[j+1]){temp = a[j];a[j] = a[j+1];a[j+1] = temp;}}}}#endif#if 0 //8.12#include<stdio.h>#include<math.h>void main(){float Function(float x);float x = 1.5,x1 = 0.0;x1 = Function(x);printf("%.3f\n",x1);}float Function(float x){float x1;float y,y1;while(fabs(y1)>=1e-6){x = x1;y1 = x*x*x+2*x*x+3*x+4;y = 3*x*x+4*x+3;x1 = x - (y1/y);}return x1;}#endif#if 0//8.13#include<stdio.h>#include<math.h>void main(){float p(float x,int n);float x = 0.0,result = 0.0;int n = 0;printf("Enter x and n value:");scanf("%f,%d",&x,&n);result = p(x,n);printf("p(x) value :%.3f\n",result);}float p(float x,int n){float y = 0.0;if(n == 0){y = 0;}else if(n == 1){y = x;}else{y = ((2*n-1)*x-p(x,n-1)-(n-1)*p(x,n-2))/n;}return y;}#endif#if 0//8.14#include<stdio.h>#include<math.h>int r = 0,l = 0;void main(){float* EStudent_aver(float scores[10][5]);float* EClass_aver(float scores[10][5]);float Maxscore(float scores[10][5]);float variance(float scores[10][5]);float scores[10][5] = {{76,43,46,73,46},{34,67,89,32,47},{31,68,73,16,87},{31,67,31,37,63},{16 ,37,61,67,86},{31,34,60,49,29},{89,89,87,87,98},{97,34,89,42,89},{45,67,34,18,64},{ 32,16,87,42,15}};float *Student_aver,*Class_aver,Student_max = 0.0,aver_variance = 0.0;int i = 0,j = 0,k = 0;/* char Student_name[] = {"Zhanghua Xiaoming Ligang Xiaotao Zhangsan Xiaoqiang CBC Baixu Gongcheng BOBO "};char score_name[] = {"Chinese Math English Chemitry WuLi"};printf("\t");for(i = 0;i < strlen(score_name);i++){printf("%c",score_name[i]);}puts("");for(i = 0;i < strlen(Student_name);i++){printf("%c",Student_name[i]);if(Student_name[i] == 32){for(j = 0;j < 5;j++){printf(" %.0f\t",scores[k][j]);}k++;puts("");}}*//* printf("\n");for(i = 0; i < 10;i++){for(j = 0;j < 5;j++){printf(" %.0f\t",scores[i][j]);}puts("");}*/// float scores[10][5];printf("NO. course1 course2 course3 course4 course5\n");for(i = 0;i < 10;i++){printf("No.%2d\t",i+1);for(j = 0;j < 5;j++){printf("%.2f\t ",scores[i][j]);}puts("");}Student_aver = EStudent_aver(scores);Class_aver = EClass_aver(scores);Student_max = Maxscore(scores);aver_variance = variance(scores);printf("Student_aver:\n");for(i = 0;i < 10;i++){printf("%.2f ",Student_aver[i]);}printf("\nClass_aver:\n");for(i = 0 ;i < 5;i++){printf("%.2f ",Class_aver[i]);}printf("\n");printf("The Max Score:\n %.2f Name: No.%d , Course: course%d\n",Student_max,r,l);printf("Student average Variance: \n %.2f\n",aver_variance);}float * EStudent_aver(float scores[10][5]){static float aver[10],sum = 0.0;int i = 0,j = 0;for(i = 0;i < 10;i++){for(j = 0; j < 5; j++){sum += scores[i][j];}aver[i] = sum/5;sum = 0;}return aver;}float* EClass_aver(float scores[10][5]) {static float aver_1[5],sum = 0.0;int i = 0,j = 0;for(j = 0;j<5;j++){for(i = 0;i<10;i++){sum +=scores[i][j];}aver_1[j] = sum/10;sum = 0;}return aver_1;}float Maxscore(float scores[10][5]) {int i = 0,j = 0;float max = scores[0][0];for(i = 0;i < 10;i++){for(j = 0;j < 5;j++){if(max < scores[i][j]){max = scores[i][j];r = i + 1;l = j + 1;}}}return max;}float variance(float scores[10][5]){float* EStudent_aver(float scores[10][5]);float s = 0.0;float sum_1 = 0.0,sum_2 = 0.0;float number_1 = 0.0,number_2 = 0.0;float* student_aver;int i = 0,j = 0;student_aver = EStudent_aver(scores);for(i = 0;i<10;i++){sum_1 += student_aver[i]*student_aver[i];sum_2 += student_aver[i];}number_1 = sum_1/10;number_2 = (sum_2/10)*(sum_2/10);s = number_1 - number_2;return s;}#endif#if 0 //测试static#include<stdio.h>#include<math.h>static j;void f1(){static i = 0;i++;}void f2(){j = 0;j++;}void main(){int k = 0;for(k = 0;k < 10;k++){f1();f2();}}#endif#if 0//8.16#include<stdio.h>#include<math.h>void main(){void Change(int n);int m = 0;printf("Enter ox number:");scanf("%x",&m);// printf("%d\n",m);Change(m);}void Change(int n){//int m;printf("十进制是: %d\n",n); }#endif#if 0//8.17#include<stdio.h>#include<math.h>void main(){void Function(int n);int m;printf("Input a integer:");scanf("%d",&m);Function(m);}void Function(int n){int i;/* if(n!=0){ch[i] = n%10;temp = n/10;i++;Function(temp);}else{ch[i] = 0;}return ch;*/i = n/10;if(i != 0){Function(i);}putchar(n%10+'0');putchar(32);}#endif#if 0 //今天是今年的多少天#include<stdio.h>#include<math.h>void main(){int i = 1,d = 0,sum = 0;int year,month,day;printf("Enter year and month and day(2012/08/19):");scanf("%d/%d/%d",&year,&month,&day);sum = day;for(;i<month;i++){switch(i){case 1:case 3:case 5:case 7:case 8:case 10:case 12: d = 31; break;case 2:if(year%400 == 0 || year%4 == 0 && year%100!=0){d = 29;}else{d = 28;}break;case 4:case 6:case 9:case 11: d = 30;break;}sum += d;}printf("The %d of The year dates!\n",sum); }#endif#if 0 //8.15#include<stdio.h>#define N 10void main(){void Enter_massage(char staff_name[N][10],int staff_number[N]);void Sort(char staff_name[N][10],int staff_number[N]);int middle_search(int staff_number[N],int number);int number,temp = 0;char staff_name[N][10];int staff_number[N];Enter_massage(staff_name,staff_number);//输入信息Sort(staff_name,staff_number);printf("Enter search staff_number:");scanf("%d",&number);temp = middle_search(staff_number,number);printf("The staff number is : %d\n",staff_number[temp]);printf("The staff name is : %s\n",staff_name[temp]);}void Enter_massage(char staff_name[N][10],int staff_number[N]) {int i;for(i = 0;i < N;i++){printf("Enter Staff number No.:");scanf("%d",&staff_number[i]);fflush(stdin);printf("Input staff name:");gets(staff_name[i]);}}void Sort(char staff_name[N][10],int staff_number[N]) {int i,j;int temp1;char temp[N][10];for(i = 0;i < N - 1; i++){for(j = 0;j < N - 1 - i; j++){if(staff_number[j]>staff_number[j+1]){temp1 = staff_number[j];strcpy(temp[j],staff_name[j]);staff_number[j] = staff_number[j+1];strcpy(staff_name[j],staff_name[j+1]);staff_number[j+1] = temp1;strcpy(staff_name[j+1],temp[j]);}}/*if(staff_number[i]<staff_number[i+1]){temp1 = staff_number[i];staff_number[i] = staff_number[i+1];staff_number[i+1] = temp1;}*/}for(i = 0;i < N;i++){printf("staff number is : %d\n",staff_number[i]);printf("staff name is %s\n",staff_name[i]);}}int middle_search(int staff_number[N],int number) //折半查找法!{int min = 0,high = 0,mid = 0;int i = 0;min = 0;high = N - 1;while(min <= high){mid = (min+high)/2;if(staff_number[mid] > number){high = mid - 1;}else if(staff_number[mid] < number){min = mid + 1;}else{return mid;}}}#endif/*fasdfa the world helloint flag=0;flag=1;//letterflag=0;//space*/#if 0 //有问题输出的是不对的!为什么指针值temp+1的值不是指向的name[1]这一行的首地址而是++后指向的是name[0]下一列的地址呢?#include<stdio.h>void main(){char name[5][10];char temp[5][10];int i = 0;// temp = &name[0];for(i = 0;i < 5;i++){gets(name[i]);strcpy(temp[i],name[i]);}for(i = 0;i < 5;i++){//puts(name[i]);puts(temp[i]);}}#endif#if 0 //看不懂的程序8.15 #define N 10find(a,b)int a[],b[];{int i,j,s,t,c[N][2];for(i=0;i<N;i++){c[i][1]=a[i];c[i][1]=i;} for(i=0;i<N;i++)for(j=0;j<N-i-1;j++)if(c[i][0]>c[i+1][0]){t=c[i][0];c[i][0]=c[i+1][0];c[i+1][0]=t;s=c[i][1];c[i][1]=c[i+1][1];c[i+1][1]=s;}for(i=0;i<N;i++)b[i]=c[i][1];return;}lookfor(h,k)int h[],k;{int i,j;for(i=0;i<N;i++)if(h[i]-k==0) j=i;return j;}main(){int number[N],x[N],i,j,u,p;char name[N][20];for(i=0;i<N;i++){gets(name[i]);scanf("%d",&number[i]);} scanf("%d",&p);find(number,x);u=lookfor(number,p);for(i=0;i<N;i++){printf("%d",number[i]);puts(name[x[i]]);}puts(name[x[u]]);}#endif#if 1 //16进制转换成10进制#include<stdio.h>#include<math.h>#define N 10int flag = 1;void main(){extern int sum;int convert(char str[],int n);char str[N];int s = 0,n = 0;puts("Enter 16 OX number:");gets(str);n = strlen(str);s = convert(str,n);if(1 == flag){printf("10 d number is : %d\n",s);}else{printf("The number isn't OX \n");}}int convert(char str[],int n){int i,j = 0;int sum = 0;for(i = n - 1;i > -1;i--,j++){if(str[i] > 47 && str[i] < 58){sum = sum + (str[i] - 48) * (int)pow(16,j);}else if(str[i] > 64 && str[i] < 71){sum = sum + (str[i] - 55)* (int)pow(16,j);}else if(str[i] > 96 && str[i] < 103){sum = sum + (str[i] - 87) * (int)pow(16,j);}else{flag = 0;}}return sum;}#endif#if 0#include<stdio.h>#include<math.h>void main(){printf("%d\n",(int)pow(2,0)); }#endif。
c程序设计第八章答案
第八章函数例子8.1函数调用的简单例子#include<stdio.h>void main(){void printstar();void print_message();printstar();print_message();printstar();}void printstar(){int i;for(i=0;i<=17;i++)printf("*");printf("\n");}void print_message(){printf(" How do you do!\n");}注:c程序的执行是从main函数开始的,如是在main函数中调用其他函数,在调用结束后流程返回到main函数,在main函数中结束整个程序的运行。
8.1写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果。
两个整数由键盘输入。
注:在程序开发中,常将一些常用的功能模块编写成函数,放在公共函数库中供大家选用。
程序设计人员要善于利用函数,以减少重复编写程序段的工作量。
#include<stdio.h>void main(){int gy(int x,int y);int gb(int x,int y);int a,b,gys,gbs;scanf("%d %d",&a,&b);gys=gy(a,b);gbs=gb(a,b);printf("gys=%d,gbs=%d\n",gys,gbs);}int gy(int x,int y){int t,i,g1;if(x>y) t=y;else t=x;for(i=1;i<=t;i++){if(x%i==0 && y%i==0)g1=i;}return(g1);}int gb(int x,int y){int z,c;c=gy(x,y);z=(x*y)/c;return(z);}注:这是一个调用两个函数的源程序文件。
C语言课后习题答案第八章解析
作业八:函数程序设计答案{ double z; z 二x+y; return z; }(重要)3.以下正确的说法是A_°在C 语言中A)B) C) D)4.若调用一个函数,且此函数中没有return 语句,则正确的说法是D_。
没有返回值返回若干个系统默认值 能返回一个用户所希望的函数值 返回一个不确定的值(重要)5. 以下不正确的说法是B_° C 语言规定A) B)C) D)6. C 语言规定,简单变量做实参时,它和对应形参之间的数据传递方式是隹^A) B) C) D)7.以下程序有语法性错误,有关错误原因的正确说法是C_。
main()(一)选择丿 (30分) 1.以下正确的函数定义形式是 A) B)C) D) double double double double fun (int x, int fun (int x;int fun (int x, intfun(int x, y);y) y) y); 2.以下正确的函数形式是D_。
A) B) C) D) double fun (int x, int { z 二x+y; return z; } fun (int x, y){ int z: return z; } fun (x, y){ int x,y; double z;double fun (int x, inty) z 二x+y; return z; } y)实参和与其对应的形参各占用独立的存储单元 实参和与其对应的形参共占用一个存储单元 只有当实参和与其对应的形参同名时才共占用存储单元 形参是虚拟的,不占用存储单元该函数A) B) C) D)实参可以是常量、变量或表达式 形参可以是常量、变量或表达式 实参可以为任意类型形参应与其对应的实参类型一致 地址传递 单向值传递山实参传给形参,再山形参传回给实参 由用户指定传递方式int G=5, k;void Prt.char ();k=Prt_char(G);}A)语句void prt.char ();有错,它是函数调用语句,不能用void 说明B)变量名不能使用大写字母C)函数说明和函数调用语句之间有矛盾D)函数名不能使用下划线8.C语言允许函数值类型缺省定义,此时该函数值隐含的类型是A)float 型B)int 型C)long 型D)double 型9.C语言规定,函数返回值的类型是Lil D_oA)return语句中的表达式类型所决定B)调用该函数时的主调函数类型所决定C)调用该函数时系统临时决定D)在定义该函数时所指定的函数类型所决定10.下面函数调用语句含有实参的个数为」func((expl,exp2), (exp3,exp4,exp5));A) 1 B) 2 C) 4 D) 5 (重要)11.以下程序的功能是计算函数F(x, y, z)二(x+y)/(x-y) + (z+y)/(z-y)的值,请选择填空。
C语言程序设计 (何钦铭 颜晖 著) 高等教育出版社第八章 课后答案
}
习题8-7
/*输入5个字符串,按由小到大的顺序输出。*/
/*指针和数组及存储单元-选择排序算法*/
#include <stdio.h>
#include <string.h>
void main(void)
{
char s[5][80],t[80];
int i,j,index;
/*输入5个字符串到数组s*/
{
index=i;
for (j = i+1; j < n; j++ )
if (a[j] < a[index])index=j;/*比较大小,记录最小元的下标*/
swap(&a[i], &a[index]);/*交换最小元与a[i]的值*/
}
}
/*定义函数swap,实现两个数交换*/
void swap (int *px, int *py)
scanf("%d",&x);
/*调用find函数,在数组a中查找xห้องสมุดไป่ตู้在位置*/
result=find(a,10,x);
/*输出查找结果*/
if(result==-1)printf("Not found.\n");
else printf("The position is %d\n",result);
void mcopy(char *s,char *t,int m);
void main()
{
char s[80],t[80];
int m;
/*输入一个字符串*/
printf("Enter a string : ");
第八章 C语言习题及答案(第八章)
8-1 编写程序,将10个数34,3,29,63,70,16,85,82,90,93存放于一组数组中,求出这十个数的和及平均值。
解:#include "stdio.h"void main(){int a[10]={34,3,29,63,70,16,85.82,90,93};int i ,sum=0;float average ;for(i=0;i<10;i++){sum=sum+a[i] ;}average=sum/10.0;printf("sum=%d,average=%f\n",sum,average);}运行结果:sum=565,average=56.5000思考:数组有何特点?此问题如果不用数组进行处理将会怎样?8-2 编写程序,求存放于上题数组中10个数的最大值,最小值及所在的位置。
解:#include "stdio.h"void main(){int a[10]={34,3,29,63,70,16,85,82,90,93};int i,sum,max,min,d_max,d_min;max=min=a[0];d_max=d_min=0;for(i=1;i<10;i++){if(a[i]>max) {max=a[i];d_max=i;}if(a[i]<min) {min=a[i];d_min=i;}}printf("max=%d,a[%d]\n",max,d_max);printf("min=%d,a[%d]\n",min,d_min);}运行结果:max=93,a[9]Min=3,a[1]思考:数组a[i]中i的变化意味着什么?8-3 编写程序,从键盘读入50个数存放于一数组中,求出该数组中最大值、最小值及所在位置。
解:#include "stdio.h"void main(){float a[50],max,min;int i,d_max,d_min;for(i=0;i<50;i++)scanf("%f",&a[i]);max=min=a[0];d_max=d_min=0;for(i=1;i<50;i++){if(a[i]>max) {max=a[i];d_max=i;}if(a[i]<min) {min=a[i];d_min=i;}}printf("max=%d,a[%d]\n",max,d_max);printf("min=%d,a[%d]\n",min,d_min);}思考:此题中不用数组也可以处理吗?如果可以,区别之处在哪里?8-4将存放于上题数组中的50个数分别按升序,降序排序。
2024年度谭浩强《C程序设计》第八章习题解析
选择排序的时间复杂度也为O(n^2)。
47
2024/2/3
插入第一个元素
将数组的第一个元素视为已排序序列,从第二个元素开始遍历数组。
插入当前元素
将当前元素插入到已排序序列中的正确位置,即找到已排序序列中第一个大于当前元素的元素,并将当前元素插入到该元素之前。
重复插入操作
重复上述插入操作,直到整个数组都插入到已排序序列中。
给定一个3x3的矩阵A和一个整数k,求矩阵A的k次幂。解析:可以使用递归或迭代的方式来实现矩阵的幂运算。递归方式较为简洁但效率较低,迭代方式需要手动实现矩阵乘法和结果矩阵的初始化等操作,但效率较高。在实现时需要注意矩阵乘法的规则和结果矩阵的大小。
给定一个n阶方阵A,求其转置矩阵B。解析:首先声明两个n阶方阵A和B,然后通过嵌套循环遍历A中的每个元素,并将元素值交换后赋值给B中对应位置的元素即可。在实现时需要注意方阵的阶数n需要通过参数传递或动态分配内存来确定。
22
2024/2/3
max = a[i];
23
2024/2/3
}
if (a[i] < min) {
24
2024/2/3
min = a[i];
25
2024/2/3
1
2
3
}
}
printf("数组中的最大值为:%dn", max);
26
2024/2/3
printf("数组中的最小值为:%d", min);
38
2024/2/3
分析
遍历字符串中的每个字符,根据字符类型进行计数。
分析
分别遍历两个字符串的首尾字符,比较是否相等,若全部相等则为逆序串。
c语言第八章答案
}
8.10
8.11
8.12
#include<stdio.h>
int strlen(char* str)
{
int i=0;
while(*str++)
i++;
return i;
}
int cmpstrlen(int len[])
for(j=0;j<3;j++)
{
scanf("%d",&matrix[i][j]);
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{if(i==j)
sum=sum+matrix[i][j];}
printf("对角线之和为:%d\n",sum);
}
8.6
#include<stdio.h>
#define N 3
int array[N][N];
voi;
printf("请输入一个3×3矩阵的数据:\n");
for(j=0;j<N;j++)
for(i=0;i<N;i++)
scanf("%d",&array[i][j]);
8.3
(1)num[5]等价于*(num+5)
(2)data[k+1]等价于*(data+k+1)
谭浩强C语言完整详细答案(第8章)
谭浩强C语言答案8.1//最大公约数与最小公倍数#include<stdio.h>int gys(int a,int b){int m,n,t,r;if(a<b){t=a;a=b;b=t;}n=a;m=b;while((r=m%n)!=0){m=a;n=r;}return(n);}int gbs(int a,int b){int k;k=a*b/gys(a,b);return k;}void main(){int a,b;printf("请输入两个整数:\n");scanf("%d%d",&a,&b);printf("最大公约数是:%d\n",gys(a,b));printf("最小公倍数是:%d\n",gbs(a,b));}8.2//一元二次方程根的情况#include<stdio.h>#include<math.h>void gen(float a,float b,float c){float k1,k2;if(b*b-4*a*c<0)printf("该函数没有实数根!\n");else if(b*b-4*a*c==0){k1=-b+sqrt(b*b-4*a*c);printf("该函数有两个相同的根为:%3.2f",k1/2);printf("\n");}else{k1=(-b+sqrt(b*b-4*a*c))/(2*a);k2=(-b-sqrt(b*b-4*a*c))/(2*a);printf("该函数有两个不同的根为:%3.2f,%3.2f",k1,k2);printf("\n");}}void main(){float a,b,c;printf("请输入三个实数:\n");scanf("%f%f%f",&a,&b,&c);gen(a,b,c);}8.3//是否为素数#include<stdio.h>#include<math.h>void sushu(int a){int i;for(i=2;i<=a-1;i++)if(a%i==0) {printf("%d不是素数",a);break;}else {printf("%d是素数",a);break;}}void main(){int a;printf("请输入一个实数:\n");scanf("%d",&a);sushu(a);}8.4//矩阵转置#include<stdio.h>#include<math.h>#define N 3int array[N][N];convert(array)int array[3][3];{int i,j,t;for(i=0;i<N;i++)for(j=i+1;j<N;j++){t=array[i][j];array[i][j]=array[j][i];array[j][i]=t;}}void main(){int i,j;for(i=0;i<N;i++)for(j=0;j<N;j++)scanf("%d",&array[i][j]);convert(array);for(i=0;i<N;i++){printf("\n");for(j=0;j<N;j++)printf("%5d",array[i][j]);}}8.5//字符串反向输出#include<stdio.h>//#include<string.h>#include<math.h>void string(char a[]){int i,k;char b[10];for(i=0;a[i]!=0;i++);k=i;for(i=0;i<k;i++)b[i]=a[k-i-1];b[i]=0;//结束字符串标志printf("%s",b);}void main(){char a[10];printf("请输入一个字符串:\n");scanf("%s",a);printf("倒序后的字符串为:\n");string(a);}8.6//连接两个字符串#include<stdio.h>#include<string.h>#include<math.h>char concate(char str1[],char str2[],char str[]) {int i,j;for(i=0;str1[i]!='\0';i++)str[i]=str1[i];for(j=0;str2[j]!='\0';j++)str[i+j]=str2[j];str[i+j]='\0';}main(){char s1[100],s2[100],s[100];scanf("%s",s1);scanf("%s",s2);concate(s1,s2,s);printf("\ns=%s",s);}8.7//找出字符串中的元音字母#include<stdio.h>#include<string.h>#include<math.h>char find(char str1[],char str2[]){int i,j=0;for(i=0;str1[i]!=0;i++){if(str1[i]=='a'||str1[i]=='A'||str1[i]=='e'||str1[i]=='E'||str1[i]=='i'||str1[i]=='I'||str1[i]=='o'||str1[i]=='O'||str1[i]=='u'||str1[i]=='U')str2[j++]=str1[i];else continue;}str2[j]='\0';printf("%s中元音字母是%s\n",str1,str2);}main(){char str1[100],str2[100];scanf("%s",str1);find(str1,str2);}8.8//输入数字1990输出1 9 9 0#include<stdio.h>#include<string.h>void main(){char str[80];scanf("%s",str);insert(str);}insert(str)char str[];{int i;for(i=strlen(str);i>0;i--){str[i*2]=str[i];str[i*2-1]=' ';}printf("%s\n",str);}8.9//统计字母,数字,空格,和其他字符的个数#include<stdio.h>#include<string.h>int alph,digit,space,others;void main(){char text[80];gets(text);alph=0,digit=0,space=0,others=0;count(text);printf("\nalph=%d,digit=%d,space=%d,others=%d\n",alph,digit,space,others);}count(char str[]){int i;for(i=0;str[i]!='\0';i++)if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))alph++;else if(str[i]>='0'&&str[i]<='9')digit++;else if(strcmp(str[i],' ')==0)space++;else others++;}8.10//输出最长单词#include<stdio.h>#include<string.h>int zimu(char c){if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))return(1);elsereturn(0);}int longest(char string[]){int i,point,point1,num=0,nummax=0,flag=1;for(i=0;i<=strlen(string);i++)if(zimu(string[i]))//是字母则记下起始位置和长度if(flag){point=i;flag=0;}elsenum++;else //不是字母则比较长度重新记下初始位置{flag=1;if(num>nummax){nummax=num;point1=point;}}return point1;}void main(){int i;char line[100];gets(line);for(i=longest(line);zimu(line[i]);i++)printf("%c",line[i]);printf("\n");}8.11//冒泡法字符串排序#include<stdio.h>#include<string.h>#define N 10char str[N];main(){int i,flag;for(flag=1;flag==1;){scanf("%s",str);if(strlen(str)>N)printf("input error");else flag=0;}sort(str);for(i=0;i<N;i++)printf("%c",str[i]);sort(char str[N]){int i,j;char t;for(j=1;j<N;j++)for(i=0;(i<N-j)&&(str[i]!='\0');i++)if(str[i]>str[i+1]){t=str[i];str[i]=str[i+1];str[i+1]=t;}}8.12//找根的值,一元三次方程牛顿迭代公式法#include<stdio.h>#include<string.h>#include<math.h>float solut(float a,float b,float c,float d){float x=1,x0,f,f1;do{x0=x;f=((a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c;x=x0-f/f1;}while(fabs(x-x0)>=1e-5);return(x);}void main(){float a,b,c,d;scanf("%f,%f,%f,%f",&a,&b,&c,&d);printf("x=%10.7f\n",solut(a,b,c,d));}8.13//递归法#include<stdio.h>#include<string.h>#include<math.h>void main()int x,n;float p();scanf("%d,%d",&n,&x);printf("P%d(%d)=%10.2f\n",n,x,p(n,x));}float p(int tn,int tx){if(tn==0)return(1);else if(tn==1)return(tx);elsereturn(((2*tn-1)*tx*p((tn-1),tx)-(tn-1)*p((tn-2),tx))/tn);}8.14//输入10个学生5门课的成绩,分别用函数求(1)每个学生的平均分(2)每门课的平均分(3)招出最高分的学生和课程(4)求平均方差//输入10个学生5门课的成绩,分别用函数求(1)每个学生的平均分(2)每门课的平均分(3)招出最高分的学生和课程(4)求平均方差#include<stdio.h>#include<string.h>#include<math.h>#define N 4#define M 4float score[N][M];float a_stu[N],a_cor[M];main(){int i,j,r,c;float h;float s_diff();float highest();input_stu();avr_stu();avr_cor();printf("\n人数科目 1 2 3 4 平均分");for(i=0;i<N;i++){printf("\nNO%2d",i+1);for(j=0;j<M;j++)printf("%8.2f",score[i][j]);//每个学生的各门成绩printf("%8.2f",a_stu[i]); //每个学生的平均成绩}printf("\n课程平均成绩");for(j=0;j<M;j++)printf("%8.2f",a_cor[j]);h=highest(&r,&c);printf("\n最高分为%8.2f在第%d行第%d列\n",h,r,c);printf("\n平均分方差为%8.2f\n",s_diff());}/*输入是个学生的成绩*/input_stu(){int i,j;float x;for(i=0;i<N;i++){printf("请输入第%d个学生的成绩:\n",i+1);for(j=0;j<M;j++){scanf("%f",&x);score[i][j]=x;}}}/*输出每个学生的平均成绩*/avr_stu(){int i,j;float s;for(i=0;i<N;i++){for(j=0,s=0;j<M;j++)s+=score[i][j];a_stu[i]=s/5.0;}}/*输出每门课的平均成绩*/avr_cor(){int i,j;float s;for(j=0;j<M;j++){for(i=0,s=0;i<N;i++)s+=score[i][j];a_cor[j]=s/(float)N;}}/*求最高分和它属于哪个学生,哪门课*/float highest(int *r,int *c){float high;int i,j;high=score[0][0];for(i=0;i<N;i++)for(j=0;j<M;j++)if(score[i][j]>high){high=score[i][j];*r=i+1;//用指针实现双向传递很经典,也可该用全局变量*c=j+1;}return(high);}float s_diff(){int i,j;float sumx=0.0,sumxn=0.0;for(i=0;i<N;i++){sumx+=a_stu[i]*a_stu[i];sumxn+=a_stu[i];}return(sumx/N-(sumxn/N)*(sumxn/N));}8.15//输入10个职工的姓名和职工号按职工号由小到大排列,姓名顺序也随之调整//输入一个职工号用折半法找出该职工的姓名#include<stdio.h>#include<string.h>#include<math.h>#define N 3void input_e(num,name)int num[];char name[N][8];{int i;for(i=0;i<N;i++){printf("\ninput No.");scanf("%d",&num[i]);printf("input name:");getchar();gets(name[i]);}}void sort(num,name)int num[];char name[N][8];{int i,j,min,temp1;char temp2[8];for(i=0;i<N-1;i++){min=i;for(j=i;j<N;j++)if(num[min]>num[j])min=j;temp1=num[i];num[i]=num[min];num[min]=temp1;strcpy(temp2,name[i]);strcpy(name[i],name[min]);strcpy(name[min],temp2);}for(i=0;i<N;i++)printf("\n%5d%10s",num[i],name[i]); }void search(n,num,name)int n,num[];char name[N][8];{int top,bott,min,loca;loca=0;top=0;bott=N-1;if((n<num[0])||(n>num[N-1])) loca=-1;while((loca==0)&&(top<=bott)){min=(bott+top)/2;if(n==num[min]){loca=min;printf("number=%d,name=%s\n",n,name[loca]);}else if(n<num[min]) bott=min-1;else top=min+1;}if(loca==0||loca==-1)printf("number=%d is not in table\n",n);}main(){int num[N],number,flag,c,n;char name[N][8];input_e(num,name);sort(num,name);for(flag=1;flag;){scanf("%d",&number);search(number,num,name);printf("continueY/N!");c=getchar();if(c=='N'||c=='n')flag=0;}}8.16//输入16进制输出10进制#include<stdio.h>#include<string.h>#include<math.h>#define MAX 1000main(){int c,i,flag,flag1;char t[MAX];i=0;flag=0;flag1=1;while((c=getchar())!='\0'&&i<MAX&&flag1){if(c>='0'&&c<='9'||c>='A'&&c<='F'||c>='a'&&c<='f'){flag=1;t[i++]=c;}else if(flag){t[i]='\0';printf("\nnumber=%d\n",htoi(t));printf("continue");c=getchar();if(c=='n'||c=='N')flag1=0;else {flag=0; i=0; }}}}htoi(s)char s[];{int i,n;n=0;for(i=0;s[i]!='\0';i++){if(s[i]>='0'&&s[i]<='9')n=n*16+s[i]-'0';if(s[i]>='a'&&s[i]<='f')n=n*16+s[i]-'a'+10;if(s[i]>='A'&&s[i]<='F')n=n*16+s[i]-'A'+10;}return(n);}8.17//递归法#include<stdio.h>void convert(n)int n;{int i;if((i=n/10)!=0)convert(i);putchar(n%10+'0');}main(){int number;scanf("%d",&number);if(number<0){putchar('-');number=-number;}convert(number);}8.18//年月日具体是多少天#include<stdio.h>#include<string.h>#include<math.h>void main(){int year,month,day;int days;int sum_day();int leap();scanf("\n%d%d%d",&year,&month,&day);days=sum_day(month,day);if(leap(year)&&(month>=3)){days+=1;printf("%d年共有366天该天是其中的%d天",days);}elseprintf("%d年共有365天该天是其中的%d天",days); }static int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; /*总天数*/int sum_day(int month,int day){int i;for(i=1;i<month;i++)day+=day_tab[i];return(day);}/*是否为闰年*/int leap(int year){int leap;leap=year%4==0&&year%100!=0||year%400==0;return(leap);}。
《C语言程序设计》教材习题答案第8章
一、选择题1.以下数组定义中,错误的是:C)int a[3]={1,2,3,4};2.以下数组定义中,正确的是:B) int a[][2]={1,2,3,4};3.设有定义“int a[8][10];”,在VC中一个整数占用4字节,设a的起始地址为1000,则a[1][1]的地址是:D)10444.已知有数组定义“int a[][3]={1,2,3,4,5,6,7,8,9};”,则a[1][2]的值是:C)65.在以下字符串定义、初始化和赋值运算中,错误的是:A) char str[10];str=”String”;6.设有以下字符串定义,char s1[]={‘S’,’t’,’r’,’i’,’n’,’g’};char s2[]=”String”;则s1和s2:C)长度不同,但内容相同。
7.设有定义“int a[10]={0};”,则说法正确的是:A)数组a有10个元素,各元素的值为0.8.设已定义“char str[6]={‘a’,’b’,’\0’,’c’,’d’,’\0’};”,执行语句“printf(“%s”,str)”后,输出结果为:B)ab9.引用数组元素时,数组元素下标不可以是:C)字符串10.已定义字符串S1和S2,以下错误的输入语句是:C)gets(s1,s2);11.下面程序段的运行结果是:A)123void main(){char a[]=”abcd”,b[]=”123”;strcpy(a,b);printf(“%s\n”,a);}12.下面程序段的运行结果是:A)123void main(){char a[]=”123”,b[]=”abcd”;if(a>b)printf(“%s\n”,a);else printf(“%s\n”,b);}二、编程题1.一维数字a的值已经,请把数组中的值按逆序存放,然后输出数组。
例如数组中原来的值为3,4,2,1,6,颠倒后变成6,1,2,4,3.#include<stdio.h>main(){int i,a[5]={2,3,45,12,5},t;printf("转换前:");for(i=0;i<5;i++)printf("%d\t",a[i]);for(i=0;i<5/2;i++){t=a[i];a[i]=a[5-i-1];a[5-i-1]=t;}printf("\n转换后:");for(i=0;i<5;i++)printf("%d\t",a[i]);}2.输入一个整数(位数不确定),从高位到低位依次输出各位数字,其间用逗号分隔。
《C语言程序设计》课后习题答案第八章
8.1 编写两个函数,分别求两个证书的最大公约数和最小公倍数,用主函数调用这两个函数并输出结果,两个整数由键盘输入。
void main(){ int Mgy(int x,int y);int Mgb(int z);int a,b,mgy,mgb;printf("请输入两个数:\n");scanf("%d,%d",&a,&b);mgy=Mgy(a,b);mgb=Mgb(a,b,mgy);printf("两个数的最大公约数为%d,最小公倍数为%d\n",mgy,mgb);}int Mgy(int x,int y){ int r,temp;if(x<y){ temp=x;x=y;y=temp;}while(x%y!=0){ r=x%y;x=y;y=r;}return y;}int Mgb(int x,int y,int z){ return (x*y/z);}8.2 求方程ax²+bx+c=0的根,用三个函数分别求当b²-4ac大于零、等于零和小于零时的根,8.3编写一个判素数的函数,在主函数输入一个整数,输出是否是素数的信息。
#include<math.h>void main(){ int Isprime(int a);int m,temp=0;printf("请输入一个数:\n");scanf("%d",&m);temp=Isprime(m);if(temp==0) printf("%d不是素数。
\n",m);else printf("%d是素数。
\n",m);}int Isprime(int a){ int i,k,flag;if(a==0||a==1) flag=0;else{ k=sqrt(a);for(i=2;i<=k;i++)if(a%i==0) flag=0; }return flag; }8.8 写一个函数,输入一个4位数字,要求输出这4个数字字符,但每两个数字间空一格空8.9编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符8.10 写一个函数,输入一行字符,将此字符串中最长的单词输出。
c语言程序设计第五版课后答案谭浩强第八章课后答案
c语⾔程序设计第五版课后答案谭浩强第⼋章课后答案c语⾔程序设计第五版课后答案谭浩强习题答案第⼋章善于利⽤指针本章习题均要求使⽤指针⽅法处理。
1. 输⼊3个整数,要求按由⼩到⼤的顺序输出。
解题思路:先获取到三个变量的地址,然后获取三个数据,通过指针进⾏⽐较转换即可答案:#include <stdio.h>void swap(int *p_a, int *p_b){int temp = *p_a;*p_a = *p_b;*p_b = temp;}int main(){int a, b, c, *p_a = &a, *p_b = &b, *p_c = &c; // 获取每个变量空间的地址printf("Please enter three numbers:");scanf_s("%d%d%d", p_a, p_b, p_c);if (*p_a > *p_b) {swap(p_a, p_b);//通过指针进⾏指向空间内的数据交换}if (*p_a > *p_c) {swap(p_a, p_c);}if (*p_b > *p_c) {swap(p_b, p_c);}printf("%d %d %d\n", *p_a, *p_b, *p_c);system("pause");return 0;}2. 输⼊3个字符串,要求按由⼩到⼤的顺序输出。
解题思路:字符串的⽐较可以使⽤strcmp函数,返回值>0表⽰⼤于,返回值⼩于0表⽰⼩于,返回追等于0表⽰相同。
其他的⽐较排序思路与数字的排序交换没有区别,逐个进⾏⽐较先找出最⼤的,然后找出第⼆⼤的。
答案:#include <stdio.h>int main(){char str[3][32];char *p[3];printf("Please enter three strings:");for (int i = 0; i < 3; i++) {p[i] = str[i];scanf_s("%s", p[i], 32);//后边的数字限制缓冲区边界,防⽌缓冲区溢出访问越界}//让p[0]和p[1]/p[2]分别进⾏⽐较,找出最⼤的字符串,i+1之后,则让p[1]和p[2]进⾏⽐较,找出第⼆⼤//i循环总个数-1次,最后⼀个是不需要⽐较的for (int i = 0; i < 2; i++) {for (int j = i + 1; j < 3; j++) {if (strcmp(p[i], p[j]) > 0) {char *tmp = p[i]; p[i] = p[j]; p[j] = tmp;}}}printf("%s %s %s\n", p[0], p[1], p[2]);system("pause");return 0;}3. 输⼊10个整数,将其中最⼩的数与第⼀个数对换, 把最⼤的数与最后⼀个数对换。
C语言第八章习题及答案
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j) sum=sum+a[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%5d",a[i][j]);
a[3]=m%10;
for(i=0;i<4;i++)
a[i]=(a[i]+5)%10;
for(i=0;i<4;i++)
b[i]=a[3-i];
printf("加密后的四位整数:");
for(i=0;i<4;i++)
printf("%d",b[i]);
printf("\n");
{
int i,m,t,p;
int a[4];
printf("请输入一个四位整数:");
scanf("%d",&m);
a[0]=m/1000;
a[1]=(m%1000)/100;
a[2]=(m%100)/10;
a[3]=m%10;
for(i=0;i<4;i++)
printf("%5d",a[i][j]);
}
printf("\n");
}
printf("对角线元素之和=%d\n",sum);
C语言程序设计应用第八章习题答案
练习与思考 88.1 选择题(1)有以下定义及语句,则对数组a元素的不正确引用的表达式是()。
int a[4][5];*p[2],j;for (j = 0 ; j <4 ; j++)p[j]=a[j];A)p[0][0] B)*(a+3)[4]C)*(p[1]+2) D)*(&a[0][0]+3)(2) 有以下程序#include <stdio.h>struct tt{int x;struct tt *y;} *p;struct tt a[4]={20,a+1,15,a+2,30,a+3,17,a};main(){ int i;p=a;for(i=1;i<=2;i++) {printf("%d,",p->x); p=p->y;}}程序的运行结果是()。
A)20,30, B)30,17 C)15,30, D)20,15,8.2 填空题(1) 以下程序段的输出结果是()。
#include <stdio.h>#define F(a,b) printf("%d,%d\n",a,b)void main(){int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};F(a,a[0]);F(*a,*(a+0));F(a[1],*(a+1));F(*a[1],**(a+1));F(*(a[1]+1),*(*(a+1)+1));F(*a,**a);}(2) 以下程序的运行时,输入i=1,j=2(回车)结果是()。
#include <stdio.h>void main(){int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};int (*p)[4],i,j;p=a;scanf("i=%d,j=%d",&i,&j);printf("a[%d][%d]=%d\n",i,j,*(*(p+i)+j));}(3) 以下程序运行后的输出结果是()。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
int score[10];
Readscore(score);
Sort(score);
8-6、
#include <stdio.h>
#define N 40
int Readscore(int score[], long num[]);
int FindMax(int score[],long num[],int n);
void main()
{
int score[N];
long num[N];
maxPos=minPos=0;
for(n=0; n<10; n++)
{
if(a[n]>max)
{
max=a[n];
maxPos=n;
}
else if(a[n]<min)
{
min=a[n];
minPos=n;
}
}
printf("max=%d,pos=%d\n",max,maxPos);
printf("min=%d,pos=%d\n",min,minPos);
for(i=2; i<n; i++)
{
f[i]=f[i-1]+f[i-2];
}
}
(3)
#include <stdio.h>
int main()
{
int a[10],n,max,min,maxPos,minPos;
for(n=0; n<10; n++)
{
scanf("%d",&a[n]);
}
max=min=a[0];
printf("%d",x);
return 0;
}
结果:
分析:调用函数时只发生值的传递,形参与实参之间再也没有关系,子函数一旦退出,内部变量就释放空间
(2)
#include <stdio.h>
void Func(int b[])
{
int j;
for(j=0; j<4; j++)
{
b[j]=j;
}
}
int main()
return 0;
}
结果:
(4)
#include <stdio.h>
#define ROW 2
#define COL 3
MultiplyMatrix(int a[ROW][COL],int b[COL][ROW],int c[ROW][ROW])
{
int i,j,k;
for(i=0; i<ROW; i++)
{
int i;
int k;
int max=score[0];
for(i=0; i<n; i++)
{
if(score[i]>max)
{
max=score[i];
k=i;
}
}
return k;
}
结果:
8-7
#include <stdio.h>
void Sort(int score[]);
void Readscore(int score[]);
int n;
int m;
n=Readscore(score,num); /*接收人数*/
m=FindMax(score,num,n);
printf("最高的成绩:%d\n学号为:%ld\n",score[m],num[m]);
}
int Readscore(int score[], long num[])
{
static int a[] = {5,6,7,8},i;
Func(a);
for(i=0; i<4; i++)
{
printf("%d",a[i]);
}
return 0;
}
结果:
分析:数组传递时向函数传递的是数组的地址值,实参与形参共享空间,在被调函数中对数组进行修改就会造成主函数中的数组也被修改。
printf("Results:\n");
PrintMatrix(c);
return 0;
}
结果:
8-3、
viod DivArray(int pArray[],int n)
{
int i;
for (i=0; i<n; i++)
{
pArray[i]/=pArray[0];
}
}
错误分析:原代码中定义了一个未初始化的指针,而不是数组,有可能会使指针访问到不该访问的内存空间,造成危险,应该定义数组,对数组中的数进行修改操作。
{
for(j=0; j<ROW; j++)
{
printf("%6d",a[i][j]);
}
printf("\n");
}
}
int main()
{
int a[ROW][COL],b[COL][ROW],c[ROW][ROW],i,j;
printf("Input 2*3 matrix a:\n");
for(i=0; i<ROW; i++)
8.2(1)
int PositiveNum(int a[],int n)
{
int i,count=0;
for(i=0; i<n; i++)
{
if(a[i]>0)count++;
}
returncount;
}
(2)
void Fib(long f[],int n)
{
int i;
f[0]=0;
f[1]=1;
一、第八章习题8(p222-p228),8.1-8.3全做,8.4-8.12中选做四道,8.13-8.19中选做三道,要求给出所选择题目的程序及执行结果。
8-1(1)、
#include <stdio.h>
void Funቤተ መጻሕፍቲ ባይዱ(int x)
{
x=20;
}
int main()
{
int x=10;
Func(x);
{
int i=-1;
do{
i++;
printf("请输入学号:");
scanf("%ld",&num[i]);
printf("请输入成绩:");
scanf("%d",&score[i]);
}while(score[i]>=0 && num[i]>=0);
return i;
}
int FindMax(int score[],long num[],int n)
{
for(j=0; j<ROW; j++)
{
c[i][j]=0;
for(k=0; k<COL; k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
}
void PrintMatrix(int a[ROW][ROW])
{
int i,j;
for(i=0; i<ROW; i++)
{
for(j=0; j<COL; j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Input 3*2 matrix b:\n");
for(i=0; i<COL; i++)
{
for(j=0; j<ROW; j++)
{
scanf("%d",&b[i][j]);
}
}
MultiplyMatrix(a,b,c);