| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1638 人关注过本帖
标题:C++数组传参问题
只看楼主 加入收藏
jioper
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2016-10-21
结帖率:70%
收藏
已结贴  问题点数:20 回复次数:1 
C++数组传参问题
输入3个整数,并找出其中的最大值


要求1:在main函数中使用for循环与cin读入3个整数,存入名为arr的整型数组中,并将该数组作为形参传递给get_max子函数。

要求2:get_max子函数的功能是找出数组中的最大值,并返回给main函数。

要求3:get_max函数调用完毕后,在main函数中使用cout打印出最大值。
我的代码:
#include<iostream>
using namespace std;
int get_max(int &a,int &b,int &c)
{
int max; max=a; if(max<b) max=b; if(max<c) max=c; return max;
}
int main()
{
    int arr[3];
    for(int  i=0;i<3;i++)
    {
        cin>>arr[i];
    }
    int amx=get_max(arr[0],arr[1],arr[2]);
    cout<<amx;
    return 0;
}
我是不是只是传了三个变量,没有把数组作为形参传递啊...要改的话怎么改呢?
搜索更多相关主题的帖子: include return 最大值 
2017-04-27 10:54
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
程序代码:
#include <iostream>
using namespace std;

int get_max( const int arr[] )
{
    int max = arr[0];
    if( max < arr[1] )
        max = arr[1];
    if( max < arr[2] )
        max = arr[2];
    return max;
}
int main( void )
{
    int arr[3];
    for( size_t i=0; i!=3; ++i )
        cin >> arr[i];
    cout << get_max(arr) << endl;
    return 0;
}
2017-04-27 11:17
快速回复:C++数组传参问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014311 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved