# array involving even numbers

• 07-08-2004
dantestwin
array involving even numbers
For this function, I have to count how many even numbers there are in this array. It compiles fine and I get no warnings but when I run it, nothing gets outputted! I'm baffled because I've done this function before and do not know why this is happening. As always any help is appreciated.

Code:

``` #include <iostream> using namespace std; int EVEN (int a[],int size );//prototype int main() {    const int size=10; int a[size]={1,2,3,4,5,6,7,8,9,10};  cout<<"The number of evens in this array are";         EVEN(a,size);//call   return 0; }  int EVEN(int a[], int size) //print number of even elements  {     int e=0;         for(int i=0;i<size;i++){         if((a[i]%2)==0)           e++;         return e;                                }     return 0;    }```
• 07-08-2004
Hammer
Start by fixing this bit:
Code:

```cout<<"The number of evens in this array are";         EVEN(a,size);//call```
You need to make that one statement, not two.
• 07-08-2004
Salem
Oh come on...

cout << EVEN(a,size);//call
• 07-08-2004
sean
And you shouldn't be returning something in the loop, otherwise it'll get through the first element of the array only.
• 07-08-2004
Prelude
>EVEN(a,size);//call
On a completely different note, this is a rather bad name for a function. First, it's all upper case, which is a well entrenched convention for macro and constant names. Second, it's not very informative. Even something like evenNumbers or numEvens or total_even_numbers would be better.
• 07-08-2004
dantestwin
Quote:

Originally Posted by Salem
Oh come on...

cout << EVEN(a,size);//call

Salem, I'm not really good with programming and I've been staring at code for hours straight. Cut me some slack. Just because you're a programming expert doesn't give you the right to be rude.
• 07-08-2004
dantestwin
You're right Sean, it just prints out to 0. But i have to return the number of evens in the array and i've done this before and somehow made it work but that was over a year ago. I just thought a return statement was an easy way to do it.
• 07-08-2004
sean
Try this

Code:

```int e=0;     for(int i=0;i<size;i++)     {           if((a[i]%2)==0) e++;      } return e;```
edit: The reasoning is just the placement of your return statement. That is not only the simplest, but IMO the best, so good choice. It's just that you only wanted to return it AFTER your loop was finished, since you can only return one thing, and you can't do anything after you do it (hence, you don't need return 0 as well).
• 07-08-2004
dantestwin
Hmmm. It worked Sean! I just don't see how removing the return statement from the for loop body would cause such a difference. Thanks. Thanks Prelude as well, I dont usually name my functions in all caps, its been a longggg day.

Oh ok Sean. That makes sense to me now. Thanks a lot.
• 07-08-2004
Hunter2
You should get into the habit of making your code neater. That bug would have been pretty quick to spot if all the opening braces and closing braces were lined up and everything.
• 07-08-2004
dantestwin
I usually do but I'm using visual c++ which has a funky autoformat thing happening.
• 07-08-2004
Hunter2
Actually, I personally find the autoformat to be quite helpful- you don't have to do the tabbing manually.
• 07-08-2004
sean
I'm with Hunter2. If it's anything like the autoformat of VS.NET - I love it. It's so intuitive that tou don't even have to think about it - it leaves you free to just sit and code.
• 07-10-2004
Prelude
>I usually do but I'm using visual c++ which has a funky autoformat thing happening.
I would wager that you're using tabs instead of spaces. Under the editor options you should always opt to replace tabs with spaces so that the format is consistent regardless of the text editor being used. You can also change the size of a tab (ie. the number of spaces it gives you). I prefer 2 because I write a lot of code without the aid of autoindention and I believe that it's just as easy to read as 4, but most think that 4 is more readable.

The smart formatting of VC++ is pretty good (if annoying at times), and shouldn't give you those wacky results unless you're using real tabs. ;)