Identifying Even Numbers In An Array

My assignment is to create an array of 25 random integers, ranging 1-100. It also has to tell me how many how many numbers (EVEN only) that are printed in the random array. I've done most of the work, but my program keeps saying that I have 0 even numbers. Any suggestions?

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

using namespace std;

void printArray(const int a[],int size);
void fillArray(int a[], int size);
int countEvens;

int main()
{
	srand((unsigned int)time(0));
	int ar[25];
	fillArray(ar, 25);
	printArray(ar, 25);

	return 0;
}
void fillArray(int a[], int size)
{
	for (int i = 0; i < size; i++)
		a[i] = rand() % 101;
}
void printArray(const int a[],int size)
{
	for (int i = 0; i < size; i += 1)
		cout << a[i] << endl;
	{
		for (int i = 1; i <= 25; i++)

			i = i + 1;
		cout << "There are " << countEvens << " even numbers in the array." << endl;
		}
Well. I dont see you counting the amount of even numbers anywhere. And you dont even use the int countEvens; variable anywhere either.

If you want to count even/odd numbers, you use the modulus operator (%).

Like this

1
2
3
4
5
6
7
for (int i = 1; i <= 25; i++)
{
          if(a[i] % 2 == 0) // If you want to count odd, you replace 0 with 1.
          {
	          countEvens++;
	  }
}


Also, remove the i = i + 1;

www.cprogramming.com/tutorial/modulus.html <-- why is this link not blue and clickable?
Last edited on
I didn't notice that I deleted the variable for int countEvens;
I realize with programming, you must pay attention to every single detail. Thank you for your help :)
I realize with programming, you must pay attention to every single detail.


For sure :p

Happy coding!
Topic archived. No new replies allowed.