I can't figure out why I am getting ambiguous numbers on average and lowest numbers. I have no errors but output is in correct any push in right direction appreciated.
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
|
#include <iostream>
#include <iomanip>
using namespace std;
const int rows = 3,
columns = 5;
void eatingHabitsInfo(double array[rows][columns]);
double getAverage(double array[rows][columns]);
double getLowest(double array[rows][columns]);
int main()
{
double poundsOfFood[rows][columns];
double averagePerDay;
double lowestPerWeek;
eatingHabitsInfo(poundsOfFood);
lowestPerWeek = getLowest(poundsOfFood);
cout << "Lowest amount eaten during the week = "
<< lowestPerWeek
<< endl;
averagePerDay = getAverage(poundsOfFood);
cout << "Average amount eaten during the week per day = "
<< averagePerDay
<< endl;
return 0;
}
void eatingHabitsInfo(double array[rows][columns])
{
double monkeyInfo[rows][columns]{
{ 20,15,18,17,16 },
{ 19,18,17,18,19 },
{ 21,19,16,18,17 } };
for (int row = 0; row < rows; row++)
{
for (int column = 0; column < columns; column++)
{
cout << "Monkey #" << (row + 1) << " ate " << monkeyInfo[row][column] << ", on day " << (column + 1) << ": ";
cout << endl;
}
cout << endl;
}
}
double getLowest(double array[rows][columns])
{
double sum,
localArray[rows];
for (int row = 0; row < rows; row++)
{
sum = 0;
for (int column = 0; column < columns; column++)
sum += array[row][column];
localArray[row] = sum;
}
double lowestNumber = localArray[0];
for (int number = 1; number < rows; number++)
{
if (localArray[number] <= lowestNumber)
lowestNumber = localArray[number];
}
return lowestNumber;
}
double getAverage(double array[][columns])
{
double columnsSum{};
int totalElements = rows * columns;
for (int row = 0; row < rows; row++)
{
for (int column = 0; column < columns; column++)
columnsSum += array[row][column];
}
return columnsSum / totalElements;
}
|
Monkey #1 ate 20, on day 1:
Monkey #1 ate 15, on day 2:
Monkey #1 ate 18, on day 3:
Monkey #1 ate 17, on day 4:
Monkey #1 ate 16, on day 5:
Monkey #2 ate 19, on day 1:
Monkey #2 ate 18, on day 2:
Monkey #2 ate 17, on day 3:
Monkey #2 ate 18, on day 4:
Monkey #2 ate 19, on day 5:
Monkey #3 ate 21, on day 1:
Monkey #3 ate 19, on day 2:
Monkey #3 ate 16, on day 3:
Monkey #3 ate 18, on day 4:
Monkey #3 ate 17, on day 5:
Lowest amount eaten during the week = -4.62798e+62
Average amount eaten during the week per day = -9.25596e+61