Not showing maximum or minimum

Hello all, I am doing a tiger eating problem in which I put the amount of food over 7 days for 3 tigers. I then Have to get maximum, minimum average.

I run the program but it doesn't print the correct maximum or minimum, despite the program working without being in a function. Thank you.

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  #include<iostream>
using namespace std;


const int DAYS = 7;

void inputArray(float food[][DAYS],int tigers);




void AvgTig(float food[][DAYS], float tavg[], int tigers);


float Most(float food[][DAYS], int , float);

float least(float food[][DAYS], int , float);


int main()
{
	float maximum = 0;
	float minimum = 0;
	float total = 0.0f;
	float totaverage = 0.0f;
	float result = 0;
	const int TIGERS = 3;
	float food[TIGERS][DAYS];
	int temp, amb;



	 inputArray(food,TIGERS);
	
	 maximum = food[0][0];
	 minimum = food[0][0];
	

	 float tavg[TIGERS];

	 AvgTig(food, tavg, TIGERS);
	 cout << "\n \n \n";

	 cout << "Average for Tiger 1, 2 & 3 in Order: \n";
	 for (int i = 0; i < TIGERS; ++i) {
		 cout << tavg[i] << " ";
	 }



	 cout << "\n \n ";

	 cout << "Enter whether you want to find minimum for tiger 1 2 or 3. (Please only enter 0, 1 or 2): ";

	 cin >> temp;

	 least(food, temp, minimum);
	 cout << "\n";

	 cout << "The Tiger " << temp << " has minimum: " << minimum << " ";

	 cout << "\n \n ";

	 cout << "Enter which tiger you want to find maximum for: ";

	 cin >> amb;
	 Most(food, amb, maximum);

	 cout << "The Tiger " << amb << " has maximum: " << maximum << " ";

	system("PAUSE");

	return 0;

	
}

void inputArray(float food[][DAYS],int tigers) {
	int total = 0;
	for (int tig = 0; tig < tigers; tig++)
	{
		for (int day = 0; day < DAYS; day++)
		{
			cout << "TIGER " << (tig + 1) << ", day " << (day + 1) << ": ";
			cin >> food[tig][day];

		}
		cout << endl;
	}

}

void AvgTig(float food[][DAYS], float tavg[], int tigers)

	{
		
		for (int i = 0; i < tigers; i++)
		{
			float total = 0; 
			for (int day = 0; day < DAYS; day++)
			{

				total += food[i][day];

			}
			// I have it for the tiger i, so save it 
			tavg[i] = total / DAYS;
			cout << endl;
		}
		
	
	}


	
  


float least(float food[][DAYS], int temp, float min) //loop for days only 
{

	min = food[0][0];  //temp has to be les
	for (int j = 0; j < DAYS; ++j) {
		if (food[temp][j] < min)
			min = food[temp][j];

	}
	return min;

}



float Most(float food[][DAYS], int amb, float max) //loop for days only 
{


	max = food[0][0];
	//amb has to be less than 3
	for (int j = 0; j < DAYS; ++j) {
		if (food[amb][j] > max)
			max = food[amb][j];

	}
	return max;

}
Your main() function never updates the values stored in minumum and maximum. Your Most() function returns the result of the calculation, but your main() ignores that returned value.
Last edited on
Why does it ignore? It only prints the first element that I have initiated
Last edited on
It ignores it because you haven't done anything with the return value of the function. Your main() function never even looks at the return value.
Last edited on
How to fix that? as far as my knowledge goes it should work, I return the value and call the function correctly
Last edited on
If you don't understand how to read the return value of a function, then I'd strongly advise you to revisit your textbook and reread what it has to say.

Calling functions, and getting data in and out of them, is one of the most important aspects of C++, and one of the most basic. You really need to get a proper understanding of them, and you'll get that far more effectively from a proper textbook than by asking piecemeal questions on a forum.

And your AvgTig function doesn't even return anything. That's obvious, because you've declared the return value as void.
Can you recommend the so called proper textbook?

Can I atleast fix the error...
Last edited on
The error is that you need to actually read the return value from the function.

(Or, if you prefer, have a variable passed by reference, so that when the function modifies the value then the value in the calling code also gets modified. But, honestly, that's a more advanced option, and it sounds like what you really need right now is a better grip of the basics.)

If you don't have any kind of textbook, then this tutorial will explain what you need to know. I recommend reading it:

http://www.cplusplus.com/doc/tutorial/functions/

As for textbook recommendations, it's been nearly 30 years since I learnt to program in C/C++. I doubt any book I learned from is still in print. I know there are people here who often recommend modern textbooks, so maybe they can help.
Topic archived. No new replies allowed.