Thread: counting even numbers

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    18

    counting even numbers

    ok... I am very new to C++... I need to write a function that will determine whether or not each number included in an array of random numbers is even, and then return the total amount of even numbers. I'm sure this is very easy, I just can't seem to figure it out... any help would be greatly appreciated!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Loop through the array, for every number test if that number modulus 2 is equal to 0, if so then it is even and you increment a variable.
    "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
    Registered User
    Join Date
    Apr 2002
    Posts
    18
    how to make the program determine wheter or not the number is even... the program that I am writing asks the user to enter how many random numbers need to be generated, and the part that I am having a problem with is writing a function to count how many of those numbers are even.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    18
    Originally posted by hk_mp5kpdw
    Loop through the array, for every number test if that number modulus 2 is equal to 0, if so then it is even and you increment a variable.
    how do I test if "the number modulus 2 is equal to 0" that is the problem I am having

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    if( number % 2 == 0 ) iNumEvens++;
    "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

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    18
    thank you... like I said... I am very new to this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Counting occurrence of numbers in C
    By stabule in forum C Programming
    Replies: 1
    Last Post: 11-13-2005, 01:55 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. Counting Page Numbers
    By dayknight in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2002, 06:52 AM