How to get rid of global variable from an existing code?

How to get rid of global variable from an existing code?
I've written a code and it works. But now I have to get rid of (don't use) the global variable from the existing code and still get the same result.
How can I do that?

My code looks like this...

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
#include <iostream>

using namespace std;

const int numRows = 3;
const int numCols = 5;

void getData(int array[][numCols], int);
double getAvg(int array[][numCols], int);
int getRowSum(int array[][numCols], int);
double getAvg(int array[][numCols], int);
int getSmall(int, int, int);
int getLarge(int, int, int);

int main()
{
	int monkeys[numRows][numCols];
	int monkey1 = 0, monkey2 = 1, monkey3 = 2, monk1Tot, monk2Tot, monk3Tot, small, large;

	getData(monkeys, numRows);

	monk1Tot = getRowSum(monkeys, monkey1);
	monk2Tot = getRowSum(monkeys, monkey2);
	monk3Tot = getRowSum(monkeys, monkey3);

	small = getSmall(monk1Tot, monk2Tot, monk3Tot);
	large = getLarge(monk1Tot, monk2Tot, monk3Tot);

	cout << "Average amount of food eaten per day by the whole family of monkeys is... " << getAvg(monkeys, numRows) << ". \n" << endl;
	cout << "The least amount of food eaten during the week by any one monkey is... " << small << ". \n" << endl;
	cout << "The greatest amount of food eaten during the week by any one monkey is... " << large << ". \n" << endl;

}

void getData(int monkeys[][numCols], int numRows)
{
	for (int rows = 0; rows < numRows; rows++)
	{
		cout << "Monkey " << (rows + 1) << "\n";
		for (int cols = 0; cols < numCols; cols++)
		{
			cout << " Day " << (cols + 1) << ": ";
			cin >> monkeys[rows][cols];

			while (monkeys[rows][cols] < 0)
			{
				cout << " You need to enter a postivie number. ";
				cin >> monkeys[rows][cols];
			}
		}

		cout << endl;
	}
}

int getRowSum(int monkeys[][numCols], int monkeyNum)
{
	int total = 0;

	for (int cols = 0; cols < numCols; cols++)
		total += monkeys[monkeyNum][cols];

	return total;
}

double getAvg(int monkeys[][numCols], int numRows)
{
	double total = 0;

	for (int cols = 0; cols < numCols; cols++)
	{
		for (int rows = 0; rows < numRows; rows++)
			total += monkeys[rows][cols];
	}

	return (total / (numCols));
}

int getMost(int monkey1, int monkey2, int monkey3)
{
	int array[3]{ monkey1, monkey2, monkey3 };
	int high = array[0];

	for (int count = 0; count < 3; count++)
	{
		if (array[count] > high)
		{
			high = array[count];
		}
	}
	return high;
}

int getSmall(int monkey1, int monkey2, int monkey3)
{
	int array[3]{ monkey1, monkey2, monkey3 };
	int low = array[0];

	for (int count = 0; count < 3; count++)
	{
		if (array[count] < low)
		{
			low = array[count];
		}
	}
	return low;
}
Global constants are lesser evil. They are sometimes unavoidable.

In your case it is better to either replace arrays (which should rarely be used) with containers which know their own size or to do some template magic:
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 <algorithm>
#include <iostream>

template <typename T, size_t R, size_t C>
void getData(T (&monkeys)[R][C])
{
    for (size_t row = 0; row < R; ++row) {
        std::cout << "Monkey " << (row + 1) << '\n';
        for (size_t col = 0; col < C; ++col) {
            std::cout << " Day " << (col + 1) << ": ";
            std::cin >> monkeys[row][col];
            while (monkeys[row][col] < 0)   {
                std::cout << " You need to enter a postivie number. ";
                std::cin >> monkeys[row][col];
            }
        }
        std::cout << '\n';
    }
}

template <typename T, size_t R, size_t C>
int getRowSum(T (&monkeys)[R][C], int monkeyNum)
{
    int total = 0;
    for (size_t col = 0; col < C; ++col)
        total += monkeys[monkeyNum][col];
    return total;
}

template <typename T, size_t R, size_t C>
double getAvg(T (&monkeys)[R][C])
{
    double total = 0;
    for (size_t row = 0; row < R; ++row)
        total += getRowSum(monkeys, row);
    return (total / C);
}

inline int getLarge(int monkey1, int monkey2, int monkey3)
{
    return std::max({monkey1, monkey2, monkey3});
}

int getSmall(int monkey1, int monkey2, int monkey3)
{
    return std::min({monkey1, monkey2, monkey3});
}

int main()
{
    constexpr int numRows = 3;
    constexpr int numCols = 5;
    int monkeys[numRows][numCols];
    int monkey1 = 0, monkey2 = 1, monkey3 = 2, monk1Tot, monk2Tot, monk3Tot, small, large;

    getData(monkeys);

    monk1Tot = getRowSum(monkeys, monkey1);
    monk2Tot = getRowSum(monkeys, monkey2);
    monk3Tot = getRowSum(monkeys, monkey3);

    small = getSmall(monk1Tot, monk2Tot, monk3Tot);
    large = getLarge(monk1Tot, monk2Tot, monk3Tot);

    std::cout << "Average amount of food eaten per day by the whole family of monkeys is... " << 
                 getAvg(monkeys) << ". \n\n";
    std::cout << "The least amount of food eaten during the week by any one monkey is... " << 
                 small << ". \n\n";
    std::cout << "The greatest amount of food eaten during the week by any one monkey is... " << 
                 large << ". \n\n";
}




closed account (48T7M4Gy)
And if you want to stay with arrays:

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
#include <iostream>

using namespace std;

void getData(int**, int, int);

int main()
{   
   const int numRows = 3;
   const int numCols = 5;

   int** monkeys = new int*[numRows];
   for (int i = 0; i < numRows; i++)
      monkeys[i] = new int[numCols];
   //Don't forget the corresponding deletes when finished

   getData(monkeys, numRows, numCols);
}

void getData(int** monkeys, int numRows, int numCols)
{
   for (int rows = 0; rows < numRows; rows++)
   {
      cout << "Monkey " << (rows + 1) << "\n";
      for (int cols = 0; cols < numCols; cols++)
      {
         cout << " Day " << (cols + 1) << ": ";
         cin >> monkeys[rows][cols];

         while (monkeys[rows][cols] < 0)
         {
            cout << " You need to enter a postivie number. ";
            cin >> monkeys[rows][cols];
         }
     }

      cout << endl;
   }
}
Last edited on
THANK YOU!!!!

@kenmort & @MiiNiPaa
Topic archived. No new replies allowed.