Min/Max Problems

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

int eaten [10], i; // declaring variables


int main ()
{
    cout << "How many pancakes did the following persons eat?" << endl << endl; // getting the user to input the data
    
    for (i=0; i<=9; i++)
    {
    cout << "Person " << i << ": ";
    cin >> eaten[i];
}
    int max = eaten[0];
    for (i = 1; i<=9; i++) // calculating maximum
    {
    if (max < eaten[i])
    max = i;
    
}
    int min = eaten[0];
    for (i = 0; i<=9; i++) // calculating minimum
    {
    if (min > eaten[i])
    min = i;
}

    cout << endl << "The person who ate the most pancakes was person: " << max << endl << endl; // printing final results
    cout << endl << "The person who ate the least pancakes was person: " << min << endl << endl;
    system("PAUSE");
    return 0;
}


My problem is that when I run the code, the minimum never says person 0 has the least and the same for the maximum. It says the amount typed in instead of the person's number. I know this is because I declare each one as "eaten[0]" which enables the rest of the "for" function to work, but I don't know any other way around it.
1
2
if (max < eaten[i])
    max = i;

Here lies the problem. Originally, 'max' was declared to hold the number of pancakes that person 0 ate and here you are changing it to represent the index of the person. You need to make another variable that represents the index of the most pancakes and least pancakes.

1
2
3
4
5
6
if (max < eaten[i]) {
    max = eaten[i];
    most_index = i;
}

cout << "The person who at the most was Person #" << most_index << endl;
That doesn't seem to solve anything on my end. The output for both is now always 9 for some reason.
This is Turbo C++ code.Change this to VC++.Its 100% working.

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

int eaten [10], i; // declaring variables


int main ()
{
    cout << "How many pancakes did the following persons eat?" << endl << endl; // getting the user to input the data

    for (i=0; i<=9; i++)
    {
    cout << "Person " << i+1 << ": ";
    cin >> eaten[i];
    }
    int pos_max=1,pos_min=1;
    int max = eaten[0];
    for (i = 1; i<=9; i++) // calculating maximum
    {
    if (max < eaten[i])
    {
	max = eaten[i];
	pos_max=i+1;
    }
    }
    int min = eaten[0];
    for (i = 0; i<=9; i++) // calculating minimum
    {
    if (min > eaten[i])
    {
	min = eaten[i];
	pos_min=i+1;
    }
    }

    cout << endl << "The person who ate the most pancakes was person: " << pos_max << " with "<<max<<" pancakes"<< endl << endl; // printing final results
    cout << endl << "The person who ate the least pancakes was person: " << pos_min << " with "<<min<<" pancakes"<< endl << endl;
    cin.get();
    return 0;
}
Topic archived. No new replies allowed.