Thread: array involving even numbers

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    61

    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;
     
     }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Oh come on...

    cout << EVEN(a,size);//call
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    And you shouldn't be returning something in the loop, otherwise it'll get through the first element of the array only.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    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.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    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.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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).
    Last edited by sean; 07-08-2004 at 04:08 PM.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    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.
    Last edited by dantestwin; 07-08-2004 at 04:11 PM.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    I usually do but I'm using visual c++ which has a funky autoformat thing happening.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Actually, I personally find the autoformat to be quite helpful- you don't have to do the tabbing manually.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To Find Largest Consecutive Sum Of Numbers In 1D Array
    By chottachatri in forum C Programming
    Replies: 22
    Last Post: 07-10-2011, 01:43 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. getting numbers in an array
    By ct26torr in forum C Programming
    Replies: 6
    Last Post: 03-04-2003, 10:31 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM