Trying to pass an Array into an argument as a pointer, but sumTINGwong

I am having trouble passing the array as an argument into the function, it is saying the "&" in the function call is a problem. But it works for normal variables. I guess I am confused?

This is an assignment for my introduction to programming concepts class, I am usually very resourceful but can not seem to find my specific problem out there in the web.

Thanks,

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
Rainfall Statistics
Design a program that lets the user enter the total rainfall for each of 12 months
into an array. 
The program should calculate and display the total rainfall for the year, 
the average monthly rainfall, and the months with the highest and lowest amounts.*/

#include <iostream>
using namespace std;

// Function Prototypes
float getMonths(float *month[]);
float calcTotal(float *month[], float *totalRainfall);
float findHighest(float *month[], float *highest);
float findLowest(float *month[], float *lowest);
void printInformation(float *totalRainfall, float *highest, float *lowest);

// Main Function ---------------------------------------------------------------------
int main()
{
	// Declaring Variables
	float month[] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
	float totalRainfall = 0;
	float averageMonthlyRainfall = 0;
	float highest = 0;
	float lowest = 0;
	
	// Function Calls
	getMonths(&month);
	calcTotal(&month, &totalRainfall);
	findHighest(&month, &highest);
	findLowest(&month, &lowest);
	printInformation(&totalRainfall, &highest, &lowest);

	return 0;
}
// End main ---------------------------------------------------------------------------

float getMonths(float *month[])
{
	for (int x = 0; x < 12; x++) /* x is the index of the array */
	{
		cout << "Enter the amount for rain for month #" << x << endl;
		cin >> *month[x];
	return *month[x];
	}
}


float calcTotal(float *month[], float *totalRainfall)
{
	for (int x = 0; x < 12; x++)
	{
		*totalRainfall += *month[x];
	}
return *totalRainfall;
}
	

float findHighest(float *month[], float *highest)
{
	*highest = *month[0]; // assignes the lelement to be the highest or lowest value
	for (int x = 0; x < 12; x++)
	{
		if (*month[x] > *highest)
		{
			*highest = *month[x];
		}

	}
	return *highest;
}


float findLowest(float *month[], float *lowest)
{
	*lowest = *month[0]; // assignes the lelement to be the highest or lowest value
	for (int x = 0; x < 12; x++)
	{
		if (*month[x] < *lowest)
		{
			*lowest = *month[x];
		}

	}
	return *lowest;
}


void printInformation(float *totalRainfall, float *highest, float *lowest)
{
	string monthName = "";
	
	switch ()
	{
	case 1:
		monthName = "January";
		break;
	case 2:
		monthName = "February";
		break;
	default:
		cout << "Somthing went wrong!";

	}

	cout << "Total rainfall for the year was: " << totalRainfall << endl;

	cout << "Highest month of rainfall was" monthName;
}
Don't try to pass arrays via pointers (or by reference for that matter) - you're making it too complicated.

The variable averageMonthlyRainfall in your main function is unused.

Additionally, can you tell me exactly what your getMonths() function is supposed to do?
Hi Xismn,

Thanks for catching my variable I was not using. I totally forgot I need to get the Average rainfall for the whole year.

the function getMonths() is supposed to get the input from the user for the amount of rain for each month and store all twelve into the array month[]

So, i should try making the array local in the function?
float getMonths(float *month[]) == getMonths(float**)
This function takes a pointer to pointer-to-float.

You want a function that takes a pointer-to-float:
getMonths(float month[]) or (equivalently) getMonths(float*)


So your function becomes:
1
2
3
4
5
6
7
8
9
10
void getMonths(float *month)
{
	for (int x = 0; x < 12; x++) /* x is the index of the array */
	{
		cout << "Enter the amount for rain for month #" << x << endl;
		cin >> month[x];
		// return *month[x];
	}
		// an unconditional return inside a loop means you don't have a loop.
}


And is called like so: getMonths(month);

If I use a void for the function, I will not get a return value right?

I was thinking if I have the return inside of the for loop, it would allow it to return each value back into its element within the array.

But I am just guessing that's how it works.


Oh I think what you're saying is that if the RETURN is within the FOR LOOP it's technically not a loop, i think I am getting what your saying
Last edited on
Yeah the RETURN inside of the FOR LOOP was my main problem.
also, I now have learned about passing arrays into functions, it's kinda like they are pointers but you don't have to point to them.

Thank's guys!

I'll show the completed program when I am done with the smaller bugs
Here is the completed program / Assignment.
Thank you for helping me understand!

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235

/*

Rainfall Statistics
Design a program that lets the user enter the total rainfall for each of 12 months
into an array.
The program should calculate and display the total rainfall for the year,
the average monthly rainfall, and the months with the highest and lowest amounts.
*/

#include <iostream>
#include <string.h>
#include <string>
#include <consoleapi.h>

using namespace std;

float getMonths(float month[], int size);
float calcTotal(float month[], float *totalRainfall);
float findHighest(float month[], float *highest, int *highMonth);
float findLowest(float month[], float *lowest, int *lowMonth);
float findAverage(float *average, float *total);
void printInformation(float total, float highest, float lowest, int *highMonth, int *lowMonth, float average);

// Main Function ---------------------------------------------------------------------
int main()
{
	string keepGoing = "yes";
	while (keepGoing == "yes")
	{

		// Declaring Variables

		const int size = 12;
		float month[size] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
		float totalRainfall = 0;
		float averageMonthlyRainfall = 0;

		int highestMonth = 0;
		int lowestMonth = 0;

		float highest = 0;
		float lowest = 0;

		// Function Calls
		getMonths(month, size); // Function for getting the input for the array
		calcTotal(month, &totalRainfall); // gets the total rainfall for the year (all elements of the array summed up)
										  // add function for finind the average yearly rainfall *****************************************
		findHighest(month, &highest, &highestMonth); // finds the highest number in the array
		findLowest(month, &lowest, &lowestMonth); // finds the lowest number in the array

		findAverage(&averageMonthlyRainfall, &totalRainfall);

		printInformation(totalRainfall, highest, lowest, &highestMonth, &lowestMonth, averageMonthlyRainfall); // prints the output to screen

		cout << "Do you want to run the program again? (yes or no?)" << endl;
		cin >> keepGoing;
		system("cls");
	}
	return 0;
}
// End main ---------------------------------------------------------------------------




float getMonths(float month[], int size)
{
	int x = 0;
	for (int x = 0; x < size; x++) /* x is the index of the array */
	{
		cout << "Enter the amount for rain for month #" << (x + 1) << " in inches: ";
		cin >> month[x];
		system("cls");
	}
	return x;
}


float calcTotal(float month[], float *totalRainfall)
{
	for (int x = 0; x < 12; x++)
	{
		*totalRainfall += month[x];
	}
	return *totalRainfall;
}


float findHighest(float month[], float *highest, int *highMonth)
{
	*highest = month[0]; // assigns the element to be the highest value
	for (int x = 0; x < 12; x++)
	{
		if (month[x] > *highest)
		{
			*highest = month[x];
			*highMonth = x;
		}

	}
	return *highest, *highMonth;
}


float findLowest(float month[], float *lowest, int *lowMonth)
{
	*lowest = month[0]; // assigns the element to be the lowest value
	for (int x = 0; x < 12; x++)
	{
		if (month[x] <= *lowest)
		{
			*lowest = month[x];
			*lowMonth = x;
		}

	}
	return *lowest, *lowMonth;
}


float findAverage(float *average, float *total)
{
	*average = (*total / 12);
	return *average;
}


void printInformation(float total, float highest, float lowest, int *highMonth, int *lowMonth, float average)
{
	// local variables 
	string monthNameHigh = "";
	string monthNameLow = "";

	// Selection Case to find the name of the month that had the HIGHEST amount of rain
	switch (*highMonth)
	{
	case 0:
		monthNameHigh = "January";
		break;
	case 1:
		monthNameHigh = "February";
		break;
	case 2:
		monthNameHigh = "March";
		break;
	case 3:
		monthNameHigh = "April";
		break;
	case 4:
		monthNameHigh = "May";
		break;
	case 5:
		monthNameHigh = "June";
		break;
	case 6:
		monthNameHigh = "July";
		break;
	case 7:
		monthNameHigh = "August";
		break;
	case 8:
		monthNameHigh = "September";
		break;
	case 9:
		monthNameHigh = "October";
		break;
	case 10:
		monthNameHigh = "November";
		break;
	case 11:
		monthNameHigh = "December";
		break;
	default:
		cout << "Error <swtich1>";

	}

	// Selection Case to find the name of the month that had the LOWEST amount of rain
	switch (*lowMonth)
	{
	case 0:
		monthNameLow = "January";
		break;
	case 1:
		monthNameLow = "February";
		break;
	case 2:
		monthNameLow = "March";
		break;
	case 3:
		monthNameLow = "April";
		break;
	case 4:
		monthNameLow = "May";
		break;
	case 5:
		monthNameLow = "June";
		break;
	case 6:
		monthNameLow = "July";
		break;
	case 7:
		monthNameLow = "August";
		break;
	case 8:
		monthNameLow = "September";
		break;
	case 9:
		monthNameLow = "October";
		break;
	case 10:
		monthNameLow = "November";
		break;
	case 11:
		monthNameLow = "December";
		break;
	default:
		cout << "Error <switch2>";

	}

	// Prints total rainfall
	cout << endl;
	cout << "Total rainfall for the year was: " << total << endl;
	cout << "The average amount of rainfall total over the year is: " << average << " in." << endl;

	// Prints the High's
	cout << "The month with the highest rainfall was: " << monthNameHigh << " with an amount of " << highest << " in. of rain" << endl;

	// Prints the Low's
	cout << "The month with the lowest rainfall was: " << monthNameLow << " with an amount of " << lowest << " in." << endl;
}

Topic archived. No new replies allowed.