0

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] >>

4
Contributors
4
Replies
5
Views
12 Years
Discussion Span
Last Post by The Senate
0

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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.