Getting a function to return an element of an array

Hello again all!

I'm having a bit of an issue trying to get a function to return which element in a an array I'm passing to the function is lowest or highest. I can get the return value of the element no problem but I'm wanting it to give me the element not the value contained inside the element. I was trying to do it with a counter but it's not working as desired. The whole program is supposed to get 12 months of rainfall data (double int) and then total the rainfall for the year, average the monthly rainfall, and then return which specific months are lowest and highest, not the amount in those months. Here's the framework. The functions in question are near the end of file. Any help would be greatly appreciated.

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

double getMonthTotal(); // Function Prototype
double findYearTotal(double[], int);// Function Prototype
double findAvg(double[], int);// Function Prototype
double findHighest(double[], int);// Function Prototype
double findLowest(double[], int);// Function Prototype

int main ()
{
	const int NUM_MONTHS = 12; // Constant for total months
	double months[NUM_MONTHS]; // Double array to pass values for each month
	double yearTotal, average, highest, lowest; // Variable to hold values the functions will send back
	
	cout << "This program will ask you for rain fall totals for 12 months.\n"
	<< "Once the date is entered the program will give you a total, an average\n"
	<< " and the highest and lowest months.\n";
	
	for (int count = 0; count < NUM_MONTHS; count++) //Loops a call to get 12 months of data from the user by calling the getMonthTotal function
	{ months[count]= getMonthTotal(); }

	yearTotal = findYearTotal(months, NUM_MONTHS); // Gets the total of all rainfall by calling the findYearTotal function
	average = findAvg(months, NUM_MONTHS); // Gets the average monthly rainfall by calling the findAvg function
	highest = findHighest(months, NUM_MONTHS); // Gets the month with the most rain by calling findHighest function
	lowest = findLowest(months, NUM_MONTHS); // Gets the month with the least rain by calling the findLowest function

	cout <<"Total Rainfall for year: \t" << yearTotal << endl; // Displays total for year
	cout << "Average Monthly rainfall: \t" << average <<endl; // Displays monthly aveage
	cout << "Least rainfall was in month: \t"<<lowest << endl;
	cout <<"Most rainfall occured in month \t" <<highest <<endl;

	char ch;	
	cout <<"Please press enter to exit\n";
	cin.ignore();
	cin.get(ch);

	return 0;
}


double getMonthTotal() // This function gets a monthly total each time it's called
{
	double monthtotal;
	cout << "\nPlease enter a total;\n";
	cin >> monthtotal;
	if (monthtotal < 0)
	{ cout <<"Invalid input\n";
		getMonthTotal();
	}
	return monthtotal;
}

double findYearTotal(double month[], int num_months) // This function totals monthly data when called
{
	double total = 0;
	for (int counter = 0; counter < num_months; counter++)
	{
		total += month[counter];
	}
	return total;
}

double findAvg( double month[], int num_months) // This function averages all monthly data when called
{
	double total = 0;
	for (int counter = 0; counter < num_months; counter++)
	{
		total += month[counter];
	}
	double average = (total / num_months);
	return average;
}

double findHighest(double month[], int num_months) // This function determines the highest value in an array
{
	int highest = month[0];
	for (int count = 0; count < num_months; count++)
	{
			if (month[count] > highest)
			highest = count;
	}
	return highest;
}

double findLowest(double month[], int num_months) // This function determins the lowest value in an array
{
	int lowest = month[0];
	for (int count = 0; count < num_months; count++)
	{
		if (month[count] < lowest)
			lowest = count;
	}
	return lowest;
}
Apply this to both functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double findHighest(double month[], int num_months) 
{
	int highest = month[0];
        int index = 0; 
	for (int count = 0; count < num_months; count++)
	{
			if (month[count] > highest)
                          {
                              highest = month [count]; 
                              index = count; 
                          }

	}
return index; 
}
I'm sorry but that still doesn't work. It does the same thing as the original code when compiled and you use test data. It's not returning the right value.

try compiling it and entering 1-12 sequentially then 12 - 1 reverse sequentially .

In both situations it's telling me the highest and lowest values both occur in month 11.
Last edited on
1
2
3
4
5
6
7
8
9
10
double findHighest(double month[], int num_months) // This function determines the highest value in an array
{
	int highest = month[0];
	for (int count = 0; count < num_months; count++)
	{
			if (month[count] > highest)
			highest = count + 1;//<<<<<<<<<<<<<<<<<<<< look at this!
	}
	return highest;
}
Yes that makes sense because of how arrays store elements, being off by 1 but I did try this yesterday and the output I was getting was unexpected. I'm going to try again when I head home for lunch but with count + 1 it was still giving me unexpected output. I forget the specifics but I'll recompile with the above and try again in about an hour and a half and post my results. Thanks Chriscpp
Sorry my bad. count + 1 doesn't work :(
You use the suggestion I made but just add + 1 to index at the end
 
return index + 1; 


However, the reason the
 
highest = count + 1; 


doesn't work is because it in essence adds a number to be tested like for example if we used 1 7 4 2

the debug would for highest would be like this
highest = 1
highest = 7
highest = 3 <--- notice the new number we are testing
highest = 4
highest = 4 <---- now we have the wrong index position.

also for the lowest function if you need to keep the operation to the appropriate test so....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
double findLowest(double month[], int num_months)
{
	int lowest = month[0];
       int index = 0; 
	for (int count = 0; count < num_months; count++)
	{
		if (month[count] < lowest)
		{ // I noticed you aren't adding the brackets for the if statements make sure you do so
                       lowest = month[count];
                        index = count; 
                }
	}
	return index + 1;
}


Last edited on
I see what you're saying. Ok that makes a little more sense. I'll go try this in about 30 minutes and report back. I really appreciate the help you guys.
This still isn't working. I'm getting the value 12 for both highest and lowest with index = count + 1;

All logical reason tells me that it should be working but it's just not.
Last edited on
It does sound like you're still mixing up the index and the value it's at.

kingkong200's response looks ok to me (though it could start the loop at count = 1, rather than 0.)

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

double getMonthTotal(); // Function Prototype
double findYearTotal(double[], int);// Function Prototype
double findAvg(double[], int);// Function Prototype
double findHighest(double[], int);// Function Prototype
double findLowest(double[], int);// Function Prototype

int main ()
{
	const int NUM_MONTHS = 12; // Constant for total months
	double months[NUM_MONTHS]; // Double array to pass values for each month
	double yearTotal, average, highest, lowest; // Variable to hold values the functions will send back
	
	cout << "This program will ask you for rain fall totals for 12 months.\n"
	<< "Once the date is entered the program will give you a total, an average\n"
	<< " and the highest and lowest months.\n";
	
	for (int count = 0; count < NUM_MONTHS; count++) //Loops a call to get 12 months of data from the user by calling the getMonthTotal function
	{ months[count]= getMonthTotal(); }

	yearTotal = findYearTotal(months, NUM_MONTHS); // Gets the total of all rainfall by calling the findYearTotal function
	average = findAvg(months, NUM_MONTHS); // Gets the average monthly rainfall by calling the findAvg function
	highest = findHighest(months, NUM_MONTHS); // Gets the month with the most rain by calling findHighest function
	lowest = findLowest(months, NUM_MONTHS); // Gets the month with the least rain by calling the findLowest function

	cout <<"Total Rainfall for year: \t" << yearTotal << endl; // Displays total for year
	cout << "Average Monthly rainfall: \t" << average <<endl; // Displays monthly aveage
	cout << "Least rainfall was in month: \t"<<lowest << endl;
	cout <<"Most rainfall occured in month \t" <<highest <<endl;

	char ch;	
	cout <<"Please press enter to exit\n";
	cin.ignore();
	cin.get(ch);

	return 0;
}


double getMonthTotal() // This function gets a monthly total each time it's called
{
	double monthtotal;
	cout << "\nPlease enter a total;\n";
	cin >> monthtotal;
	if (monthtotal < 0)
	{ cout <<"Invalid input\n";
		getMonthTotal();
	}
	return monthtotal;
}

double findYearTotal(double month[], int num_months) // This function totals monthly data when called
{
	double total = 0;
	for (int counter = 0; counter < num_months; counter++)
	{
		total += month[counter];
	}
	return total;
}

double findAvg( double month[], int num_months) // This function averages all monthly data when called
{
	double total = 0;
	for (int counter = 0; counter < num_months; counter++)
	{
		total += month[counter];
	}
	double average = (total / num_months);
	return average;
}

double findHighest(double month[], int num_months) // This function determines the highest value in an array
{
	int highest = month[0];
	int index =0;
	for (int count = 0; count < num_months; count++)
	{
			if (month[count] > highest)
			highest = month[count];
			index = count + 1;
	}
	return index;
}

double findLowest(double month[], int num_months) // This function determins the lowest value in an array
{
	int lowest = month[0];
	int index = 0;
	for (int count = 0; count < num_months; count++)
	{
		if (month[count] < lowest)
			lowest = month[count];
			index = count + 1;
	}
	return index;
}


It's not working. compile and try entering 1-12 for each element then 12-1 and see what I mean.

Also you have to set count to 0 initially because an array starts with 0 not 1.
Last edited on
lines 82-84 and lines 95-97 are still missing the brackets that kingkong200 pointed out...

1
2
3
4
if(condition)
    func1();
    func1();
    func3();


ONLY func1 is controlled by the if-condition, irrespective of the indentation

This is better written as

1
2
3
4
if(condition)
    func1();
func1();
func3();


or even better as

1
2
3
4
5
if(condition) {
    func1();
}
func1();
func3();


To control all functions at the same time, your need

1
2
3
4
5
if(condition) {
    func1();
    func1();
    func3();
}


Andy
Last edited on
Wow. Good grief. I'm sorry guys. Thank you all for pointing that out. I'm still getting a 0 value when highest or lowest appears in element[0] even with the +1 though. But at least it is pointing to the right element now.
Try this, Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
double findHighest(double month[], int num_months) // This function determines the highest value in an array
{
	int highest = month[0];
	int index = 0;
	for (int count = 1; count < num_months; count++)
	{
			if (month[count] > highest)
                        {
			    highest = month[count];
			    index = count; // Edit: was + 1;
			}
	}
	return index + 1; // Edit: +1 now here
}


(An alternative fix would have beed to initialized index to 1 rather than 0.)
Last edited on
don't go
 
index = count + 1; 


but rather just go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
double findLowest(double month[], int num_months)
{
	int lowest = month[0];
       int index = 0; 
	for (int count = 1; count < num_months; count++)
	{
		if (month[count] < lowest)
		{ // I noticed you aren't adding the brackets for the if statements make sure you do so
                       lowest = month[count];
                        index = count; 
                }
	}
	return index + 1; // notice the + 1 is here and not by count 
}


The reason the + 1 is at the end is to adjust for the low value being in index spot of 0.

For example in the find low function we use the data set 1 5 4 3
so technically the lowest value is 1 and it index spot is 0. Accordingly, when it goes into the loop it should not return a true value at the if statement and therefore the index value will remain a zero. However, if we put that + 1 at the return statement it will automatic update index to be index + 1. Or in this case the value return to the main will be 1. Hopefully, that makes sense.


Andy is correct the for loop can start at 1 if you decide to do that as the first loop just compares array[0] and array[0].
Last edited on
I had to leave my house and head back to work so I won't be able to try it until I get home but the explanation as to why it needs to be outside the if statement makes total sense now that I see it explained. I really appreciate the help guys!
Topic archived. No new replies allowed.