编程题复习资料.docx

  1. 1、本文档共11页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
一、函数的定义与调用 (1)分别用冒泡法(升序) 、选择法(降序) 、擂台法(升序)编写三个对一维数组进 行排序的函数, 函数名为 sort1() 、sort2() 、sort3() 。再定义一个输出数组元素值的函数 print() 。 在主函数中定义一维整型数组 a[N] (N=10),用键盘输入 10 个整数给 a[N] 数组。依次调用 sort1()、print() 、sort2() 、print() 、sort3() 、print() ,进行升序、降序、升序的操作,并输出每 次排序后的结果。 输入十个实验数据: 10,25,90,80,70,35,65,40,55, 5 (2)编写一个函数 px(float x,int n) 用递归的方法求下列级数前 n 项的和 s。 s x 2 4 5 6 ( 1) 3 n 1 xn x x x x x 在主函数中定义变量 x 与 n,用键盘输入 x 与 n 的值, 调用 px() 函数计算并返回级数前 n 项和 s。最后输出 s 的值。 输入实验数据: x=1.2 n=10 解答参考 (1) #include <iostream.h> #include <iomanip.h> #define N 10 void print(int a[]) { int i; for(i=0;i<N;i++) cout<<setw(5)<<a[i]; cout<<endl; } void sort1( int a[] ) { int i,j,temp; for(i=0;i<N-1;i++) for(j=0;j<=N-1-i;j++) if (a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } void sort2( int a[] ) { int i,j,temp; for(i=0;i<N-1;i++) for(j=i+1; j<N;j++) if (a[i]<a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } void sort3( int a[] ) { int i,j,k,temp; for(i=0;i<N-1;i++) { k=i; for(j=i+1; j<N;j++) if (a[k]>a[j]) k=j; if (k>i) { temp=a[i]; a[i]=a[k]; a[k]=temp;} } } void main(void) { int i; int b[10]; cout<<" 请输入 10 个数:"<<endl; for(i=0;i<10;i++) cin>>b[i]; sort1(b); cout<<" 输出排好序的 10 个数:"<<endl; print(b); sort2(b); cout<<" 输出排好序的 10 个数:"<<endl; print(b); sort3(b); cout<<" 输出排好序的 10 个数:"<<endl; print(b); } 程序运行结果 : 请输入 10 个数: 2 6 9 11 5 61 25 32 22 14 19 输出排好序的 10 个数 : 2 5 6 9 10 11 14 22 25 32 输出排好序的 10 个数 : 32 25 22 14 11 10 9 6 5 2 输出排好序的 10 个数 : 2 5 6 9 10 11 14 22 25 32 (2) ①递归公式为: x ; n=1 px(n)= px(n-1)+(-1) n-1xn ; n>1 ②递归结束条件: n=1 ③递归约束条件: n>1 # include <iostream.h> # include <math.h> void main(void) { float x; int n; float px(float,int); cout<<"please input x,n:"; cin>>x>>n; cout<<"px="<<px(x,n)<<endl; } float px(float x,int n) { float p; if (n==1) p=x; else p=px(x,n-1)-pow(-1,n)*pow(x,n); return p; } 程序运行结果 : please input x,n:2 4 px=-10 二、类与对象的定义与使用 (1)定义一个复数类 Complex,复数的实部 Real 与虚部 Image 定义为私有数据成员。 用复数类定义复数对象 c1、c2、c3,用默认构造函数将 c1 初始化为 c1=20+40i ,将 c2 初 始化为 c2=0+0i ,用拷贝构造函数将 c3 初始化为 c3=20+40i 。用公有成员函数

文档评论(0)

5566www + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:6122115144000002

1亿VIP精品文档

相关文档