0
#include <iostream>

using namespace std;

//Constants
const int EMPLOYEE_NUM = 4, MONTH_NUM = 5;
const double MONTH_QUOTA[MONTH_NUM] = {1, 1.05, 1.1, 1.15, 1.2};


int main ()
{

    //Define Arrays
    string names[EMPLOYEE_NUM][2], count[EMPLOYEE_NUM] = {"first", "second", "third", "fourth"};
    double salesGoal[EMPLOYEE_NUM][MONTH_NUM];

    //Initialize Loop Variables
    int employee, month;


    //Get Names of Salesmen
    for (int employee = 0; employee < EMPLOYEE_NUM; employee++)
    {
        cout << "Enter the first name of the " << count[employee] << " salesman: \t";
        cin >> names[employee][0];
        cout << "Enter the last name of the " << count[employee] << " salesman: \t";
        cin >> names[employee][1];
    }

    //Get Initial Sales Goal For Each Salesman
    for (int employee = 0; employee < EMPLOYEE_NUM; employee++)
    {
        cout << "Enter the initial sales goal for " << names[employee][0] << ": \t$";
        cin >> salesGoal[employee][0];
        while (salesGoal[employee][0] < 0)
            {
                cout<< "Number cannot be negative. Enter the initial sales goal for " << names[employee][0] << ": \t$";
                cin >> salesGoal[employee][0];
            }
    }

    //Make Sales Goal Array for All 5 Months
    for (int employee = 0; employee < EMPLOYEE_NUM; employee++)
        {
            for (int month = 1; month < MONTH_NUM; month++)
            {
                salesGoal[employee][month] = salesGoal[employee][month-1]*MONTH_QUOTA[month];
            }
        }

    cout << endl;


    //Display Names And Projected Sales Table
    cout << "Name\t\t\t"<< "Sales Goal" << endl;
    cout << "*****************************************************" <<endl;

    for (int employee = 0; employee < EMPLOYEE_NUM; employee++)    
        {  
            cout << names[employee][1] << ", " << names[employee][0] << endl;

            for (int month = 0; month < MONTH_NUM; month++)
            {
                cout <<  "\t" << "Month " << month + 1 << ":\t$" << salesGoal[employee][month] <<endl;
            }
            cout << endl;
        }

    return 0;
}

I have made this for a class, however I want to make this part of the code into a function

    for (int employee = 0; employee < EMPLOYEE_NUM; employee++)
        {
            for (int month = 1; month < MONTH_NUM; month++)
            {
                salesGoal[employee][month] = salesGoal[employee][month-1]*MONTH_QUOTA[month];
            }
        }

after looking through stacks i found an explanation along the lines of this


using namespace std;

//Contants
const int EMPLOYEE_NUM = 4, MONTH_NUM = 5;
const double MONTH_QUOTA[MONTH_NUM] = {1, 1.05, 1.1, 1.15, 1.2};

    //Make Array with All 5 Months
    void projectedSales(double (*x)[EMPLOYEE_NUM][MONTH_NUM], double MONTH_QUOTA[])
    {
        for (int employee = 0; employee < EMPLOYEE_NUM; employee++)
        {
            for (int month = 1; month < MONTH_NUM; month++)
            {
                x[employee][month] = x[employee][month-1]*MONTH_QUOTA[month];
            }
        }
    }


int main ()
{

    //Define Arrays
    string names[EMPLOYEE_NUM][2], count[EMPLOYEE_NUM] = {"first", "second", "third", "fourth"};
    double salesGoal[EMPLOYEE_NUM][MONTH_NUM];
    double (*p_salesGoal)[MONTH_NUM];
    p_salesGoal = salesGoal

    //Initialize Loop Variables
    int employee, month;


    //Get Names of Salesmen
    for (int employee = 0; employee < EMPLOYEE_NUM; employee++)
    {
        cout << "Enter the first name of the " << count[employee] << " salesman: \t";
        cin >> names[employee][0];
        cout << "Enter the last name of the " << count[employee] << " salesman: \t";
        cin >> names[employee][1];
    }

    //Get Initial Sales Goal For Each Salesman
    for (int employee = 0; employee < EMPLOYEE_NUM; employee++)
    {
        cout << "Enter the initial sales goal for " << names[employee][0] << ": \t$";
        cin >> salesGoal[employee][0];
        while (salesGoal[employee][0] < 0)
            {
                cout<< "Number cannot be negative. Enter the initial sales goal for " << names[employee][0] << ": \t$";
                cin >> salesGoal[employee][0];
            }
    }

    cout << endl;

    projectedSales(p_salesGoal, MONTH_QUOTA);


    //Display Names And Projected Sales Table
    cout << "Name\t\t\t"<< "Sales Goal" << endl;
    cout << "*****************************************************" <<endl;

    for (int employee = 0; employee < EMPLOYEE_NUM; employee++)    
        {  
            cout << names[employee][1] << ", " << names[employee][0] << endl;

            for (int month = 0; month < MONTH_NUM; month++)
            {
                cout <<  "\t" << "Month " << month + 1 << ":\t$" << salesGoal[employee][month] <<endl;
            }
            cout << endl;
        }

    return 0;
}

but receive the errors

main.cpp:20:58: error: invalid operands of types ‘double [5]’ and ‘double’ to binary ‘operator*’
                 x[employee][month] = x[employee][month-1]*MONTH_QUOTA[month];
                                      ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:36:5: error: expected ‘;’ before ‘int’
     int employee, month;
     ^~~
main.cpp:57:44: error: cannot convert ‘double (*)[5]’ to ‘double (*)[4][5]’ for argument ‘1’ to ‘void projectedSales(double (*)[4][5], double*)’
     projectedSales(p_salesGoal, MONTH_QUOTA);

what am i doing wrong? how do i pass the array i've made known as salesGoal to my function so it can be computed and modified with my constant global array MONTH_QUOTA?

Mat
  • 188,820
  • 38
  • 367
  • 383
  • 1
    Please use a `std::array` or `std::vector` rather than a C-style array. – Jesper Juhl Oct 19 '19 at 20:52
  • Should not you be using a double pointer: point to another pointer which should point to start of each row . – macroland Oct 19 '19 at 20:57
  • You passed “the array itself” (*i.e.*, a pointer to its first (subarray) element) rather than a pointer to the array. But why are you doing that rather than just declaring an “array parameter” anyway? – Davis Herring Oct 20 '19 at 01:42
  • @macroland: That’s one approach, but it’s less efficient when the bounds (except perhaps the first) are constant expressions. – Davis Herring Oct 20 '19 at 01:48
  • @macroland no idea what that means honestly. – Jacob Hochman Oct 20 '19 at 01:59
  • @davisherring i used this stacks sub https://stackoverflow.com/questions/5329107/passing-a-pointer-representing-a-2d-array-to-a-function-in-c to try and fix up the original code. but i guess i did it wrong? i have no clue what i'm doing honestly lol – Jacob Hochman Oct 20 '19 at 02:00
  • @JacobHochman: The answer there is correct, but not really relevant to what you want. Just declare the parameter like the original array and it works. (This parallelism is a feature of C/C++, although what it actually *means* confuses newcomers.) – Davis Herring Oct 20 '19 at 02:58
  • Please leave out the irrelevant details and provide a [mre]. – L. F. Oct 20 '19 at 05:59

0 Answers0