Check for even values in array

I'm trying to write a simple program that checks the data in an array and tells me how many even values I have, this is what I have so far. When I run the program how it is currently written it continues to count up without end. Any help would be great.

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
 #include "stdafx.h"

#include <iostream>
#include <fstream>
using namespace std;

#define MAX_ROWS 3
#define MAX_COLUMNS 2
	int A[MAX_ROWS][MAX_COLUMNS] = {3, 2, 4, 5, 2, 2};
	int B, C, D;
int _tmain(int argc, _TCHAR* argv[])
	{
	for (B = 0; B < 3; B++)
	{
		for (C = 0; C < 2; C++)
		{

			for (A[B][C] % 2 == 0; D++;)
				cout <<"There are "<< D <<" even numbers"<< '\n';
			
		}
	}
	
	

	return 0;
}
Why are you declaring a two dimensional array but initializing it with a one dimensional array? Do you want to check an array with two dimensions or one dimension?

Also- I haven't run the code myself but I doubt you need a triple nested for loop to search an array with only two dimensions.
Last edited on
With two and I just caught that my self and I believe I have fixed it with this.
int A[MAX_ROWS][MAX_COLUMNS] = { { 3, 2 }, { 4, 5 }, { 2, 2 } };
I'm thinking the correct way to get this done is a if statement, but it still isn't doing what I want. it is still just counting up from 0 to 5 here is my if statement.
1
2
3
4
5
			if (A[B][C]%2 == 0)
			{
				D++;
			}
			cout <<"There are "<< D <<" even numbers"<< '\n';
Thanks guys I got it, I had my cout statement in the wrong place.
Topic archived. No new replies allowed.