I am working on displaying the amount ofodd numbers in this array. It always says there is 5. That is not correct. When I use the debug it shows that it is placing a different value in the myArray and using that to determine the amount of odd numbers. Please someone take a look.

#include <iostream>
using std::cout;
using std::endl;

const int MAX = 10;
int myArray[MAX] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11};
int odd = 0;

int main()
{
	for (int i = 1; i < MAX; i++)
	{
		if ((i%2)==1)
		odd++;
	}
	cout << "Number of odd integers: " << odd << endl;

	return 0;
}

<< moderator edit: added code tags: [code][/code] >>

Recommended Answers

All 4 Replies

The first element of an array is at index zero. That is, myArray[0] contains 1, and myArray[10] contains 11. Right now, your for loop iterates i from 1 to 9, and you probably want to iterate from 0 to 9.

What's the point? Your code doesn't even look at the array at all...

What's the point? Your code doesn't even look at the array at all...

Oh man, I really missed that...

Just write "myArray%2" in the "if", because you are only checking to see if the iterator is odd or not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.