Passing 2 & 3 dimension arrays by reference

Please can someone explain to me how to pass my arrays into the fuction an call it by reference im stuck :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>


using namespace std;

int n;
ifstream input;
ofstream output;
void validateeData();
void validateData();
int finalw();
int totalFinal();
int inputData();
string nonNumeric[3][9];
double semesterEvaluation[3][3][3];
double numericEvaluation[3][9];
const double spring_weight = 0.37;
const double summer_weight = 0.24;
const double fall_weight = 0.39;

int main()
{
    cout << "How many Employees? ";
    cin >> n;
    while(n < 1 || n > 3)
    {
        cout << "Invalid! Number must range from 1 - 3" << endl;
        cout << "How many Employees? ";
        cin >> n;
    }
    inputData();
    validateData();
    validateeData();
    finalw();
    totalFinal();
    return 0;
}

int inputData()
{
    //Reads the data from the input file into proper arrays
    input.open("Project5_A04484750_Input.txt");
    output.open("Project5_A04484750_Output.txt");
    cout << "Successfully opened File" << endl;
    if(!input)
    {
        cout << "ERROR! File Does Not Exist" << endl;
        return 0;
    }

}

void validateData()
{
    for(int number = 0; number < n; number++)
    {
        for(int i = 0; i < 7; i++)
        {
            getline(input, nonNumeric[number][i]);
        }
        output << " " << nonNumeric[number][0] << endl;
        output << " " << nonNumeric[number][1] << endl;
        output << endl;
        output << "                 Name of Employee: " << nonNumeric[number][2] << endl;
        output << "               Name of Supervisor: " << nonNumeric[number][3] << endl;
        output << "                     Employee ID#: " << nonNumeric[number][4] << endl;
        output << "                 Telephone Number: " << nonNumeric[number][5] << endl;
        output << "                          Address: " << nonNumeric[number][6] << endl;
    }
}
void validateeData()
{
    for(int number = 0; number < n; number++)//Number of employees
    {
        for(int j = 0; j < 3; j++) //Years
        {
            for(int k = 0; k < 3; k++)//Semesters
            {
                input >> semesterEvaluation[number][j][k];
                switch(j)
                {
                    case 0: //case for 2013
                        output << " Spring Semester Evaluation, " << fixed << setprecision(2) << 2013 + j << ": " << semesterEvaluation[number][j][k] << endl;
                        break;
                    case 1: //case for 2014
                        output << " Summer Semester Evaluation, " << fixed << setprecision(2) << 2013 + j << ": " << semesterEvaluation[number][j][k] << endl;
                        break;
                    case 2: //case for 2015
                        output << "   Fall Semester Evaluation, " << fixed << setprecision(2) << 2013 + j << ": " << semesterEvaluation[number][j][k] << endl;
                        break;
                }
            }
        }
    }
}

int finalw()
{
    for(int number = 0; number < n; number++)
    {
        for(int i = 0; i < 3; i++)
        {
            input >> numericEvaluation[number][5];
            input.ignore();
            //These are the located and assigned arrays from the input file
            //Locate each a line and calculate for spring, summer, and fall
            numericEvaluation[number][0] = (semesterEvaluation[number][0][0] * spring_weight) + (semesterEvaluation[number][0][1] * summer_weight) + (semesterEvaluation[number][0][1] * fall_weight);
            numericEvaluation[number][1] = (semesterEvaluation[number][1][2] * spring_weight) + (semesterEvaluation[number][1][1] * summer_weight) + (semesterEvaluation[number][1][2] * fall_weight);
            numericEvaluation[number][2] = (semesterEvaluation[number][2][0] * spring_weight) + (semesterEvaluation[number][2][1] * summer_weight) + (semesterEvaluation[number][2][2] * fall_weight);
            numericEvaluation[number][3] = numericEvaluation[number][0] + numericEvaluation[number][1] + numericEvaluation[number][2];
            //Reading the Salary from the input file
            output << "  Final Weighted Evaluation, 2013: " << numericEvaluation[number][0] << endl;
            output << "  Final Weighted Evaluation, 2014: " << numericEvaluation[number][1] << endl;
            output << "  Final Weighted Evaluation, 2015: " << numericEvaluation[number][2] << endl;
        }
    }
}

int totalFinal()
{
    for(int number = 0; number < n; number++)
    {
        for(int i = 0; i < 3; i++)
        {
            input >> numericEvaluation[number][5];
            input.ignore();
            //Calculating the average
            numericEvaluation[number][4] = numericEvaluation[number][3]/3;
            output << "  Total Final Weighted Evaluation: " << numericEvaluation[number][3] << endl;
            output << "Average Final Weighted Evaluation: " << numericEvaluation[number][4] << endl;
            input >> semesterEvaluation[number][0][1];
        }
    }
}
Why do you want to call them by reference?
all the dimensions except 1 have to be coded in.

void foo(int input[][3][3]) //ok, call with foo(semesterEvaluation).

I really dislike multi D constructs.
But if you *must* do this, consider a typedef for your fixed sized things and use that in the functions to make it cleaner and easier to fix if you change the program. If the dimensions actually vary you can do something else, but yours appear to all be fixed size.

Last edited on
keskiverto wrote:
Why do you want to call them by reference?

@beemo, think carefully on keskiverto's question. Your teacher likely wants you to refactor your code so that your methods take the arrays as parameters. You'll then likely be declaring the arrays in main() rather than in global scope and then passing them in.

Back to the question, though. If you were to refactor a method to take, for simplicity's sake, a 1D array, the general setup would look like this:

1
2
3
4
5
6
7
8
9
10
void foo(int arr[])
{
}

int main()
{
    int myarray[] = { 1, 2, 3, 4, 5};
    foo(myarray);
    return 0;
}


The question to help your understanding here is this: How is myarray being passed?
Topic archived. No new replies allowed.