Passing Multi-Array Data to a Function

I'm trying to pass data from a 3d array to an averaging function. Right now I'm getting an error where I'm trying to call the function "no matching function for call to average_temp". I declared the rows(days) and months(columns) of each dimension in the function prototype, omitting the extremes because I'm not using that data in this function. The examples I have seen call the function, func(array, SIZE). I'm having formatting issues, any help would be appreciated.


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
#include <iostream>
#include <string>

using namespace std;

const int days = 3;
const int months = 3;
const int extremes = 2;

double average_temp(int[days][months], int);

int main()
{
    
    int almanac[days][months][extremes] =
    {
        
        {{67,50}, {71,53}, {75,55}}, // day 1
        {{67,50}, {72,53}, {75,55}}, // day 2
        {{67,50}, {72,53}, {75,55}}, // day 3
        
    };
    
    string userchoice;
    
    cout << " I have a bunch of weather data. There are a few ways this data can be displayed. " << endl;
    cout << " Enter 1 - the high temperatures for all the data. " << endl;
    cout << " Enter 2 - the low temperatures for all the data. " << endl;
    cout << " Enter 3 - the average temp for June, July or August. " << endl;
    cout << " Enter 4 - the highest temp for June, July or August. " << endl;
    cout << " Enter 5 - the lowest temp for June, July or August. " << endl;
    cin >> userchoice;
    
    if(userchoice == "3")
    {
        cout << "\nThe average tempearature was" << average_temp(almanac, [days][months]) << endl; // This is where error is showing. 
    }
    return 0;
}
    
    double average_temp(int A[3][3], int num_elements)
    {
    
    int total=0;
    double avg;
    
    for (int i = 0; i < num_elements; i++)
    {
        
        for (int j = 0; j < num_elements; j++)
        {
            total = total + A[i][j];
        }
    }
    avg = (double) total / (double) num_elements;
    return avg;
}

You're not using your parameters correctly. In your function you list the array first, followed by num_elements. In your call where you say there's an error you need to follow the parameters (array, num_elements).
I tried changing my parameters to match the function, average_temp(almanac, num_elements), but it still does not recognize average_temp? I've been looking at examples, but none of them seem to call more than one dimension. Do I need to initialize a variable that is equal to the dimensions the array and call that as the "num_elements"?
Your function calls for a two dimensional array as its first parameter. You're passing a three dimensional array as a value on line 36 of your code.

As for your second parameter, to my knowledge passing something like [a][b] is not legal either. Something more along the lines of days * months would be appropriate.
Topic archived. No new replies allowed.