收藏 分销(赏)

C编程题B.doc

上传人:人****来 文档编号:3068808 上传时间:2024-06-14 格式:DOC 页数:6 大小:22.50KB 下载积分:6 金币
下载 相关 举报
C编程题B.doc_第1页
第1页 / 共6页
C编程题B.doc_第2页
第2页 / 共6页


点击查看更多>>
资源描述
四.编程题 13. 编写一个自定义函数:int f( long a[], int n, long x) ,f( )的功能是:对给定的含有n个元素的一维数组a及某个指定数x,查找数组a中是否存在元素值等于x的元素,若存在,则函数值返回找到的下标最大的那个元素的下标;若不存在,则函数值返回-1。 #include <iostream> using namespace std; int f(long a[], int n, long x); void main() { long x=7; long a[5]={3,5,2,7,9}; cout<<f(a,5,x)<<endl; } 14. 编写一个自定义函数:int f( long a[], int n, long x) ,f( )的功能是:对给定的一维数组a及某个指定数x,找出数组a中元素值等于x的元素个数,以及最后一个值为x的元素所在位置下标。在函数中输出这两个结果,并将找到的元素个数作为函数值返回。 #include <iostream> using namespace std; int f(long a[], int n, long x); void main() { long x=7; long a[5]={3,7,2,7,9}; cout<<f(a,5,x)<<endl; } 15. 编写一个自定义函数:int f( long a[], int n, long x) ,f( )的功能是:对给定的含有n个元素的一维数组a及某个指定数x,查找数组a中是否存在元素值等于x的元素,若存在,则函数值返回找到的下标最小的那个元素的下标;若不存在,则函数值返回-1。 #include <iostream> using namespace std; int f(long a[], int n, long x); void main() { long x=8; long a[5]={3,7,2,7,9}; cout<<f(a,5,x)<<endl; } 25. 本题在主函数中给定数组a及其元素个数n,通过调用自定义函数f,使数组a的元素反序存放:即第1个位置的元素与倒数第1个位置的元素交换,第2个位置的元素与倒数第2个位置的元素交换,…,如此类推。交换的元素组数与元素总数n有关。要求n在主函数中给定。根据主函数代码和函数f的声明,完成自定义函数设计。 #include <iostream> using namespace std; void f(int x[], int n); void main() { int i, a[10]; for (i=0;i<=10-1;i++) cin>>a[i]; f (a, 10); for (i=0;i<=10-1;i++) cout<<a[i]<<','; } 29. 本题在主函数中给定数组a及其元素个数n,通过调用自定义函数f得到给定数组中的所有元素之和。请根据函数f的声明语句及其功能完成f函数代码设计。 #include <iostream> using namespace std; #define N 10 int f( int x[ ], int n); void main() {int i,a[N]; for (i=0;i<=N-1;i++) cin>>a[i]; cout<<f(a,N); } 30. 本题在主函数中给定数组a及其元素个数n,通过调用自定义函数f求给定一维数组中所有下标为偶数的元素之和。请根据函数f的声明语句及其功能完成函数代码设计。 #include <iostream> using namespace std; #define N 10 int f( int x[ ], int n); void main() {int i,a[N]; for (i=0;i<=N-1;i++) cin>>a[i]; cout<<f(a,N); } 31. 本题在主函数中给定数组a及其元素个数n,通过调用自定义函数f求给定一维数组中所有下标为奇数的元素之和。请根据函数f的声明语句及其功能完成函数代码设计。 #include <iostream> using namespace std; #define N 10 int f( int x[ ], int n); void main() {int i,a[N]; for (i=0;i<=N-1;i++) cin>>a[i]; cout<<f(a,N); } 32. 本题在主函数中给定方阵数组a及其行数(列数)M,通过调用自定义函数f 求给定二维数组中所有上三角元素(不含主对角线)之和,例如:数组a的元素如下: 1 2 3 4 5 6 7 8 9 其上三角的元素和为11。请根据函数f的声明语句及其功能完成f函数代码设计。 #include <iostream> using namespace std; #define M 3 int f ( int x[ ][M] ); void main() { int i, j, a[M][M]; for ( i=0; i<=M-1; i++ ) for ( j=0; j<=M-1; j++ ) cin>>a[i][j]; cout<<f(a); } 33. 本题在主函数中给定方阵数组a及其行数(列数)M,通过调用自定义函数f求给定二维数组中所有下三角元素(不含主对角线)之和。请根据函数f的声明语句及其功能完成f函数代码设计。 #include <iostream> using namespace std; #define M 3 int f ( int x[ ][M]); void main() {int i, j,a[M][M]; for (i=0;i<=M-1;i++) for (j=0;j<=M-1;j++) cin>>a[i][j]; cout<<f(a); } 34. 本题在主函数中给定方阵数组a及其行数(列数)M,通过调用自定义函数f求给定二维数组中所有主对角线上元素之和。请根据函数f的声明语句及其功能完成f函数代码设计。 #include <iostream> using namespace std; #define M 3 int f ( int x[ ][M]); void main() {int i, j,a[M][M]; for (i=0;i<=M-1;i++) for (j=0;j<=M-1;j++) cin>>a[i][j]; cout<<f(a); } 38. 编写自定义函数f()的功能是找出一个数组a中第3大的元素,将该元素的值作为函数值返回,数组a及其元素个数m由主函数给定。根据自定义函数f的声明语句和功能编写f函数的代码。 #include <iostream> using namespace std; int f(int a[],int m); main() { int a[10]={21,33,22,23,45,46,32,21,3,4}; cout<<"The result is "<<f(a,10)<<endl; } 39. 编写函数f(),求具有n个元素的一维数组a中最大元素的下标(如果有多个并列最大的元素,则以最后一个为准)。其中,数组a及其元素个数n由主函数给定。 #include <iostream> using namespace std; int f(int a[], int n); main() { int a[10]={21,33,22,23,45,46,32,21,3,4}; cout<<"The result is "<<f(a,10)<<endl; } 40. 编写函数f(),求具有n个元素的一维数组a中最大偶数元素的下标,函数值返回该元素下标;若无偶元素,函数值返回-1。如果有多个并列最大的偶数元素,则以最后一个为准。其中,数组a及其元素个数n由主函数给定。 #include <iostream> using namespace std; int f(int a[], int n); main() { int a[10]={21,33,22,23,45,46,32,21,3,4}; cout<<"The result is "<<f(a,10)<<endl; } 41. 编写函数f(),求具有n个元素的一维数组a中最大奇数元素的下标,函数值返回该元素下标;若无奇元素,函数值返回-1。如果有多个并列最大的奇数元素,则以最后一个为准。其中,数组a及其元素个数n由主函数给定。 #include <iostream> using namespace std; int f(int a[], int n); main() { int a[10]={21,33,22,23,45,46,32,21,3,4}; cout<<"The result is "<<f(a,10)<<endl; } 42. 编写函数f(),求具有M行N列的二维数组中最大元数的值。其中,数组a及其行数m由主函数调用语句的实参给定。 #include <iostream> using namespace std; #define M 3 #define N 4 int f(int a[][N], int m); main() { int a[M][N]; for(int i=0;i<M;i++) for(int j=0;j<N;j++) cin>>a[i][j]; cout<<"The result is "<<f(a,M)<<endl; } 43. 编写函数f(),计算具有n个元素的一维数组a中每组相邻两个数的差,找出其中的最大差作为函数值返回。 #include <iostream> using namespace std; #define M 5 int f(int a[], int n); main() { int i, a[M]; for(i=0;i<M;i++) cin>>a[i]; cout<<"The result is "<<f(a,M)<<endl; } 1. 编写函数int f(int x[ ], int n),求出20个数中的最大数,并在以下main函数中进行测试。 #include<iostream.h> void main( ) { int a[ ]={1,4,6,7,3,9,10,30,59,32,48,72,87,13,27,45,64,8,2,6}; cout<<"a中最大值为:"<<f(a,20)<<endl; }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服