Parallel Arrays

I'm having problems with my homework and my professor is not responding. How do I get my arrays to return the string name of the month for the getHighest and getLowest functions?

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

//Function prototypes
void getRainfall(double[], int);  //To retrieve the user input.
double getTotal(double[], int);  //To total the rainfall amounts.
double getAverage(double[], int);  //To get the average rainfall.
double getLowest(double[], int, int&);  //Returns the lowest value, provides the index of the lowest value in the last parameter.
double getHighest(double[], int, int&); //Returns the highest value, provides the index of the highest value in the last parameter.

//Global Variable
const int NUM_MONTHS = 12;

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

int main()
{
	//Declare variables
	double 	total, average;
	int low, high;
	string lowMonth, highMonth;

	//Call Functions
	getRainfall(rainfall, NUM_MONTHS);  //To retrieve the user input.
	total = getTotal(rainfall, NUM_MONTHS);	//To total the rainfall amounts.
	average = getAverage(getTotal, NUM_MONTHS);  //To get the average rainfall.
	lowMonth = getLowest(rainfall, NUM_MONTHS, low);  //Returns the lowest value, provides the index of the lowest value in the last parameter.
	highMonth = getHighest(rainfall, NUM_MONTHS, high); //Returns the highest value, provides the index of the highest value in the last parameter.
	
	//Display the following:
	cout << "The total rainfall for the year is: " << total << endl;
	cout << "The average rainfall for the year is: " << fixed << showpoint << setprecision(2) << average << endl;
	cout << "Least amount of rainfall fell in: " << highMonth << endl;
	cout << "Most amount of rainfall fell in: " << lowMonth << endl;

	return 0;
}

//********************************************************************
//				void getRainfall(double[], int)						 *
//	To retrieve the user input for the total rainfall for each month.*
//********************************************************************

void getRainfall(double rainfall[], int NUM_MONTHS)
{
	int index; //Loop counter

	for (index = 0; index < NUM_MONTHS; index++)
	{
		cout << "Enter rainfall for " << month[index] << ": ";
		cin >> rainfall[index];
	}
}

//********************************************************************
//			double getTotal(const double amount[], int size)		 *
//			To total the rainfall amounts.							 *
//********************************************************************

double getTotal(double amount[], int NUM_MONTHS)
{
	double total = 0; //Accumulator

	for (int index = 0; index < NUM_MONTHS; index++)
		total += amount[index];

	return total;
}

//********************************************************************
//			double getAverage(double rainfall[], int NUM_MONTHS);	 *
//			To get the average rainfall.							 *
//********************************************************************

double getAverage(double getTotal, int NUM_MONTHS)
{
	//double total = 0;
	double average;

	//for (int count = 0; count < NUM_MONTHS; count++) 
	//	total += rainfall[count];
	
	average = getTotal / NUM_MONTHS;

	return average;
}

//*******************************************************************************************
//				double getLowest(double amount[], int size)									*
//	Returns the lowest value, provides the index of the lowest value in the last parameter. *
//*******************************************************************************************

double getLowest(double amount[], int NUM_MONTHS, int &low)
{
	low = amount[0];  //Variable to hold lowest value.
	int lowMonth = 0; //Set low value to intial rainfall value.  //Variable to return month element location.
	
	for (int index = 0; index < NUM_MONTHS; index++)  
	{
		if (amount[index] < low)
		{
			low = amount[index];
			lowMonth = index;
		}
	}

	return lowMonth;
}

//*********************************************************************************************
//				double getHighest(double amount[], int size)								  *
//	Returns the highest value, provides the index of the highest value in the last parameter. *
//*********************************************************************************************

double getHighest(double amount[], int NUM_MONTHS, int &high)
{
	high = amount[0];  //Variable to hold highest value
	int highMonth = 0; //Set high value to intial rainfall value.  //Variable to return month element location.

	for (int index = 0; index < NUM_MONTHS; index++)	
	{
		if (amount[index] > high)
		{
			high = amount[index];
			highMonth = index;
		}
	}

	return highMonth;
}
Let's start with one of the functions:

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
//*********************************************************************************************
//				double getHighest(double amount[], int size)								  *
//	Returns the highest value, provides the index of the highest value in the last parameter. *
//*********************************************************************************************

double getHighest(double amount[], int NUM_MONTHS, int &high)
{
        // Your comment below is incorrect, this should be the index value of the array (the month number).
	high = amount[0];  //Variable to hold highest value
	
        // This should be a double and this is what should be assigned the highest value.
        int highMonth = 0; //Set high value to intial rainfall value.  //Variable to return month element location.

	for (int index = 0; index < NUM_MONTHS; index++)	
	{
		if (amount[index] > high)
		{
                        // You have the variables reversed. high is the index, highMonth is the value.
			high = amount[index];
			highMonth = index;
		}
	}

	return highMonth;
}


Look closely at your comment about what the function will be doing. Your code doesn't match the comment. So the first thing to do is to rework the function so that it is doing what your comment states is the purpose.



Ok, so it should look more like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//*********************************************************************************************
//				double getHighest(double amount[], int size)								  *
//	Returns the highest value, provides the index of the highest value in the last parameter. *
//*********************************************************************************************

double getHighest(double amount[], int NUM_MONTHS, int &highMonth)
{
	//high = amount[0];  //Variable to hold highest value
	months = amount[0];
	double highMonth = 0; //Variable to hold highest value

	for (int index = 0; index < NUM_MONTHS; index++)	
	{
		if (amount[index] > high)
		{
			highMonth = amount[index];
			months = index;
		}
	}

	return highMonth;
}
Last edited on
Here's a list of my errors from the build.

1>------ Build started: Project: Week7_Assignment1, Configuration: Debug Win32 ------
1> Source.cpp
1>c:\users\beverly\onedrive\school\ciss 241\week7_assignment1\week7_assignment1\source.cpp(64): error C2664: 'double getAverage(double [],int)': cannot convert argument 1 from 'double (__cdecl *)(double [],int)' to 'double []'
1> c:\users\beverly\onedrive\school\ciss 241\week7_assignment1\week7_assignment1\source.cpp(64): note: There is no context in which this conversion is possible
1>c:\users\beverly\onedrive\school\ciss 241\week7_assignment1\week7_assignment1\source.cpp(65): warning C4244: 'argument': conversion from 'double' to 'char', possible loss of data
1>c:\users\beverly\onedrive\school\ciss 241\week7_assignment1\week7_assignment1\source.cpp(66): warning C4244: 'argument': conversion from 'double' to 'char', possible loss of data
1>c:\users\beverly\onedrive\school\ciss 241\week7_assignment1\week7_assignment1\source.cpp(133): warning C4244: '=': conversion from 'double' to 'int', possible loss of data
1>c:\users\beverly\onedrive\school\ciss 241\week7_assignment1\week7_assignment1\source.cpp(140): warning C4244: '=': conversion from 'double' to 'int', possible loss of data
1>c:\users\beverly\onedrive\school\ciss 241\week7_assignment1\week7_assignment1\source.cpp(156): error C2065: 'months': undeclared identifier
1>c:\users\beverly\onedrive\school\ciss 241\week7_assignment1\week7_assignment1\source.cpp(157): error C2082: redefinition of formal parameter 'highMonth'
1>c:\users\beverly\onedrive\school\ciss 241\week7_assignment1\week7_assignment1\source.cpp(161): error C2065: 'high': undeclared identifier
1>c:\users\beverly\onedrive\school\ciss 241\week7_assignment1\week7_assignment1\source.cpp(163): warning C4244: '=': conversion from 'double' to 'int', possible loss of data
1>c:\users\beverly\onedrive\school\ciss 241\week7_assignment1\week7_assignment1\source.cpp(164): error C2065: 'months': undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
Note the difference in arguments between line 30 and lines 28-29 and 31-32.
Yes? You can't call a function to determine the average? I read in my book that you could.
Yes? You can't call a function to determine the average? I read in my book that you could.

Yes, you can. The difference between line 30 and those other lines are that in the other lines you are calling those (similar functions) with an array name as the first argument. On line 30, you are using a function name as the first argument, when an array is expected.
Oh, ok. Do I call the function after the array then? Sorry, these arrays have me very confused.
Topic archived. No new replies allowed.