i need Help with a small bit of code

Dec 31, 2015 at 6:08pm
Im trying to output the person who ate the least, this is from the begginers exercises

for some reason the variable temp only takes the variable person, no the array index, can someone point me in the right direction, also any other tips and tricks for doing the same thing

cheers guys

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

int person = 1;
int temp = 0;
int amount[10][1];
int pig;
int input;

int main() {
	
	cout << "please input 10 peoples pancakes consumed" << endl;
	
	for (person; person<=10;)
	{
		cout << "person " << person << endl;
		cin >> input;
		amount[person][1] = input;
		person++;
	}
	for (person = 1; person <= 10;)
	{
		if (amount[person][1] < temp)
		{
			temp = amount[person][1];
			
		}
		person++;
		
	}
	for (person=1; person <= 10;)
	{
	    if (amount[person][1] == temp)
	    {
	        skinny = temp;
	    }
	    person++;
	}
	
	cout << "person who ate the least is person " << skinny << endl;
	
	return 0;
}
Dec 31, 2015 at 6:29pm
That's not exactly how for-loops work.

http://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm

"skinny" is also not anywhere in your program.

int amount[10][1];

You don't need a 2D array. You only need an array of 10 people. int amount[10]

Arrays start at the index 0, not 1. So your for-loop should go from 0-9.
Jan 1, 2016 at 1:51am
Why you make a 2D Array?
Jan 1, 2016 at 2:51am
Just to give you an idea:

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
#include <iostream>
#include <cctype> // For toupper function.

//Function Prototypes.
void inputNumOfPancakes(int[], int);
void findHighest(int[], int, int, int);
void findLowest(int[], int, int, int);

int main() 
{

    //Declare the variables.
    const int SIZE{ 10 };
    int amount[SIZE] = { 0 };
    int pos{ 0 }, highest{ 0 }, lowest{ 0 };
    char tryAgain{ ' ' };

    do
    {
	   // Call the function for the user to input the values.
	   inputNumOfPancakes(amount, SIZE);

	   // Call the function to find the highest value.
	   highest = amount[0]; // Initialize the variable with the first array element.
	   findHighest(amount, SIZE, pos, highest);

	   // Call the function to find the lowest value.
	   lowest = amount[0]; // Initialize the variables with the first array element.
	   findLowest(amount, SIZE, pos, lowest);

	   std::cout << "Do you want to input the numbers again? >>>";
	   std::cin >> tryAgain;

    } while (toupper(tryAgain) == 'Y');

    return 0;
}

void inputNumOfPancakes(int amount[], int size)
{
    std::cout << "Please input 10 peoples pancakes consumed" << std::endl;
    for (int count = 0; count < size; count++)
    {
	   std::cout << "Person number " << count + 1 << " >>>";
	   std::cin >> amount[count];
    }
}

void findHighest(int amount[], int size, int pos, int highest)
{
    for (int count = 1; count < size; count++)
    {
	   if (amount[count] > highest)
	   {
		  highest = amount[count];
		  pos = count;
	   }
    }

    std::cout << "The person who ate the most is person number " << pos + 1 << " with " <<
	   highest << " pancakes" << std::endl;
}

void findLowest(int amount[], int size, int pos, int lowest)
{
    for (int count = 1; count < size; count++)
    {
	   if (amount[count] < lowest)
	   {
		  lowest = amount[count]; 
		  pos = count;
	   }
    }

    std::cout << "The person who ate the least is person number " << pos + 1 << " with " <<
	   lowest << " pancakes" << std::endl;
}
Last edited on Jan 1, 2016 at 2:55am
Jan 1, 2016 at 6:21pm
managed to implement the changes and it now works

I havnt done much with functions just yet and havnt come across the header <cctype> yet

thanks for the help guys
Topic archived. No new replies allowed.