第五章作业
来源:互联网 发布:js prototype 继承 编辑:程序博客网 时间:2024/06/02 17:09
第五章 函数学习要求和实验
- 学习要求:
理解函数定义的意义。
掌握函数定义格式。
了解函数调用方式:值调用和引用调用。
掌握函数嵌套调用方法。
掌握数组作为函数的参数的使用。
理解全局变量、局部变量,动态变量、静态变量的概念,掌握不同变量的作用域与生命周期。
进一步积累程序调试经验。- 实验作业
1.调试分析课本每一个例题,有可能的话更改成2-3个方法的新程序;
2.编程实现课本每一个编程习题。
3. 编程实现输入两个4X5矩阵和5X3矩阵,定义函数并在主函数中调用计算它们的积。
4.编程计算S[n]=1!+21+3!+...n!。要求定义两个函数,一个计算n!,一个计算s[n],在后一个函数中调用前一个函数。然后在主程序中输入数n的值,然后调用定义函数输出结果。
5.编写一个函数,输入一个十六进制数,输出相应的十进制数。
本章学习的时间较长,书本翻阅了数次,所以大部分例题都比较熟悉了,所以只写下自己不够熟悉以及运用不够熟练的例题,那些太简单的就没有写下来了..
例题5.3
/***************************** 例5.3 *****************************/#include<iostream>using namespace std;double S(double);int main(){double a = S(5.0);cout<<"面积为"<<a<<endl;return 0;}double S(double r){double pi =3.14;double a =pi *r*r;return a;}
例题5.5
/***************************** 例5.5 *****************************/#include<iostream>using namespace std;int num(int i,int j){if(i>j) return 1;if(i ==j) return 0;return -1;}int main(){int k =2;int n = num(k,++k);cout<<n<<endl;return 0;}
例题5.9
/************************************ 例5.9 ***********************************/#include<iostream>using namespace std;void num(int &u, int &v);int main(){int a =3; int b =4;cout<<"a = "<<a<<"\t b = "<<b<<endl;num(a,b);cout<<"a = "<<a<<"\t b = "<<b<<endl;return 0;}void num(int &u,int &v){int temp =v;v=u;u=temp;}
例题5.13
/*********************************** 例5.13 ***********************************/#include<iostream>using namespace std;long f2(int);long f1(int p){int k;long r;k=p*p;r= f2(k);return r;}long f2(int q){long fact = 1;for(int i =1;i<=q;i++)fact *= i;return fact;}int main(){int i;long sum =0;for(i =2; i<=3;i++)sum += f1(i);cout<<"sum = "<<sum<<endl;return 0;}
例题5.15
/****************************** 例5.15 ******************************/#include<iostream>using namespace std;float Fuck(int n);int main(){int a;float f;cout<<"input an integer number:";cin>>a;f =Fuck(a);cout<<a<<"! ="<<f<<endl;return 0;}float Fuck(int n){float fact;if(n == 0)fact = 1;elsefact = n * Fuck(n-1);return fact;}
例题5.20
/*************************** 例5.20 ***************************/#include<iostream>using namespace std;void sort(int);void echoa();int a[5] ={6,3,9,8,2};int main(){echoa();sort(5);echoa();return 0;}void echoa(){for(int i =0;i<5;i++)cout<<a[i]<<'\t';cout<<endl;}void sort(int n){int i,j,min,temp;for(i =0;i<n-1;i++){min =i;for(j=i+1;j<n;j++)if(a[j]<a[min])min = j;temp = a[i];a[i]=a[min];a[min]=temp;}}
例题5.25
/**************************** 例5.25 ****************************///文件 file1.cpp#include<iostream>using namespace std;int a;int power(int n);int main(){int b =3,c,d,m;cout<<"enter a and m:"<<endl;cin>>a>>m;c = a * b;cout<<"a = "<<a<<"\t b = "<<b<<"\t c ="<<c<<endl;d = power(m);cout<<"a= "<<a<<"\t m = "<<m<<" \t d = "<<d<<endl;return 0;}//文件 file2.cppextern int a;int power(int n){int i,y =1;for(i =1;i<=n;i++)y *=a;return y;}
作业2
习题2
/***************************** 习题2 *****************************/#include<iostream>using namespace std;long intPower(int base,int exponent);int main(){int a,b;long int c;cout<<"请输入一个整数以及所要求的整数次幂的次数"<<endl;cin>>a>>b;c= intPower(a,b);cout<<"所求的结果为:"<<endl;cout<<a<<"的"<<b<<"次幂为:"<<c<<endl;return 0;}long intPower(int base,int exponent){long int d=base;for(int i=1;i<exponent;i++)d *= base;return d;}
习题3
/******************************* 习题3 *******************************/#include<iostream>using namespace std;int a;void power(int n){int i;for(i =2;i<n;++i)if(n%i==0)break;if(i ==n)cout<<a<<"是素数"<<endl;elsecout<<a<<"不是素数"<<endl;}int main(){char b;while(1){cout<<"请输入一个正整数"<<endl; cin>>a; power(a);cout<<"若继续请输入Y,否则输入N"<<endl;cin>>b;if(b=='y'||b=='Y'){continue; cout<<endl;cout<<endl;}elsebreak;}return 0;}
已经编译出程序来判断是否是素数,但是题目中的 "并加以测试" 是什么意思? 不懂.这个判断的过程就应该已经是测试的过程了吧?
习题4
/******************************* 习题4 *******************************/#include<iostream>#include<math.h>using namespace std;long power(int x){int i, t, n, k; i = 0, t = x, n = 0; while(t>0) { t = t/10; n++; } k = pow(10,n-1); while(x>0) { t = x%10; x = x/10; i += t*k; k = k/10; }return i;}int main(){int a,i;char b;while(1){cout<<"请输入一个正整数"<<endl; cin>>a; i=power(a);cout<<a<<"的逆转数为:"<<i<<endl;cout<<"若继续请输入Y,否则输入N"<<endl;cin>>b;if(b=='y'||b=='Y'){continue; cout<<endl;cout<<endl;}elsebreak;}return 0;}
习题5
/****************************** 习题5 ******************************/#include<iostream>using namespace std;void power(int n){switch(n/10) { case 6: { cout<<"该学生得D"<<endl<<endl; break; } case 7: { cout<<"该学生得C"<<endl<<endl; break; } case 8: { cout<<"该学生得B"<<endl<<endl; break; } case 9: { cout<<"该学生得A"<<endl<<endl; break; } default: cout<<"该学生Not Pass"<<endl<<endl; } }int main(){int a;char b;while(1){cout<<"请输入学生分数:"<<endl;cin>>a;power(a);cout<<"若继续请输入Y,否则输入N"<<endl;cin>>b;if(b=='y'||b=='Y'){continue; cout<<endl;cout<<endl;}elsebreak;}return 0;}
习题6
/************************* 习题6 *************************/#include<iostream>using namespace std;float power(int n[10]){int d=0,e;for(int i=0;i<10;i++){d+=n[i];}e=d/10;return e;}int main(){int a[10],k,aver;char b;while(1){cout<<"请分别输入10个学生的分数:"<<endl;for(k=0;k<10;k++){cout<<"a["<<k<<"]="<<endl;cin>>a[k];}aver=power(a);cout<<"平均成绩为:"<<aver<<endl;cout<<"若继续请输入Y,否则输入N"<<endl;cin>>b;if(b=='y'||b=='Y'){continue; cout<<endl;cout<<endl;}elsebreak;}return 0;}
习题7
我能说我最大公约数和最小公倍数不知道怎么写嘛?
习题8
/*********************** 习题8 ***********************/#include<iostream>using namespace std;void a1(); //九九乘法表全输出void a2(); //九九乘法表对角线以上void a3(); //九九乘法表对角线以下int main(){a1();a2();a3();return 0;}void a1(){int i,n,s;for(i=1;i<=9;i++){for(n=1;n<=9;n++){s=i*n;cout<<n<<"*"<<i<<"="<<s<<'\t';}cout<<endl;}cout<<" "<<endl;cout<<" "<<endl;}void a2(){int i,n,s;for(i=1;i<=9;i++){for(n=1;n<=i;n++){s=i*n;cout<<i<<"*"<<n<<"="<<s<<'\t';}cout<<endl;}cout<<" "<<endl;cout<<" "<<endl;}void a3(){int i,n,s;for(i=1;i<=9;i++){for(n=1;n<=i;n++){s=i*n;cout<<n<<"*"<<i<<"="<<s<<'\t';}cout<<endl;}cout<<" "<<endl;cout<<" "<<endl;}
第二个与第三个 同样用了下三角的输出方式,但是排列不一样..
习题9
不是很理解什么意思..
作业3.
/************************* 作业3 *************************/#include<iostream>#include<iomanip>using namespace std;int ji(int a[4][5], int b[5][3]);int main(){int a[4][5],b[5][3],c[4][3]={0},d,e;cout<<"请输入一个4行5列数组"<<endl;for(d=0;d<4;d++){for(e=0;e<5;e++){cin>>a[d][e];}}cout<<"请输入一个5行3列数组"<<endl;for(d=0;d<5;d++){for(e=0;e<3;e++){cin>>b[d][e];}}for(d = 0;d<4;d++) for(e = 0;e<3;e++) { c[d][e] = ji(a,b); } cout<<"A矩阵与B矩阵的积为:"<<endl; for(d = 0;d<4;d++) { for(e = 0;e<3;e++) { cout<<setw(4)<<c[d][e]; } cout<<endl; } return 0; } ji(int a[4][5], int b[5][3]){int d,e,f,g,c[4][3]={0};for(d=0;d<4;d++) for(e=0;e<3;e++) { for(f=0;f<5;f++) { g=a[d][e]*b[d][e]; c[d][e]=c[d][e]+g; } } return c[d][e];}
这个明显就是上一周的作业.. 明明一个主程序就能搞定..反而要分开调用..这不是坑爹嘛?麻烦死了..
作业4.
/*************************** 作业4 ***************************/#include<iostream>using namespace std;float Fuck(int n);int main(){int a,i;float f=0;cout<<"请输入一个整数:"<<endl;cin>>a;for(i=1;i<=a;i++)f +=Fuck(i);cout<<"S["<<a<<"]="<<f<<endl;return 0;}float Fuck(int n){float fact;if(n == 0)fact = 1;elsefact = n * Fuck(n-1);return fact;}
作业5
这个不会..
0 0
- 第五章:作业
- 第五章:作业
- 第五章作业。
- 第五章实验作业
- 第五章作业
- 第五章作业
- 第五章作业
- 第五章作业
- 第五章作业part1
- 第五章作业
- 第五章作业
- 第五章作业
- 第五章作业
- 第五章作业二
- 第五章作业一
- 第五章作业
- 第五章作业
- 第五章作业2
- 【最简分数】#13 A. Numbers
- 数组最大子序列
- UML之活动图
- ospf协议的基本配置
- 创建表空间
- 第五章作业
- 工作问题积累(十二)error C2668: “pow”: 对重载函数的调用不明确
- 基于DOS的多任务系统的实现_杭电_2014
- ListView 编程: Adapter 何方神圣?
- java正则表达式特殊字符
- Java/IO
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xb2 in position 0: ordinal not in range的解决办法
- mysql导入CSV文件及JAVA读取CSV文件和MYSQL批量插入和更新及ibatis常用SQL语句
- iOS的相对路径和绝对路径