zoukankan      html  css  js  c++  java
  • 数组和函数

    1. 数组作为参数

    需要注意:

    • 修改数组的函数,实际上传入的是一个指针,这导致原数组的长度信息丢失,因此还需要传入数组长度数据;
    #include <iostream>
    
    const int ArSize = 8;
    
    int sum_arr(int arr[], int n);
    
    int main()
    {
        int p{},m{},q{};
        int cookies[ArSize] = {1, 2, 4, 8, 16, 32, 64, 128};
        std::cout << cookies << " = array address";
    
        int sum = sum_arr(cookies, ArSize); // 8个元素求和
        std::cout << "Total cookies eaten :" << sum << std::endl;
    
        sum = sum_arr(cookies, 4); // 前4个元素求和
        std::cout << "Total cookies eaten " << sum << " cookies.
    ";
    
        sum = sum_arr(cookies + 4, 4); // 后4个元素求和
        std::cout << "Last four eaters eaten " << sum << " cookies.
    ";
    }
    
    int sum_arr(int arr[], int n)
    {
        int total = 0;
        std::cout << arr << " = arr, ";
    
        std::cout << sizeof arr << " = sizeof arr
    ";
        for (int i = 0; i < n; i++) {
            total += arr[i];
        }
        return total;
    }
    

    实际传入的是数组地址,这样有两个好处:

    • 节省空间
    • 降低数据破坏风险

    2.数组填充

    由于数组作为参数,实际上是传址,为了防止函数无意中修改数组内容,使用const进行保护。

    #include <iostream>
    
    const int Max = 5;
    int fill_array(double ar[], int limit);
    void show_array(const double ar[], int n);
    void revalue(double r, double ar[], int n);
    
    int main() {
        using namespace std;
        double properties[Max];
    
        int size = fill_array(properties, Max);
        show_array(properties, size);
    
        if (size > 0) {  //判断输入的乘数是否有效,无效则重新输入
            cout << "请输入:";
            double factor;
            while (!(cin >> factor)) {
                cin.clear();
                while (cin.get() != '
    ')
                    continue;
                cout << "Bad input; Please input a new number: 
    ";
            }
            revalue(factor, properties, size);
            show_array(properties, size);
        }
        cout << "完成
    ";
        cin.get();
        cin.get();
        return 0;
    }
    
    int fill_array(double ar[], int limit) {
        using namespace std;
        double temp;
        int i;
    
        for (i = 0; i < limit; i++) {
            cout << "Enter value #" << (i + 1) << ": ";
            cin >> temp;
    
            if (!cin) { //如果输入无效字符
                cin.clear();
                while (cin.get() != '
    ')
                    continue;
                cout << "Bad input; Please input a new number: 
    ";
                break;
            } else if (temp < 0) { //如果输入负数
                break;
            }
            ar[i] = temp; //类型正确,给数组赋值
        }
        return i;
    }
    
    void show_array(const double ar[], int n) //const可以防止程序对传入参数进行修改
    {
        using namespace std;
        for (int i = 0; i < n; i++) {
            cout << "Property #" << (i + 1) << ": $";
            cout << ar[i] << endl;
        }
    }
    
    void revalue(double r, double ar[], int n)
    {
        for (int i = 0; i < n; i++) {
            ar[i] *= r;
        }
    }
    
  • 相关阅读:
    MFC project for a non-Unicode character set is deprecated
    关于Visual Studio 2013 编译 multi-byte character set MFC程序出现 MSB8031 错误的解决办法
    字符串比较自实现
    各种语言里获取当前模块的方法:ABAP,ABSL,C,nodejs
    SAP CRM product attachment的document template功能
    ABAP, UI5和webpack的处理入口
    ABAP, Maven, CF App和Webpack的build
    json格式的字符串序列化和反序列化的一些高级用法
    SAP ABAP Netweaver容器化, 不可能完成的任务吗?
    UI Component in CRM WebUI and Hybris
  • 原文地址:https://www.cnblogs.com/geoffreyone/p/9879979.html
Copyright © 2011-2022 走看看