实验目的
- 掌握函数定义和调用
- 掌握函数调用时实参与形参的对应关系以及“值传递”的方式
- 掌握函数的嵌套调用和简单递归调用
- 掌握数组作为函数参数的用法
实验内容
1、编写函数,实现统计和输出一个正整数中各位数字中零的个数,及各位数字中最大者。
例如:1080其零的个数是2,各位数字中最大者是8。主函数负责正整数的输入。
2、编写递归函数实现n!。
3、自定义函数实现对n个数进行选择法排序,主函数调用。
要求:共自定义3个函数,分别对数组进行输入,排序和输出。
4、自定义函数实现对一个有序数组进行插入操作,保证插入后的数组依然有序。主函数调用。
要求:1)共自定义2个函数,分别对数组进行插入和输出。
2)数组、待插数据在主函数中定义和赋值。
1 #include<iostream> using namespace std; int shuling(int n,int *digits,int*max); int main(){ int n,lingshu,max; cin>>n; shuling(n,&lingshu,&max); cout<<n<<" "<<"has"<<" "<<lingshu<<" "<<"digits 0,and the max is "<<max<<"."<<endl; } int shuling(int n,int *digits,int*max){ int p,q; *digits=0; *max=0; while(n!=0){ if(n%10==0){ (*digits)++; } p=n%10; if(p>*max) *max=p; n=n/10; } } | |
2 #include<iostream> using namespace std; int di(int n); int main(){ int n,c; cin>>n; c=di(n); cout<<c<<endl;
} int di(int n){ int result=0; if(n==1) return 1; else result=n*di(n-1); return result; }
} |