Finding the Average of a 2-D Array

My problem is as follows:

A researcher has been given a grayscale image as a 2D array of floating point numbers.

In a grayscale image each pixel value represents the brightness of the pixel.

The image is encoded as a C++ array here:

double image[8][8] = {
{0.7, 0.8, 1.0, 0.5, 0.5, 0.0, 1.0, 0.5},
{0.7, 0.5, 1.0, 0.5, 0.6, 1.0, 0.8, 0.6},
{0.5, 0.1, 1.0, 0.1, 0.1, 1.0, 0.3, 0.7},
{0.2, 0.0, 1.0, 0.0, 0.7, 1.0, 0.0, 0.4},
{0.2, 1.0, 0.0, 0.2, 1.0, 0.2, 0.2, 0.0},
{0.6, 1.0, 0.1, 0.9, 0.4, 0.6, 1.0, 0.3},
{0.9, 1.0, 1.0, 1.0, 1.0, 1.0, 0.2, 1.0},
{0.9, 0.0, 1.0, 0.1, 0.3, 0.3, 0.6, 0.7}
};
This researcher wishes to reduce this data to a simpler measurement, such as the average value of the pixels.

The Problem:

Write a program that prints the average pixel brightness of this array. Use two decimal places of precision.

_____________________________________________________________________________

The code I have created thus far is below. It is running, but I know that the answer for the average should be 0.57 and my program currently prints 0.01.

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
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
	int i = 0;
	int j = 0;
	double sum = 0;
	double image[8][8] = {
		{ 0.7, 0.8, 1.0, 0.5, 0.5, 0.0, 1.0, 0.5 },
		{ 0.7, 0.5, 1.0, 0.5, 0.6, 1.0, 0.8, 0.6 },
		{ 0.5, 0.1, 1.0, 0.1, 0.1, 1.0, 0.3, 0.7 },
		{ 0.2, 0.0, 1.0, 0.0, 0.7, 1.0, 0.0, 0.4 },
		{ 0.2, 1.0, 0.0, 0.2, 1.0, 0.2, 0.2, 0.0 },
		{ 0.6, 1.0, 0.1, 0.9, 0.4, 0.6, 1.0, 0.3 },
		{ 0.9, 1.0, 1.0, 1.0, 1.0, 1.0, 0.2, 1.0 },
		{ 0.9, 0.0, 1.0, 0.1, 0.3, 0.3, 0.6, 0.7 }
	};

	for (i = 0; i < image[i][j]; i++) {
		for (j = 0; j < image[i][j]; j++) {
			sum += image[i][j];
	}
}

	cout << fixed << setprecision(2) << (sum / 64) << endl;


	system("pause");
	return 0;
}
How did you came up with those loop conditions?
I assume you are talking about the "i < image[i][j]" part? Honestly, that is the part that is causing me grief-- so far, those conditions are the only ones I have tried that returns something other than 0.00. I had originally had i < 64, but that was no use. I have tried quite a few other values here, but can't seem to get the loop conditions right.
Try 8
Haha, oh my goodness, just tried this, it worked, then saw your message. Thank you! I have both for loops for rows and columns in 2D but am still stuck treating the for loop as 1D array:/ I appreciate your help with this stupid mistake
Ok. Plain "8" or "64" is often called a "magic constant". It is not always easy to see why they are there and whether one "8" relates to an another "8". If you ever have to change such value, which constants need to be changed?

A solution is to use "named constant". Likewise, variable names could hint about their purpose. For example:
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
#include <iostream>
#include <iomanip>

int main() {
	constexpr size_t ROWS = 8; // size_t is an unsigned integer
	constexpr size_t COLS = 8; // indices are 0 or positive, are they not?

	double image[ROWS][COLS] = {
		{ 0.7, 0.8, 1.0, 0.5, 0.5, 0.0, 1.0, 0.5 },
		{ 0.7, 0.5, 1.0, 0.5, 0.6, 1.0, 0.8, 0.6 },
		{ 0.5, 0.1, 1.0, 0.1, 0.1, 1.0, 0.3, 0.7 },
		{ 0.2, 0.0, 1.0, 0.0, 0.7, 1.0, 0.0, 0.4 },
		{ 0.2, 1.0, 0.0, 0.2, 1.0, 0.2, 0.2, 0.0 },
		{ 0.6, 1.0, 0.1, 0.9, 0.4, 0.6, 1.0, 0.3 },
		{ 0.9, 1.0, 1.0, 1.0, 1.0, 1.0, 0.2, 1.0 },
		{ 0.9, 0.0, 1.0, 0.1, 0.3, 0.3, 0.6, 0.7 }
	};

	double sum = 0; // introduce variables where they are needed; not too early
	for ( size_t row = 0; row < ROWS; ++row ) {
		for ( size_t col = 0; col < COLS; ++col ) {
			sum += image[ row ][ col ];
		}
	}
	std::cout << std::fixed << std::setprecision(2) << (sum / (ROWS*COLS)) << '\n';

	return 0;
}
Topic archived. No new replies allowed.