Thread: Integer problem

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    9

    Unhappy Integer problem

    i had this question today on my introductory C programming course midterm.

    Take 4 integers from user and display the number of odd or even integers present.
    for example, if u enter 3,6,7,88 -- then the output should be 2 odd numbers and 2 even numbers....like this..

    can sum1 help me with this??

    i just cannot get the algorithm right, let alone the CODE!! im so frstrated...please help me

    i can do until scanf, where the user inputs. then from the conditional statements,,,im stuCK!!!@~!

    help me

    hobilla

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Start by writing something (with scanf, if you want) that will get all the required input and print it out without doing anything. Once you know that it is working, post it to make sure your potential cboard gurus agree, and we can proceed from there.

    The key to distinguishing odd and even numbers is modulus (%) so this is actually a very easy task, algorithmically speaking. What you need to do is learn to use the appropriate commands, appropriately.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    do it the same way you do it in your head. when you see an even number, add to the count of even numbers. when you see an odd number, add to the count of odd numbers.
    Code:
    int odd = 0, even = 0;
    
    while (more numbers)
      if (even) even++;
      else odd++;
    now just fill in the underlined parts.

  4. #4
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    In order to get rid of the unnecessary if-statement, one may as well want to use:

    Code:
    static int eo[2];
    
    foreach(n in numbers)
            eo[n%2]++;
    But you can safely ignore this, hobilla.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    we didnt do while

    so we only did if else and switch statement..
    so how can i do it?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    By writing it out on paper how you would do it if you were handed some numbers in real life.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Problem with cin. Please help me.
    By Antigloss in forum C++ Programming
    Replies: 17
    Last Post: 06-06-2005, 09:50 AM
  3. A homework problem about C++ (Pointer)
    By joenching in forum C++ Programming
    Replies: 10
    Last Post: 03-14-2005, 04:28 PM
  4. Conversion of character string to integer
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 04:34 AM
  5. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM