2d Array average not working?

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
When I compile and run the code, I get:


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 = 86
Average amount eaten during the week per day = 17.8667


??????

Note that your getLowest() function can be simplified as you don't need the extra array.

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
#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] {};

	eatingHabitsInfo(poundsOfFood);

	cout << "Lowest amount eaten during the week = "
		<< getLowest(poundsOfFood)
		<< endl;

	cout << "Average amount eaten during the week per day = "
		<< getAverage(poundsOfFood)
		<< endl;
}

void eatingHabitsInfo(double array[rows][columns])
{
	const 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) << ": \n";

		cout << '\n';
	}
}

double getLowest(double array[rows][columns])
{
	double lowestNumber {9999}; // A number higher than can be expected

	for (int row = 0; row < rows; row++) {
		double sum {};

		for (int column = 0; column < columns; column++)
			sum += array[row][column];

		if (sum < lowestNumber)
			lowestNumber = sum;
	}

	return lowestNumber;
}

double getAverage(double array[rows][columns])
{
	double columnsSum {};

	for (int row = 0; row < rows; row++)
		for (int column = 0; column < columns; column++)
			columnsSum += array[row][column];

	return columnsSum / (rows * columns);
}

Last edited on
Interesting wonder why I am not getting that I am using Visual Studio 2019 maybe it's something with that.


I just tried running on the c++ shell from this site this is what I get.

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 = 8.29559e-317
Average amount eaten during the week per day = 8.11737e-311
Last edited on
OK. The problem is in eatingHabitsInfo(). array isn't being initialised!

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
#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] {};

	eatingHabitsInfo(poundsOfFood);

	cout << "Lowest amount eaten during the week = "
		<< getLowest(poundsOfFood)
		<< endl;

	cout << "Average amount eaten during the week per day = "
		<< getAverage(poundsOfFood)
		<< endl;
}

void eatingHabitsInfo(double array[rows][columns])
{
	const 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) << ": \n";
			array[row][column] = monkeyInfo[row][column];
		}

		cout << '\n';
	}
}

double getLowest(double array[rows][columns])
{
	double lowestNumber {9999}; // A number higher than can be expected

	for (int row = 0; row < rows; row++) {
		double sum {};

		for (int column = 0; column < columns; column++)
			sum += array[row][column];

		if (sum < lowestNumber)
			lowestNumber = sum;
	}

	return lowestNumber;
}

double getAverage(double array[rows][columns])
{
	double columnsSum {};

	for (int row = 0; row < rows; row++)
		for (int column = 0; column < columns; column++)
			columnsSum += array[row][column];

	return columnsSum / (rows * columns);
}


It just 'happened' to work for me for my memory layout. Doh!

Last edited on
Thanks seeplus that's kind of what I thought was happening I am still trying to wrap my head around functions and arrays. Just saw what you did thanks!!!
Last edited on
You were getting the average etc of what ever happened to be in memory as poundsOfFood in main() wasn't being initialised when it was defined. It's good practice to always initialise a variable when it is defined - otherwise it contains whatever value happens to be at the used memory location at that time.
Thank you will try to remember that!!!
Topic archived. No new replies allowed.