首页 > 解决方案 > 试图将 3d 数组传递给函数 c++

问题描述

我正在尝试将 3d 数组传递给函数。我在函数中还有更多工作要做,但我似乎无法将数组从 main 函数获取到 void 函数。

这一定是我想念的简单的东西,因为我还在学习很多东西。只需快速解释一下我在主函数中使用 printMonthlySales 函数做错了什么就意味着很多。

此外,void 函数必须是浮点数组,主函数必须使用浮点数组。


#include <iostream>;
#include <iomanip>;

using namespace std;

const int NUM_DEPTS = 2;
const int NUM_STORES = 2;
const int NUM_MONTHS = 12;

void printMonthlySales(int a, float array[2][12][2]);

int main() {
    char sentinel = 'y';
    int userInput = 0;
    float storeMonthlySales[NUM_STORES][NUM_MONTHS][NUM_DEPTS] = { 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2,
                                                                2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2,
                                                                3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0, 4.1, 4.2,
                                                                2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0, 3.1, 3.2 };
    while (sentinel == 'y' || sentinel == 'Y')
    {
        cout << "1. January" << endl;
        cout << "2. February" << endl;
        cout << "3. March" << endl;
        cout << "4. April" << endl;
        cout << "5. May" << endl;
        cout << "6. June" << endl;
        cout << "7. July" << endl;
        cout << "8. August" << endl;
        cout << "9. September" << endl;
        cout << "10.October" << endl;
        cout << "11.November" << endl;
        cout << "12.December" << endl;
        cout << "Please select a month using 1-12: ";
        cin >> userInput;
        printMonthlySales(userInput, storeMonthlySales[NUM_STORES][NUM_MONTHS][NUM_DEPTS]);
    }
    

    return 0;
}

void printMonthlySales(int a, float array[2][12][2])
{
    int b = a;
    const int m = 12;
    float sumStoreOne = 0;
    float sumStoreTwo = 0;
    float sumDepartmentOne = 0;
    float sumDepartmentTwo = 0;
    float totalSales = 0;
    for (int i = 0; i < 2 ; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            cout << array[i][b][j] << endl;

        }
    }
}

标签: c++arrays

解决方案


推荐阅读