Thread: How can I add up how many ints are even in the same array??

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    12

    How can I add up how many ints are even in the same array??

    I'v only started to learn c++ this year so maybe this is just a simple problem.

    here is the question I was given:

    Write a function of the form int countEvens(int[], int) that returns the number of even ints in a given array, when passed the array and the number of elements in the array. Remember the % "mod" operator computes the remainder, e.g. 5 % 2 is 1.

    countEvens({2, 1, 2, 3, 4},5) → 3
    countEvens({2, 2, 0},3) → 3
    countEvens({1, 3, 5},3) → 0


    I passed the array and used the "mod" operator to identify which elements are even.

    my problem is that I don't know how to add up how many even elements are in the array. Can anyone help me?

    my code so far just displays which numbers in the array are even, I want to display how many are even. Here is my code

    Code:
     
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int countEvens(int a[], int size)
    {
        int i;
        
        for (i=0; i<size; i++)
        {
            if ((a[i]%2) == 0)
            {
               cout << a[i] << " is even" << endl;
            }   
        }   
        
    }    
    
    
    
    int main()
    {
        int array[5] = {2,3,4,5,2};
        countEvens(array,5);
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Obviously you need a counter variable (initialized to 0) that you increment by 1 each time you get an even number. At the end of the function you return the value of this variable. When you call the function you must use the return value somehow, either print it directly or store the return value in another variable in your main function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is 0 odd ... or even? Or both?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Elysia View Post
    Is 0 odd ... or even? Or both?
    Uhm, I guess you said it for a reason, but I have to say that:
    if n is zero or Natural

    n*2 is Even
    n*2 + 1 is Odd
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of Ints to a Strings
    By mattAU in forum C Programming
    Replies: 10
    Last Post: 08-17-2006, 05:25 AM
  2. array of ints
    By barim in forum C Programming
    Replies: 4
    Last Post: 01-01-2005, 05:21 AM
  3. need help with char and ints in an array
    By satory in forum C Programming
    Replies: 1
    Last Post: 12-05-2004, 01:41 PM
  4. INTs for array size
    By Granger9 in forum C Programming
    Replies: 3
    Last Post: 09-13-2002, 10:02 AM
  5. Array of ints
    By Tiger in forum C Programming
    Replies: 8
    Last Post: 10-01-2001, 12:17 PM