Trouble with using arrays in Functions

Hello,
I haven't spent a lot of time debugging this program but I wanted to preemptively post this in case it took awhile for a response. I keep getting error messages when compiling. The message says: "line 54: cannot convert 'double' for argument '1' to 'double getTotalRF(double, int)' and the same in line 55.

Now, I'm a beginner and I forget/don't understand things but I thought you declared all the types of variables in the function prototype, then when you call the function you pass the name of the array and it's size into the function. What am I missing?

Thanks in advance.


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
 /*
Josh Krynock CodeLab 70002

Rainfall Statistics

Write a program  that lets the user enter the total rainfall for each of 12 months
(starting with January) into an array  of doubles . 

The program  should calculate and display (in this order):

    the total rainfall for the year,
    the average monthly rainfall,
    and the months with the highest and lowest amounts.

Months should be expressed as English names  for months in the Gregorian calendar,
i.e.: January, February, March, April, May, June, July,
August, September, October, November, December.
*/

#include <iostream>
#include <iomanip>
using namespace std;

//Function prototypes
void getRainfall();
double getAveRF(double, int);
double getTotalRF(double, int);
double getHighRF(double, int);
double getLowRF(double, int);


//Array names
const int NUM_MONTHS = 12;
double rainfall[NUM_MONTHS];
//Month array to hold names of months
string month[NUM_MONTHS] = {"January", "February", "March",
							"April", "May", "June", 
							"July", "August", "September",
							"October", "November", "December"};
							



int main()
{
	//Variables to hold total, high/low score and average.
	double 	total,
			high, low,
			average;
	
			
	cout << fixed << showpoint << setprecision(2);
	getRainfall();
	total = getTotalRF(rainfall, NUM_MONTHS);
	high = getHighRF(rainfall, NUM_MONTHS);
	
	
	return 0;
}

void getRainfall()
{
	cout << "This program gets the total rainfall for each month\n";
	cout << "then displays the total yearly rainfall, average monthly rainfall,\n";
	cout << "and the months with the highest/lowest amounts.\n\n";
	
	for (int count = 0; count < NUM_MONTHS; count ++)
	{
		cout << "Enter rainfall for " << month[count] << ": ";
		cin >> rainfall[count];
	}
}

double getTotalRF(rainfall, NUM_MONTHS)
{
	double total = 0; 
	for (int count = 0; count < NUM_MONTHS; count++)
	{
		total += rainfall[count];
		return total;
	}
}

double getHighRF(rainfall, NUM_MONTHS)
{
		double highest; //Accumulator
		highest = rainfall[0]; //Variable to hold lowest amount of rain.
		//Loop to step through array.
		for ( int count = 1; count < NUM_MONTHS; count++)
		{
			if (rainfall[count] > highest)
				highest = rainfall[count];
		}
		return highest;
}
You are passing an array. The parameter of the prototype is not an array but a single value.

Change the signature of the function like so:

double getTotalRF(double rainfall[], int NUM_MONTHS) // Note: all parameters need a type

Change the prototype and the other functions accordingly.
The void getRainfall() is a void function so nothing is returned by the function and nothing is returned to the main. When you call getTotalRF(rainfall, NUM_MONTHS); in the main you are not passing the array to the function because it wasn't returned to the main. You have to return the array with the data from the user to the main. For me is too complicated to return an array from a function, i think you have to use pointers, so i just moved it to the main.

In the functions prototypes getTotalRF, getAveRF, getHighR, getLowRF you are passing an array so you must add the name of the array followed by []. Same rule in the functions definitions that are after the main

1
2
3
4
5
//Function prototypes
double getAveRF(double rainfall[], int);
double getTotalRF(double rainfall[], int);
double getHighRF(double rainfall[], int);
double getLowRF(double rainfall[], int);


I gave the same name "rainfall" to the array but could be any name. If you choose a different name you have to remember to give the same name in the function definitions after the main.
For example I gave it the name "ave".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Function prototypes
double getAveRF(double ave[], int);

int main()
{
   ........
}

double getAveRF(double ave[], int NUM_MONTHS)
{
   .......
   
}




This is the fixed code.

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
/*
 Josh Krynock CodeLab 70002
 
 Rainfall Statistics
 
 Write a program  that lets the user enter the total rainfall for each of 12 months
 (starting with January) into an array  of doubles .
 
 The program  should calculate and display (in this order):
 
 the total rainfall for the year,
 the average monthly rainfall,
 and the months with the highest and lowest amounts.
 
 Months should be expressed as English names  for months in the Gregorian calendar,
 i.e.: January, February, March, April, May, June, July,
 August, September, October, November, December.
 */

#include <iostream>
#include <iomanip>
using namespace std;

//Function prototypes
double getAveRF(double rainfall[], int);
double getTotalRF(double rainfall[], int);
double getHighRF(double rainfall[], int);
double getLowRF(double rainfall[], int);


//Array names
const int NUM_MONTHS = 12;
double rainfall[NUM_MONTHS];
//Month array to hold names of months
string month[NUM_MONTHS] = {"January", "February", "March",
    "April", "May", "June",
    "July", "August", "September",
    "October", "November", "December"};


int main()
{
    //Variables to hold total, high/low score and average.
    
    cout << fixed << showpoint << setprecision(2);
    cout << "This program gets the total rainfall for each month\n";
    cout << "then displays the total yearly rainfall, average monthly rainfall,\n";
    cout << "and the months with the highest/lowest amounts.\n\n";
    
    for (int count = 0; count < NUM_MONTHS; count ++)
    {
        cout << "Enter rainfall for " << month[count] << ": ";
        cin >> rainfall[count];
    }
    cout << endl;
    cout << "The Total Rainfall for the year is: " << getTotalRF(rainfall, NUM_MONTHS) << endl;
    cout << "The month with the highest rainfall is: " << getHighRF(rainfall, NUM_MONTHS) << endl;
    cout << "The month with the lowest rainfall is: " << getLowRF(rainfall, NUM_MONTHS) << endl;
    cout << "The average monthly rainfall is : " << getAveRF(rainfall, NUM_MONTHS) << endl;
    
    
    return 0;
}

double getTotalRF(double rainfall[], int NUM_MONTHS){
    double total = 0;
    for (int count = 0; count < NUM_MONTHS; count++){
        total += rainfall[count];
        }
    return total;
}

double getHighRF(double rainfall[], int NUM_MONTHS)
{
    double highest; //Accumulator
    highest = rainfall[0]; //Variable to hold lowest amount of rain.
    //Loop to step through array.
    for ( int count = 1; count < NUM_MONTHS; count++)
    {
        if (rainfall[count] > highest)
            highest = rainfall[count];
    }
    return highest;
}

double getLowRF(double rainfall[], int NUM_MONTHS)
{
    double lowest; //Accumulator
    lowest = rainfall[0]; //Variable to hold lowest amount of rain.
    //Loop to step through array.
    for ( int count = 1; count < NUM_MONTHS; count++)
    {
        if (rainfall[count] < lowest)
            lowest = rainfall[count];
    }
    return lowest;
}

double getAveRF(double rainfall[], int NUM_MONTHS)
{
    double total = 0;
    
    for (int count = 0; count < NUM_MONTHS; count++){
        total += rainfall[count];
        }
    return total/NUM_MONTHS;
   
}

Thanks to both of you for replying!!! I can't use pointers yet because we haven't covered them in class yet.

I played around with it and figured it out. This was my final solution. For whatever reason it works with the void function. I don't know why...

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
/*
Josh Krynock CodeLab 70002

Rainfall Statistics

Write a program  that lets the user enter the total rainfall for each of 12 months
(starting with January) into an array  of doubles . 

The program  should calculate and display (in this order):

    the total rainfall for the year,
    the average monthly rainfall,
    and the months with the highest and lowest amounts.

Months should be expressed as English names  for months in the Gregorian calendar,
i.e.: January, February, March, April, May, June, July,
August, September, October, November, December.
*/

#include <iostream>
#include <iomanip>
using namespace std;

//Function prototypes
void getRainfall(double[], int);
double getTotalRF(const double[], int);
int getLowRF(const double[], int);
int getHighRF(const double[], int);

//Array names
const int NUM_MONTHS = 12;
double rainfall[NUM_MONTHS];
//Month array to hold names of months
string month[NUM_MONTHS] = {"January", "February", "March",
							"April", "May", "June", 
							"July", "August", "September",
							"October", "November", "December"};
							
int main()
{
	//Variables to hold total, high/low score and average.
	double 	total, average;
	int low, high;			
	
	getRainfall(rainfall, NUM_MONTHS);
	total = getTotalRF(rainfall, NUM_MONTHS);
	low = getLowRF(rainfall, NUM_MONTHS);
	high = getHighRF(rainfall, NUM_MONTHS);
	
	cout << "Total rainfall: " << total << endl;
	cout << "Average rainfall: " << (total / NUM_MONTHS) << endl;
	cout << "Least rainfall in: " << month[low] << endl;
	cout << "Most rainfall in: " << month[high] << endl;
	
	return 0;
}

void getRainfall (double rainfall[], int size)
{
	int index; //Loop counter
	for (index = 0; index < size; index ++)
	{
		cout << "Enter rainfall for " << month[index] << ": ";
		cin >> rainfall[index];
	}
}

double getTotalRF(const double amount[], int size)
{
	double total = 0; //accumulator
	for (int index = 0; index < size; index++)
		total += amount[index];
	return total;
}

int getLowRF(const double amount[], int size)
{
	double low; //Variable to hold lowest value
	int lowMonth; //Variable to return month element location.
	
	low = amount[0];
	lowMonth = 0; //Set low value to intial rainfall value, January which is 0
	for (int index = 1; index < size; index++)
	{
		if (amount[index] < low)
		{
			low = amount[index];
			lowMonth = index;
		}
	}
	return lowMonth;
}

int getHighRF(const double amount[], int size)
{
	double high; //Variable to hold highest value
	int highMonth; //Variable to return month element location.
	
	high = amount[0];
	highMonth = 0;
	for (int index = 1; index < size; index++)
	{
		if (amount[index] > high)
		{
			high = amount[index];
			highMonth = index;
		}
	}
	return highMonth;
}
Last edited on
Topic archived. No new replies allowed.