Functions

I'm having trouble figuring out function parameters for my program. This is my code so far. How can I fix my program so that it will work properly.



Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Provide a the following functions:
getData: Read values into the array
displayData: Print out the highs and lows for each month
averageHigh: Calculates and returns the average high temperature


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


using namespace std;

int months [12][2];
const int numMonths = 12;
void getData(float[][]);
float averageHigh(float [] [2],int );
void displayData(float[][2]);



int main()
{


   getData();
    averageHigh(months,numMonths)




}

void getData()
{
    int month;
    for(int i; i < 13 && i!=0; i++)
    {
    cout << "Enter the high temperature for " << i << endl;
    cin >> month;

    switch (i)
    {
    case 1:
        cout << "January" << endl;
          break;
    case 2:
        cout << "February" << endl;
          break;
    case 3:
        cout << "March" << endl;
          break;
    case 4:
        cout << "April" << endl;
          break;
    case 5:
        cout << "May" << endl;
          break;
    case 6:
        cout << "June" << endl;
          break;
    case 7:
        cout << "July" << endl;
          break;
    case 8:
        cout << "August" << endl;
          break;
    case 9:
        cout << "September" << endl;
          break;
    case 10:
        cout << "October" << endl;
        break;
    case 11:
        cout << "November" << endl;
        break;
    case 12:
        cout << "December" << endl;
          break;

    }

    }
}

float averageHigh(float[][],int m)
{
   float sum = 0;


    for (int i = 0; i <m; i++)
        sum +=A[i][0];
    return (sum/m);
}



float displayData()float [] [2])
{
	cout<<"The average high temperature is: ";

}
First of all the prototypes needs to match the implementation.

See:

http://www.cplusplus.com/doc/tutorial/arrays/

for how to pass a multidimensional array. float[][2] within the prototype is correct, but you need a name in order to access the passed paramter.

Accessing the passed arrary looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void getData(float[][]);
...


int main()
{
int months [12][2];

   getData(months);
...
}

void getData(int months [][2])
{
    for(int i = 1; i < 13; i++)
    {
...
    cout << "Enter the high temperature for " << i << endl;
    cin >> months[i-1][1]; // Note: [i-1] because an array starts with 0 / [1] store the high temperature in the second field
...
Topic archived. No new replies allowed.