Finding largest and smallest element in array

hey guys, I’m trying to display the highest and lowest elements in this array. I can find the highest element no problem, but the lowest isn’t working and I'm not sure why (lines 14 - 21). I think its just a simple mistake but haven’t been able to fine it.

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
	cout << "      Only the reds sorted by price" << endl;
	double sumR = 0;
	int z = 0;
	double hw = 0, lw = 0;
	double redWP[wines.size()];
	lw = redWP[0];
	for (int i = 0; i < wines.size(); i++) 
	{
		wt = wines[i]->get_wine_type();
		if (wt == "red")
		{
			sort(wines.begin(), wines.end(), sort_price);
			redWP[i] = wines[i]->get_price();
			if ( redWP[i] > hw)
			{
				hw = redWP[i];
			}
			if ( redWP[i] < lw)
			{
				lw = redWP[i];
			}
			wines[i]->print();
			sumR += redWP[i];
			cout << sumR << endl;
			z++;
		}
	
	}
Last edited on
1
2
	double redWP[wines.size()];
	lw = redWP[0];


lw begins with a random value that could be lower than all the values you compare it to.
Your Awesome! That was the problem.

However I have just set it to lw = 99999999, what would be a way to ensure that there is no value lower then this to start? in this program its user entered)
You could set it to the highest possible double value, std::numeric_limits<double>::max()
which you can get through #include <limits> .
cool! thank you for the help!
Topic archived. No new replies allowed.