Calculating stats in a 2D Array

Hello!

I'm currently trying to understand the usage of two-dimensional arrays and am looking for some help in how to average out certain totals for specific lines and columns.

For example, let's say I wanted to find the highest number in a row, or find a the average of every number in the array, what is the first step in that which would help me solve it? Any and all feedback is welcome. Below is my code that I'm currently working on.

Many thanks in advance,
-CC
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
#include <iostream>

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Top NBA Player Points Scored in Last 5 Games" << std::endl;
    
    const int rows = 5;
    const int columns = 5;
    int total = 0;
    double average = 0.0;
    int Points [5][5] = {{54, 57, 57, 44, 40},
                                 {31, 28, 23, 28, 21},
                                 {15, 16, 40, 37, 40},
                                 {27, 16, 26, 23, 36},
                                 {24, 20, 40, 26, 12}};
    for (int r = 0; r < rows; ++r){
        std::cout << "Player  " << r+1 << ": ";
        for (int c = 0; c < columns; ++c){
            std::cout << Points [r][c] << " ";
            total += Points [r][c];
    }
        average = total / columns;
        std::cout << "Average: " << average << std::endl;
        total = 0;
        average = 0.0;
        
}
    
    
    return 0;
}
Couple things, if you have const ints that can be used for your array, put them in there
int Points[rows][columns];
Try not to use what's called "magic numbers".

So far your loop is setup fine, but if you want your average to have a decimal point, you'll have to type cast total and columns as such.
average = (double)total / (double)columns;

If you want to keep track of your highest number for the row, make 2 int variables.
int highest = 0, temp = 0;

Copy Points[r][c] into temp, then check per a conditional statement
1
2
if (highest < temp)
    highest = temp;


and reset highest on the end of the outer loop.
Last edited on
Hello ContraCode92,

Welcome to the forum.

If you do not mind some general comments that I hope you find useful.

I do not see the line using namespace std;. This is good you are better off not using it and you do qualify what is in the standard name space. Better to learn this early in small pieces than later all at once.

Line 3. If you are not going to use "argc" and "argv" they do not have to be there unless you have something in mind that you have not coded yet.

Lines 7 and 8. Generally I put my constant variables above main as global variables. Pretty much the only variables I make global. I would suggest writing them as:constexpr int MAXROWS{ 5 }; and constexpr int MAXCOLS{ 5 };. The capital letters help to remember that this variable is defined as a constant and can not be changes. This is a little more descriptive of what it is fore and as a global variable it is available to the whole file.

For lines 9, 10 and 11 you have defined and initialized your variables well. If by chance your compiler is up to the C++11 standards or after you can use empty {}s known as the uniform initializer. This will set an "int" to zero and a double to 0.0 or you can put any number you need between the {}s as seen in the above example.

The for loop as with main it makes it much easier to fine the opening brace if you press enter and type the opening on the next line. When the {}s line up in the same column it makes it easier to find one that is missing esepically when have three or four lines of closing braces in different columns. If you indented correctly it is easy to line the columns up to find the missing brace. And it makes the code easier to read.

The for loops are good and work fine. Once I rearranged the code and added a couple of blank lines it became clearer what the for loops are doing.

My last suggestion is in the code that follows just above the return. Many people use an IDE like Visual Studio to work on their program or other IDEs these tend to open a window to run the program and when return is executed the window closes. This is a way to keep the window open to have a chance to read what is on the screen.

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

int main(int argc, const char * argv[])
{
	// insert code here...
	std::cout << "\nTop NBA Player Points Scored in Last 5 Games\n" << std::endl;  // <--- Changed. Added '\n's.

	const int rows = 5;
	const int columns = 5;
	int total = 0;
	double average = 0.0;
	int Points[5][5] =
	{
		{ 54, 57, 57, 44, 40 },
		{ 31, 28, 23, 28, 21 },
		{ 15, 16, 40, 37, 40 },
		{ 27, 16, 26, 23, 36 },
		{ 24, 20, 40, 26, 12 }
	};

	for (int r = 0; r < rows; ++r)
	{
		std::cout << "Player  " << r + 1 << ": ";

		for (int c = 0; c < columns; ++c)
		{
			std::cout << Points[r][c] << " ";
			total += Points[r][c];
		}

		average = total / columns;
		std::cout << "Average: " << average << std::endl;
		total = 0;
		average = 0.0;
	}

	// This next line may or may not be needed. If you have to press "Enter" to see the ptompt you do not
	// need this next line.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue";
	std::cin.get();

	return 0;
}


Hope that helps,

Andy
Topic archived. No new replies allowed.