Array - compare elements and extract min max values

alright, here is the problem,
i made an array of 10 people,
used for loop to input how many cookies which person ate,
then output that into a table,

on second for loop i can show who ate the most cookies.
But i cant do the same with the minimum, i can do it if there is 0 value in the array but anything more than 0 wont work.

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
  #include "stdafx.h"
#include <iostream>

using namespace std;



int main()
{
	int anPersons[10];
	int nMax = 0;
	int nMin = 0;
	int nPerson;

	// Header
	cout << "element - value\n";
	// Input cookies amounts eaten!

	for (int x = 0; x <= 9; x++)
	{
		cin >> anPersons[x];
	}
	// Output table!
	cout << "**********************\n";
	for (int x = 0; x <=9; x++)
	{
		cout << "Person " << x << " ate " << anPersons[x] << " cookies.\n";
	}
	// Find cookie monster!
	for (int x = 0; x <= 9; x++)
	{
		if (anPersons[x] > nMax)
		{
			nMax = anPersons[x];
			nPerson = x;
		}
	}
	cout << "Person number " << nPerson << " ate " << nMax << " cookies.\n";
	// Find cookie hater!
	for (int x = 0; x <= 9; x++)
	{
		if (anPersons[x] <= nMin)
		{
			nMin = anPersons[x];
			nPerson = x;
		}
	}
	cout << "Person number " << nPerson << " ate: " << nMin << " cookies.\n";



cin.clear();
	cin.ignore(255, '\n');
	cin.get();
	return 0;
}
before line 40 put:
nMin = anPersons[0];
This sets the minimum value to be initially the first person in your array. Then you can iterate over looking for someone who ate less.
wow that did it, thanks a lot
Topic archived. No new replies allowed.