DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Need hellp with my array (http://www.daniweb.com/forums/thread29295.html)

mav1 Jul 28th, 2005 11:02 pm
Need hellp with my array
 
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] >>

Rashakil Fol Jul 28th, 2005 11:32 pm
Re: Need hellp with my array
 
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.

winbatch Jul 28th, 2005 11:44 pm
Re: Need hellp with my array
 
What's the point? Your code doesn't even look at the array at all...

Rashakil Fol Jul 28th, 2005 11:55 pm
Re: Need hellp with my array
 
Quote:

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

Oh man, I really missed that...

The Senate Jul 29th, 2005 6:32 am
Re: Need hellp with my array
 
Just write "myArray[i]%2" in the "if", because you are only checking to see if the iterator is odd or not.


All times are GMT -4. The time now is 4:53 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC