'Finding the highest and smallest number in a 2D array not working
So I've been looking for a solution but nothing yet. Moreover most of the program works fine is just when it comes to calculating the greatest number and the lowest number, it displays the wrong day, amount of food and wrong monkeyNum.
Here's an example of what's wrong: Wrong Output
Although the average is right. The greatest and lowest number are completely wrong. Monkey number 2 didn't ate 3 pounds on day 4 nor is that the lowest amount for that monkey, nor is the lowest amount from the whole family of monkeys. The lowest amount in this case should be 0.01 but it isn't. The greatest number for monkey number 3 is right but that's not the greatest value in the 2D array, the greatest value should be 98, monkey #2 day #2
Here's the part of the code that could be wrong:
double MIN_Food(double MINf[][DAYS], int monkey, int &subscriptD, int &subscriptM) // Calculate the monkey the consumed the least amount of food
{
int row, col;
double min{};
for (row = 0; row < monkey; row++)
{
min = MINf[0][0];
for (col = 0; col < DAYS; col++)
{
if (MINf[row][col] < min)
{
min = MINf[row][col];
subscriptD = col; // Store day number
subscriptM = row; // Store monkey number
}
}
}
return min;
}
double MAX_Food(double MAXf[][DAYS], int monkey, int &subscriptD, int &subscriptM) // Calculate the monkey that consumed the most amount of food
{
int row, col;
double max{};
for (row = 0; row < monkey; row++)
{
max = MAXf[0][0];
for (col = 0; col < DAYS; col++)
{
if (MAXf[row][col] > max)
{
max = MAXf[row][col];
subscriptD = col;
subscriptM = row;
}
}
}
return max;
}
Here's the whole code:
#include <iostream>
using namespace std;
// Declare global variables
const int DAYS = 5; // Columns
const int MONKEY = 3; // Rows
// Prototype functions
void DayAvg(double[][DAYS], int);
double MIN_Food(double[][DAYS], int, int&, int&);
double MAX_Food(double[][DAYS], int, int&, int&);
int main()
{
double Monkey_Consumption[MONKEY][DAYS]; // 2D Array to hold the data entered by the user.
double leastF, mostF; // Hold the lowest and highest value within 2D array
int Subscript_Day = 0, // Holds day of consumption
Subscript_Monkey = 0; // Holds monkey who consumed
DayAvg(Monkey_Consumption, MONKEY);
leastF = MIN_Food(Monkey_Consumption, MONKEY, Subscript_Day, Subscript_Monkey); // Call the function the calculates de least amount of food consumed.
cout << "Monkey number "; // Note: Had to format the cout statements this way because it wasn't displaying the right results the other way; probably IDE bug.
cout << (Subscript_Monkey + 1);
cout << " ate the least amount of food,\n" << leastF;
cout << " pounds, on day " << (Subscript_Day + 1) << endl;
mostF = MAX_Food(Monkey_Consumption, MONKEY, Subscript_Day, Subscript_Monkey); // Call the function the calculates de higher amount of food consumed.
cout << "Monkey number ";
cout << (Subscript_Monkey + 1);
cout << " ate the most amount of food,\n" << mostF;
cout << " pounds, on day " << (Subscript_Day + 1);
}
void DayAvg(double DailyF[][DAYS], int monkey) // Calculate avg amount of pounds consumed everyday
{
for (int Monkey_Num = 0; Monkey_Num < MONKEY; Monkey_Num++) // Loop to update rows
{
for (int Day_Num = 0; Day_Num < DAYS; Day_Num++) // Loop to update columns
{
cout << "Enter the pounds eaten by monkey number " << (Monkey_Num + 1) << "\non day " << (Day_Num + 1);
cout << ": ";
cin >> DailyF[Monkey_Num][Day_Num]; // Get data from user
}
}
for (int col = 0; col < DAYS; col++)
{
double total = 0; // Variable to hold the sum of a single column
for (int row = 0; row < MONKEY; row++) // Loop to sum the elements of each column
{
total += DailyF[row][col];
}
double average = total / MONKEY; // Calculate the average
//Display results
cout << "The average amount eaten on day " << (col + 1) << " is " << average << " pounds.\n";
}
}
double MIN_Food(double MINf[][DAYS], int monkey, int &subscriptD, int &subscriptM) // Calculate the monkey the consumed the least amount of food
{
int row, col;
double min{};
for (row = 0; row < monkey; row++)
{
min = MINf[0][0];
for (col = 0; col < DAYS; col++)
{
if (MINf[row][col] < min)
{
min = MINf[row][col];
subscriptD = col; // Store day number
subscriptM = row; // Store monkey number
}
}
}
return min;
}
double MAX_Food(double MAXf[][DAYS], int monkey, int &subscriptD, int &subscriptM) // Calculate the monkey that consumed the most amount of food
{
int row, col;
double max{};
for (row = 0; row < monkey; row++)
{
max = MAXf[0][0];
for (col = 0; col < DAYS; col++)
{
if (MAXf[row][col] > max)
{
max = MAXf[row][col];
subscriptD = col;
subscriptM = row;
}
}
}
return max;
}
Solution 1:[1]
That is also my problem, what I did is to change the max= MAXF[0][0] to max= MAXF[row][col].
It works for me, hope it helps :>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tyler2P |
