Finding lowest/highest values in 2D array

Hey guys, any idea as to how to find the lowest value and the highest value in this 2D array? I made the functions to find the lowest and highest, but all im getting on execution is a 1 on both, no matter what number i input.

Here's my code
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
#include <cstdlib>
#include <iostream>

using namespace std;

const int MONKEYS = 3;
const int DAYS = 7;
double average = 0;

int getSum (int [][DAYS]);
void getChart (int [][DAYS]);
void dayAvg (int table [][DAYS]);
int findLowest (int table [][DAYS]);
int findHighest (int table [][DAYS]);

int main(int argc, char *argv[])
{
    int table [MONKEYS][DAYS];
    
    cout << "The diet of 3 monkeys over 7 days, please input your info: \n\n";
    
    getChart(table);
    
    cout << "The total amount of food consumed by the monkeys: "
         << getSum(table) << " Pounds\n\n";
         
    dayAvg(table);
    
    cout << "The lowest amount of food eaten was: " << findLowest(table) << " Pounds\n";
    cout << "The highest amount of food eaten was: " << findHighest(table) << " Pounds\n";
    
    //ofstream outData;
    //outData.open("results.txt");
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

int getSum (int table [][DAYS])
{
    int sum = 0;
    for ( int monkey = 0;  monkey < MONKEYS; monkey++)
    {
        for (int day = 0; day < DAYS; day++)
            sum += table [monkey][day];
    }
    return sum;
}

void getChart (int table[][DAYS])
{
    for (int monkey = 0; monkey < MONKEYS; monkey++)
    {
        for (int day = 0; day < DAYS; day++)
        {
            cout << "Monkey " << (monkey+1) << ", ";
            cout << "Day " << (day+1) << ": ";
            cin >> table [monkey][day];
        }
        cout << endl;
    }
}

void dayAvg (int table [][DAYS])
{
    for ( int day = 0;  day < DAYS; day++)
    {
        int total = 0;
        for (int monkey = 0; monkey < MONKEYS; monkey++)
        {
            total += table [monkey][day];
        }
        average = total/MONKEYS;
        cout << "Average food consumed on day " << (day+1)
             << " by all 3 monkeys is: " << average << " Pounds" << endl;
    }
}

int findLowest (int table [][DAYS])
{
    int lowest = table [MONKEYS][DAYS];
    for (int monkey = 0; monkey < MONKEYS; monkey++)
    {
        for (int day = 0; day < DAYS; day++)
        {
            if (table [monkey][day] < lowest)
                lowest = table[MONKEYS][DAYS];
        }
        
        cout << endl;
    }
    return lowest;
}

int findHighest (int table [][DAYS])
{
    int highest = table [MONKEYS][DAYS];
    for (int monkey = 0; monkey < MONKEYS; monkey++)
    {
        for (int day = 0; day < DAYS; day++)
        {
            if (table [monkey][day] > highest)
                highest = table[MONKEYS][DAYS];
        }
        
        cout << endl;
    }
    return highest;
}
the same problems in both functions.
1. element table[MONKEYS][DAYS] does not exist. when you declare an array arr[constant], the elements are named form arr[0] to arr[constant-1];
2. MONKEYS and DAYS are constants. when you find that some element of table is lower than lowest, you set lowest to the same value every time. You should set it to table[monkey][day].
3. see http://www.cplusplus.com/forum/articles/17108/
But when i try to do that, it says that [monkey] and [day] are not initialized
halopower67, you already have if (table [monkey][day] > highest) so you should be able to use highest = table[monkey][day];.....
This is what im having problems with, when i apply the lowest = table[monkey][day]; to my code, it doesn't work for finding the lowest, but it works for finding the highest. Heres my complete code, the problem is that it is still not finding the lowest:
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
#include <cstdlib>
#include <iostream>

using namespace std;

const int MONKEYS = 3;
const int DAYS = 7;
double average = 0;

int getSum (int [][DAYS]);
void getChart (int [][DAYS]);
void dayAvg (int table [][DAYS]);
int findLowest (int table [][DAYS]);
int findHighest (int table [][DAYS]);

int main(int argc, char *argv[])
{
    int table [MONKEYS][DAYS];
    
    cout << "The diet of 3 monkeys over 7 days, please input your info: \n\n";
    
    getChart(table);
    
    cout << "The total amount of food consumed by the monkeys: "
         << getSum(table) << " Pounds\n\n";
         
    dayAvg(table);
    
    cout << "The lowest amount of food eaten : " << findLowest(table) << " Pounds\n";
    cout << "The highest amount of food eaten : " << findHighest(table) << " Pounds\n";
    
    //ofstream outData;
    //outData.open("results.txt");
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

int getSum (int table [][DAYS])
{
    int sum = 0;
    for ( int monkey = 0;  monkey < MONKEYS; monkey++)
    {
        for (int day = 0; day < DAYS; day++)
            sum += table [monkey][day];
    }
    return sum;
}

void getChart (int table[][DAYS])
{
    for (int monkey = 0; monkey < MONKEYS; monkey++)
    {
        for (int day = 0; day < DAYS; day++)
        {
            cout << "Monkey " << (monkey+1) << ", ";
            cout << "Day " << (day+1) << ": ";
            cin >> table [monkey][day];
        }
        cout << endl;
    }
}

void dayAvg (int table [][DAYS])
{
    for ( int day = 0;  day < DAYS; day++)
    {
        int total = 0;
        for (int monkey = 0; monkey < MONKEYS; monkey++)
        {
            total += table [monkey][day];
        }
        average = total/MONKEYS;
        cout << "Average food consumed on day " << (day+1)
             << " by all 3 monkeys is: " << average << " Pounds" << endl;
    }
}

int findLowest (int table [][DAYS])
{
    int lowest = table [MONKEYS][DAYS];
    for (int monkey = 0; monkey < MONKEYS; monkey++)
    {
        for (int day = 0; day < DAYS; day++)
        {
            if (table [monkey][day] < lowest)
                lowest = table[monkey][day];
        }
        
        cout << endl;
    }
    return lowest;
}

int findHighest (int table [][DAYS])
{
    int highest = table [MONKEYS][DAYS];
    for (int monkey = 0; monkey < MONKEYS; monkey++)
    {
        for (int day = 0; day < DAYS; day++)
        {
            if (table [monkey][day] > highest)
                highest = table[monkey][day];
        }
        
        cout << endl;
    }
    return highest;
}
as I said, table[MONKEYS][DAYS] is out of bounds. its value is undefined. use table[0][0] here instead. do this for both functions.
Also, now that this is working, how could i write all this information to a file? could it be done with one function? or would it have to be implemented on every function i do?
for a file, firstly open it. then replace every cout with the fstream object. You'll either have to make that object global or pass it to every function that needs it (which would generally be preferred).
This is what i did for the writing to a file but the thing is that its not working for the void functions, any clues?
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
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

const int MONKEYS = 3;
const int DAYS = 7;
double average = 0;

int getSum (int [][DAYS]);
void getChart (int [][DAYS]);
void dayAvg (int table [][DAYS]);
int findLowest (int table [][DAYS]);
int findHighest (int table [][DAYS]);

int main(int argc, char *argv[])
{
    int table [MONKEYS][DAYS];
    ofstream outfile;
    
    cout << "The diet of 3 monkeys over 7 days, please input your info: \n\n";
    
    outfile.open("MonkeyBuisnessFile.txt");
    
    getChart(table);
    
    cout << "The total amount of food consumed by the monkeys: "
         << getSum(table) << " Pounds\n\n";
    outfile << "The total amount of food consumed by the monkeys: "
         << getSum(table) << " Pounds\n\n";
         
    dayAvg(table);
    
    cout << "The lowest amount of food eaten : " << findLowest(table) << " Pounds\n";
    outfile << "The lowest amount of food eaten : " << findLowest(table) << " Pounds\n";
    cout << "The highest amount of food eaten : " << findHighest(table) << " Pounds\n";
    outfile << "The highest amount of food eaten : " << findHighest(table) << " Pounds\n";
    
    outfile.close();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

int getSum (int table [][DAYS])
{
    int sum = 0;
    for ( int monkey = 0;  monkey < MONKEYS; monkey++)
    {
        for (int day = 0; day < DAYS; day++)
            sum += table [monkey][day];
    }
    return sum;
}

void getChart (int table[][DAYS])
{

    for (int monkey = 0; monkey < MONKEYS; monkey++)
    {
        for (int day = 0; day < DAYS; day++)
        {
            cout << "Monkey " << (monkey+1) << ", ";
            cout << "Day " << (day+1) << ": ";
            cin >> table [monkey][day];
        }
        cout << endl;
    }
}

void dayAvg (int table [][DAYS])
{
    for ( int day = 0;  day < DAYS; day++)
    {
        int total = 0;
        for (int monkey = 0; monkey < MONKEYS; monkey++)
        {
            total += table [monkey][day];
        }
        average = total/MONKEYS;
        cout << "Average food consumed on day " << (day+1)
             << " by all 3 monkeys is: " << average << " Pounds" << endl;
    }
}

int findLowest (int table [][DAYS])
{
    int lowest = table [0][0];
    for (int monkey = 0; monkey < MONKEYS; monkey++)
    {
        for (int day = 0; day < DAYS; day++)
        {
            if (table [monkey][day] < lowest)
                lowest = table[monkey][day];
        }
        
        cout << endl;
    }
    return lowest;
}

int findHighest (int table [][DAYS])
{
    int highest = table [0][0];
    for (int monkey = 0; monkey < MONKEYS; monkey++)
    {
        for (int day = 0; day < DAYS; day++)
        {
            if (table [monkey][day] > highest)
                highest = table[monkey][day];
        }
        
        cout << endl;
    }
    return highest;
}
I wrote:
then replace every cout with the fstream object
In your functions you write to cout, so you don't write to a file. replace cout with outfile. Do do so either make outfile global or pass it into the functions as a parameter.
Last edited on
Topic archived. No new replies allowed.