array to function

hello fellow tech people!

Im having some trouble with passing an array to a function to do some operation(s).
My code runs and does what is asked to do. But, the little detail is that I need to
get the average through a function. I've done thorough reseach, but i still seem to
grasp the essence of it.

How can I do it so that when I run my code, and input all the 12 values. That thse are passed into a function that would basically all the 12 values and divide by 12. help?




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
 #include <iostream>
#include <iomanip>
using namespace std;

double yearlyRainAverage();

int main() {

    const int months = 12;
    double inchesofrain[months];
    double yearlyAverage;
    double sum = 0

    for (int count = 0; count < months; count++)
    {
        cout<<"Enter the rainfall (in inches) for month #"<< count + 1<<":  ";
        cin>>inchesofrain[count];

        sum += inchesofrain[count];


        if(inchesofrain[count] < 0){

            cout <<"Rainfall must be 0 or more.\n";
            cout<<"please re-enter: "<<endl;
            cout<<"Enter the rainfall (in inches) for month #"<< count + 1<<":  ";
            cin>>inchesofrain[count];

        }

    }
    cout << fixed << showpoint << setprecision(2) << endl;

    yearlyAverage = sum / 12;
    cout<<"the average is "<<yearlyAverage;


    return 0;
}

double yearlyRainAverage(){
    
}
Last edited on
I suggest you to use vectors.
1
2
3
4
5
6
7
8
#include <vector>
using namespace std;
static vector<float>yourArray(2);

float yourFunction(vector<float>Array){
Array[0] = 2.3f;
Array[1] = 2.2f;
}


then you can insert your array like this:
1
2
3
4
int main(int argc, char **argv){
yourFunction(yourArray);
return 0;
}
Last edited on
1
2
3
4
5
6
double yearlyRainAverage( double  inchesOfRain[], const int months)
{
     double sum = 0;
     for (int i = 0;  i < months;  ++i) sum += inchesOfRain[i];
     return sum / months;
}

Consider that at passing arrays to functions these arrays will be passed as pointers. So the functions don't know their size.

I also agree with peytuk: It would be a benefit if you read up into std::vector facilities, that's the most often used container and has some useful advantages than builtin arrays.
Last edited on
Since a function receives a pointer that points to caller's array, the function can modify values in the array.

Calculation of average does not need to modify the array. The function can state that clearly:
double yearlyRainAverage( const double inchesOfRain[], const int months )
Now users of the function see that the function will not modify the array
and the compiler reports an error, if you try to modify inside the function.
thank you all for your help!! is very much well appriciated! may I have one last question before I stop bothering you guys...

how can I find the subscript of the month and display it?
what is it that is not working in my function?

should say for example: "The largest amount of rainfall was: 2.00 inches in month 9"



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
#include <iostream>
#include <iomanip>
using namespace std;

double yearlyRainAverage(double[], const int);
double smallestRainfall(double [], const int);
double largestRainfall(double [], const int);
int searchHighestMonth(double[], const int, int);

int main() {

    const int months = 12;
    double inchesOfRain[months];
    double sumOfAllMonths=0;
    int monthPosition;

    monthPosition=searchHighestMonth(inchesOfRain, months, 12);


    for (int count = 0; count < months; count++)
    {
        cout<<"Enter the rainfall (in inches) for month #"<< count + 1<<":  ";
        cin>>inchesOfRain[count];

        sumOfAllMonths += inchesOfRain[count];


        if(inchesOfRain[count] < 0){

            cout <<"Rainfall must be 0 or more.\n";
            cout<<"please re-enter: "<<endl;
            cout<<"Enter the rainfall (in inches) for month #"<< count + 1<<":  ";
            cin>>inchesOfRain[count];

        }

    }
    cout << fixed << showpoint << setprecision(2) << endl;

    cout<<"the total rainfall for the year is "<<sumOfAllMonths<<" inches"<<endl;
    cout<<"the average is "<<yearlyRainAverage(inchesOfRain, 12)<<" inches"<<endl;

//    cout<<"The smallest amount of rainfall was: "<<smallestRainfall(inchesOfRain, 12)<<" inches ";
//    cout<<"in month "<<endl;

    cout<<"The largest amount of rainfall was: "<<largestRainfall(inchesOfRain, 12)<<" inches ";
    cout<<"in month "<<(monthPosition+1)<<endl;

    return 0;
}

double yearlyRainAverage(double inchesofrain[], const int months){
    double sum=0;
    for(int i=0;i<months; i++){
        sum+=inchesofrain[i];
    }
    return sum/months;
}

double smallestRainfall(double inchesofrain[], const int months){
    double smallest;
    int i;

    smallest=inchesofrain[0];
    for(i=0; i < months; i++){
        if(inchesofrain[i] < smallest){
            smallest = inchesofrain[i];
        }
    }
    return smallest;

}


double largestRainfall(double inchesofrain[], const int months){
    double largest;
    int i;

    largest=inchesofrain[0];
    for(i=0; i < months; i++){
        if(inchesofrain[i] > largest){
            largest = inchesofrain[i];
        }
    }
    return largest;
}

int searchHighestMonth(double inchesofrain[], const int months, int value){
    int index=0;
    int position = -1;
    bool found = false;

    while(index < months && !found){
        if(inchesofrain[index] == value)
        {
            found = true;
            position = index;
        }
        index++;
    }
    return position;
}


}

Last edited on
Consider:
1
2
3
4
5
6
7
8
9
10
int max_element( const double data[], int N )
{
  int max = 0;
  for ( int i=1; i < N; ++i) {
    if ( data[max] < data[i] ) {
      max = i;
    }
  }
  return max;
}

Then you can:
1
2
3
int maxmonth = max_element( inchesOfRain, months );
double maxrain = inchesOfRain[maxmonth];
std::cout << "Month " << maxmonth+1 << " had " << maxrain << " of rain\n";
Last edited on
At line 17 you're trying to get the month from the values of your array, which is at that time not yet initialized ;)
Last edited on
@keskiverto I tried what you told me, and yes. the result is now different but it gives me an incredibly long number (1x10^270)
Do the months have realistic rain values before you call the max_element?
Topic archived. No new replies allowed.