Another HW help!

This is a discussion on Another HW help! within the C Programming forums, part of the General Programming Boards category; Code: printf("Enter an integer:\n"); scanf("%d",&n); if(n>0)pluscounteven+2; else if (n>0)pluscountodd++; else if (n<0)minuscounteven=-2; else if (n<0)minuscountodd=-2; else zerocount++; This one seems ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    24

    Another HW help!

    Code:
    printf("Enter an integer:\n");
    scanf("%d",&n);
    if(n>0)pluscounteven+2;
    else if (n>0)pluscountodd++;
    else if (n<0)minuscounteven=-2;
    else if (n<0)minuscountodd=-2;
    else zerocount++;
    This one seems easy, but I can't find a way to it. I'm scanning "m", which will tell me to type an integer "m" times, you put in the integers, and the end result should give me

    Code:
    Among "m" scanned integers there are:
    0 positive even and 0 positive odd integers,
    0 negative even and 0 negative odd integers, and
    0 zero integers.
    , but with positive integers and not all zeros. I am able to do it if it's just positive or negative, but I don't know how to separate the odds and evens. I was hope that I could somehow start the odds at 1/-1, but I don't know if that's possible. Someone help! Much appreciated!

  2. #2
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Code:
    printf("Enter an integer:\n");
    scanf(" %d",&n);
    if(n>0){
         pluscounteven+=2;
         pluscountodd++;
    }
    else if (n<0){
         minuscounteven-=2;
         minuscountodd-=2;
    }
    else{
         zerocount++;
    }
    Did you mean that?
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    u might need a while loop to get the integer from the user m times
    Code:
    while(count < m)
    {
         read ( integer n);
         process n
         .
         .
         count++;
    }
    ssharish2005

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    24
    Quote Originally Posted by 00Sven
    Code:
    printf("Enter an integer:\n");
    scanf(" %d",&n);
    if(n>0){
         pluscounteven+=2;
         pluscountodd++;
    }
    else if (n<0){
         minuscounteven-=2;
         minuscountodd-=2;
    }
    else{
         zerocount++;
    }
    Did you mean that?
    ~Sven
    That would increase the number of even numbers I have by a factor of 2. I meant if I put 5 integers (1,2,3,4,5), then I should get an output.

    Code:
    Among 5 scanned integers there are:
    2 positive even and 3 positive odd integers,
    0 negative even and 0 negative odd integers, and
    0 zero integers.
    I think I just figured out it should be something inside. if(n>0 && n == ??????)

    That's the part I don't get now. How would I separate odds from evens?
    Last edited by Tarento; 04-22-2006 at 11:30 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    > How would I separate odds from evens?
    Basic math text book perhaps?
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing HW registers using bit fields and UINT32
    By samdomville in forum C Programming
    Replies: 4
    Last Post: 12-10-2008, 12:00 PM
  2. Replies: 10
    Last Post: 12-05-2008, 11:47 AM
  3. ioctl request to get the HW address
    By threahdead in forum Linux Programming
    Replies: 7
    Last Post: 01-03-2008, 12:34 AM
  4. Help with HW
    By DontBugMe in forum C++ Programming
    Replies: 7
    Last Post: 04-11-2006, 10:45 AM
  5. Hw
    By SAMSAM in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-23-2003, 06:17 AM

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