Find the number of even numbers on even positions

This is a discussion on Find the number of even numbers on even positions within the C++ Programming forums, part of the General Programming Boards category; Display the number of even numbers on even positions in the sequence. Input 5 1 2 4 5 6 Output ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    1

    Question Find the number of even numbers on even positions

    Display the number of even numbers on even positions in the sequence.

    Input
    5
    1 2 4 5 6



    Output
    2

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    You're not expecting us to do your assignment for you, are you? Because we won't.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    What are you having problems with?
    Just iterate through the indices 0, 2, 4, 6... and check if the number at this position is even or not (using modulo).

    You need 2 variables - counter and position. If position is odd, ignore current number, otherwise check if the current number is even and increment counter.
    I never put signature, but I decided to make an exception.

  4. #4
    Registered User bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    Posts
    87
    Quote Originally Posted by kmdv View Post
    What are you having problems with?
    Just iterate through the indices 0, 2, 4, 6... and check if the number at this position is even or not (using modulo).

    You need 2 variables - counter and position. If position is odd, ignore current number, otherwise check if the current number is even and increment counter.
    its easier to input them into an array, then move through them with a double incrementing for loop
    Code:
    for(int a = 0; a < array_end; a+=2)

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by bobknows View Post
    its easier to input them into an array, then move through them with a double incrementing for loop
    Code:
    for(int a = 0; a < array_end; a+=2)
    I wouldn't say it's easier, he probably doesn't know how to create a dynamic array. Moreover it would needlessly consume memory.
    I never put signature, but I decided to make an exception.

  6. #6
    Registered User bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    Posts
    87
    true,

    i guess i was only thinking of how short to get it, not how complex it would be.
    i didnt think of having one variable and cin ing it multiple times either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number user input
    By jimtuv in forum C Programming
    Replies: 62
    Last Post: 06-13-2010, 12:08 PM
  2. Multiple classes question
    By TriKri in forum C++ Programming
    Replies: 20
    Last Post: 06-11-2010, 05:03 PM
  3. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 05:24 PM
  4. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  5. Find all factors of a number
    By Spono in forum C Programming
    Replies: 5
    Last Post: 10-23-2003, 02:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21