练习8函数的应用1ans_第1页
练习8函数的应用1ans_第2页
练习8函数的应用1ans_第3页
练习8函数的应用1ans_第4页
练习8函数的应用1ans_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、练习8 函数的应用1 参考答案一、目的1、 掌握函数的定义和调用。2、 掌握普通变量、一维数组、字符串作函数参数时的参数传递。二、实验1 程序填空编写函数:C8_1c.C中的函数double fun(double x)实现如下功能(指导P219编程题1)。请在一对/*/之间填写函数体。 x (x<1)fun(x)= (1x<10) ln3x (x10)注:fun(0.76)= 0.760,fun(5.10)= 3.033double fun(double x)/*/ double y; if(x<1) y=x; else if (x<10) y=sqrt(2*x-1);

2、 else y=log(3*x); return y;/*/void main()clrscr(); printf("fun(0.76) = %8.3lfn", fun(0.76); printf("fun(5.10) = %8.3lfn", fun(5.10); getch();2 程序填空:C8_2c.C实现指导P153填空题4(通过调用add函数求两个浮点数的和)。请在每对/*/之间填写内容。#include <stdio.h>void add(double x,double y,double *p)/*/ *p /*/=x+y;voi

3、d main() double a=55.44, b=44.55 ; double k; add(a,b,/*/ &k /*/); /*调用时,使x=a,y=b,p=&k即pk */ printf("%.2lfn",k);3 书面作业选做程序填空编写函数:C8_3c.C实现指导P40实验3.7-4, 函数void sort(int a, int n)用冒泡排序法对数组a中的n个数按从小到大排序。请在一对/*/之间填写函数体。 (算法参见教程P138例5.11)注:输入第一组数据:5 4 3 2 1;输入第二组数据:3 5 4 1 2;验证程序的正确性。#in

4、clude <stdio.h>#define N 5void sort(int a, int n)/*/ int i,j,temp; for(i=N-1;i>=1;i-) for(j=0;j<i;j+) if(aj>aj+1) temp=aj; aj=aj+1; aj+1=temp; /*/ void main() int aN,i; printf("Enter %d numbers: ",N); for(i=0;i<N;i+)scanf("%d",&ai); sort(a,N); printf("S

5、orted numbers :n"); for(i=0;i<=N-1;i+) printf("%d ",ai); printf("n"); getch();4 程序填空:C8_4c.C中的函数 int sortfind(int a,int n,int x) 的功能是用折半查找法(二分查找法)从已按升序排列的数组a(共n个元素)中查找x,返回x在a中的位置,若没找到则返回-1。请在每对/*/之间填写内容。(算法参见教程P134例5.7)#include <stdio.h>#include <conio.h>#defi

6、ne N 10void main( ) int a=7, 9, 12, 18, 21, 25, 33, 39, 45, 60; int x, x_at; int sortfind(int a,int n,int x); /*函数原型声明*/ printf("Input a number to be searched: "); scanf("%d", &x); x_at=sortfind(a,sizeof(a)/sizeof(int),x); /*函数调用*/ /* sizeof(a)/sizeof(int)求出数组a的元素个数 */ if (x_

7、at=-1) printf("Not exist!n"); else printf("%4d is at %4d.n", x,x_at); getch();int sortfind(int a,int n,int x) int left, mid, right; left=/*/ 0 /*/; right=n-1; if(x>=aleft && x<=aright) while( left<=right) mid= /*/ (left+right)/2 /*/; if( amid=x) return mid; else

8、if(/*/ x>amid /*/) left=mid+1; else right=mid-1; return -1;5 书面作业程序填空编写函数:C8_5c.C中的函数 void move(int a, int m, int n) 功能是将数组a(共有m个元素)中元素向右循环移位n次。请在一对/*/之间填写函数体。(教程P136例5.9实现向左循环移位n次,可参考)#include <stdio.h>#include <conio.h>#define M 9void main( ) void move(int a,int m,int n); int aM=1,

9、2, 3, 4, 5, 6, 7, 8, 9; int i, n; printf("nPlease input times: "); scanf("%d", &n); move(a,M,n); for(i=0;i<M;i+) printf("%4d", ai); getch();void move(int a,int m,int n)/*/ int i, j, temp; for(i=1;i<=n;i+) temp=am-1; for(j=m-1;j>0;j-) aj=aj-1; a0=temp; /*/6

10、书面作业程序填空编写函数:C8_6c.C中的函数 void newstr( char *str1,char *str2)功能是从串str1中取出ASCII值为偶数且下标为偶数的字符组成新串str2。请在一对/*/之间填写函数体。注:运行结果是 4h8nB#include<stdio.h>#include <conio.h>#include <string.h>void main()char s20="4ahg368hnjB",news20; void newstr( char *str1,char *str2); /*函数原型声明*/ne

11、wstr( s,news); /*函数调用*/printf("%sn",news); getch();解1:下标法存取字符(i为原串下标,j为新串下标),'0'控制循环结束void newstr( char *str1,char *str2)/*/ int i,j; for(i=j=0;str1i!='0'i+) if(str1i%2=0&&i%2=0) str2j+=str1i; str2j='0'/*/ 解2:下标法存取字符(i为原串下标,j为新串下标),串长控制循环结束,#include <stri

12、ng.h>void newstr( char *str1,char *str2)/*/ int i,j; for(i=j=0;i<strlen(str1);i+=2) if(str1i%2=0) str2j+=str1i; str2j='0'/*/解3:指针法存取字符(p为原串指针,q为新串指针),'0'控制循环结束void newstr( char *str1,char *str2)/*/ char *p=str1,*q=str2; for(; *p!='0'p+) if(*p%2=0&&(p-str1)%2=0)

13、*q+=*p; *q='0'/*/三、编程选做-简单变量作函数参数-7 程序填空编写函数:C8_7c.C中的函数float opr(float a,char op,float b)返回对a、b进行运算的结果,其中op为一个运算符(+、-、*或/)。请在一对/*/之间编写函数体。#include <stdio.h>#include <conio.h>void main()float x,y,opr(float,char,float); char op; scanf("%f%c%f", &x,&op,&y); if

14、 (op='+'|op='-'|op='*'|op='/'&&y!=0) printf("=%fn", opr(x,op,y); else printf("Input error.n"); getch();float opr(float a,char op,float b)/*/或:switch(op) case '+':return a+b; case '-': return a-b;case '*': return a*b;

15、case '/': return a/b; float z; switch(op) case '+':z=a+b; break; case '-':z=a-b; break; case '*':z=a*b; break; case '/':z=a/b; return z; /*/8 程序填空编写函数:C8_8c.C中的函数int counf(void)返回1999之间同构数的个数。请在一对/*/之间填写函数体。(同构数定义见教程P122习题4.25)注:n=7解1:#include <stdio.h>#

16、include <conio.h>int counf(void)/*/ int k,n=0; for(k=1;k<=9;k+) if(k*k%10=k)n+; for(k=10;k<=99;k+) if(k*k%100=k)n+; for(k=100;k<=999;k+) if(long)k*k%1000=k)n+; return n; /*/void main() printf("n = %dn", counf(); getch();解2:int counf(void)/*/ int k,n=0,r; long m; for(k=1;k<

17、;=999;k+) m=(long)k*k ; if(k<10) r=m%10 ; else if(k<100) r=m%100 ; else r=m%1000 ; if(k=r) n+; return n;/*/解3:int counf(void)/*/ int k,n=0; long m; for(k=1;k<=999;k+) m=(long)k*k ; if(k=m%10|k=m%100|k=m%1000 ) n+; return n;/*/9 程序填空:C8_9c.C中函数double myfun1(double a, double b)实现教程P111例4.34(用

18、二分法求方程x3+1.1x2+0.9x-1.4=0在区间a,b中的一个根,请在每对/*/之间填写内容。注:输入 -0.2,1 输出 A root of equation is: 0.67#include <stdio.h>#include <conio.h>#include<math.h>double myfun1(double a, double b) double c,fa,fb,fc; do c=(a+b)/2; fc=/*/ c*c*c+1.1*c*c+0.9*c-1.4 /*/; fa=a*a*a+1.1*a*a+0.9*a-1.4; if(fc*

19、fa>0) a=c; else b=/*/ c /*/; while(fabs(fc)>=1e-6); return c;void main()double a,b,fa,fb,x; do printf("Input a,b: n"); scanf("%lf,%lf",&a,&b); fa=a*a*a+1.1*a*a+0.9*a-1.4; fb=b*b*b+1.1*b*b+0.9*b-1.4; while(fa*fb>=0); x=myfun1(/*/ a,b /*/); /*函数调用*/ printf("A

20、root of equation is: %8.2lfn",x); getch();10 程序填空编写函数:C8_10c.C中的函数double myfun2(double x) 实现教程P122习题4.29的功能,即用牛顿迭代法求方程f(x)=X3+4X2-10=0在0,3区间内的根,迭代公式为xn=xn-1-f(xn-1)/f'(xn-1) )。请在一对/*/之间填写函数体。注:x=1.365230#include <stdio.h>#include <conio.h>#include <math.h>void main() doubl

21、e myfun2(double x); printf("The root is %lfn", myfun2(1.5); getch();double myfun2(double x)/*/ double x0,f,f1; do x0=x; f=(x0+4)*x0)*x0-10; f1=(3*x0+8)*x0; x=x0-f/f1; while(fabs(x-x0)>=1e-5); return x; /*/11 教程P248习题7.3(编写函数double efun(int n):用1 + 1/1!+ 1/2!+ 1/3!+ + 1/n! 求e的近似值。在主程序中输入

22、n的值,调用该函数求结果。)#include <stdio.h>void main()double efun(int n); /*函数原型声明*/ int n; printf("nPlease input a integer:"); scanf("%d",&n); if(n<0) printf("n Negative argument!n"); else printf("n e=%fn",efun(n); getch();double efun(int n) int i; double e

23、=1,p=1; for(i=1;i<=n;i+) p=p*i; e=e+1.0/p; return e;12 编写函数double myfun3(double a,double b,int n)实现教程P117例4.39的功能:用梯形法求函数f(x)=X2+4.8在a,b上的定积分,n为划分的小梯形个数。用主函数调用之。13 编写函数double myfun4(double a,double b,int n)实现教程P122习题4.30的功能:用矩形法求函数f(x)=X2+xsinx在a,b上的定积分,n为划分的小矩形个数。用主函数调用之。-指针变量作函数参数-14 程序填空编写函数:C

24、8_19c.C中的函数void max_min(int x,int n,int *pmax,int *pmin)求数组x(共n个数)中的最大数和最小数,结果分别存放在指针参数pmax、pmin所指单元;主函数定义变量amax和amin,并通过函数调用max_min(a,N,&amax,&amin),求得数组a的最大数amax和最小数amin。请在一对/*/之间填写函数体。void max_min(int x,int n,int *pmax,int *pmin)/*/ int i; *pmax=*pmin=x0; for( i=1;i<n; i+) if(xi>*pm

25、ax) *pmax=xi; else if (xi<*pmin) *pmin=xi;/*/void main()int a10=3,5,1,9,4,6,8,10,2,7; int amax,amin; max_min(a,10,&amax,&amin); printf("max=%dnmin=%dn",amax,amin); getch();- 一维数组作函数参数-15 程序填空编写函数:C8_15c.C中的函数void fun(int a, int m, int b, int n, int c)功能是将两个已按升序排好的数组a(共有m个数)和数组b(

26、共有n个数)合并,使合并后的数组c仍按升序排列。请在一对/*/之间填写函数体。(注:不允许合并后再排序,算法参见教程P142例5.13)#include <stdio.h>#include <conio.h>#define M 5#define N 8void fun(int a, int m, int b, int n, int c) /*/ int i=0,j=0,k=0; while (i<=m-1 && j<=n-1) if (ai<=bj) ck+=ai+; else ck+=bj+; if (i<=m-1) /* 数组

27、a还有元素 */ while (i<=m-1) ck+=ai+; else /* 数组b还有元素 */ while(j<=n-1) ck+=bj+; /*/void main() int arraM=5,10,15,27,46,arrbN=2,5,8,15,29,42,45,50; int arrcM+N,k; printf("narray arra is:"); for(k=0;k<M;k+) printf("%d ",arrak); printf("narray arrb is:"); for(k=0;k<

28、N;k+) printf("%d ",arrbk); fun(arra,M,arrb,N,arrc); printf("nresult arrc is:"); for(k=0;k<M+N;k+) printf("%d ",arrck); getch();16 编写函数void fun(int a, int m, int b, int n, int c)将已按升序排好的数组a(共有m个数)和已按降序排好数组b(共有n个数)合并,使合并后的数组c仍按升序排列,用主函数调用之。(注:不允许合并后再排序)-字符串作函数参数-17 编写函数void sortstr(char *str)将str串按ASCII码值升序重排,用主函数调用之。#include <stdio.h>#include <conio.h>#include <string.h>void main() char str81; int len,i,j,k;void sortstr(char *str); printf("Please input string: ");

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论