Problem in Loop!!

This is a discussion on Problem in Loop!! within the C Programming forums, part of the General Programming Boards category; n numbers are entered from the keyboard into an array. Write a program to find out many of them are ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    Problem in Loop!!

    n numbers are entered from the keyboard into an array. Write a program to find out many of them are positive, how many are negative, how many are even and how many odd.

    Sample Input #Supposed to be)
    Enter 10 numbers : 10 20 23 -6 52 34 22 66 -22 -1
    Sample Output #1:
    Positive Numbers : 7
    Negative Numbers : 3
    Odd Numbers : 2
    Even Numbers : 8

    Code:
    #include <stdio.h>
    #define MAX_INP 10
    
    int main ()
    {
        int no[MAX_INP]={0},i=0,j=1,pos=0,neg=0,odd=0,even=0;
        
        for (i =0; i<10; i++){
            printf("Enter no %d: ",j);
            scanf("%d",&no[i]);
            j++;
            }
            
        
         for (i=0; i<10; i++){
            if(no[i] > 0){
                     pos++;
                     }
                     
            if(no[i] < 0){
                 neg++;
                 }
                         
            if((no[i]/2) == 1 || no[i]/2 == -1){
                 odd++;
                 }
                 
            if((no[i]/2) == 0){
                 even++;
                 }
                             }
                             
            printf("\n\nPositive numbers : %d\nNegative Numbers : %d\nOdd Numbers : %d\nEven Numbers : %d",pos,neg,odd,even);
        
        
        
        getch ();
        return 0;
    }

    Out put is not shown correctly in case of odd and even while =ve and -ve numbers are showing properly. Please Help!!!!!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,174
    Nothing to do with the loop. It is your tests for what is odd or even that are broken.

    If no[i] is 23, then no[i]/2 is 10 (integer arithmetic). The tests you are doing will not classify it as odd or even.

    If no[i] is -1 then no[i]/2 is zero. Your test means it will be counter it as even.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    82
    OOOH!!! my mistake. i mistakenly used / instead of %

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by fredsilvester93 View Post
    Out put is not shown correctly in case of odd and even while =ve and -ve numbers are showing properly. Please Help!!!!!
    The easiy way to detect an odd or even number is by testing bit 0...
    Code:
    if (number & 1)
      odd++;
    else
      even++;
    Last edited by CommonTater; 11-19-2011 at 07:38 PM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,174
    Quote Originally Posted by CommonTater View Post
    The easiy way to detect an odd or even number is by testing bit 0...
    Code:
    if (number & 1)
      odd++;
    else
      even++;
    The slightly less cryptic way is use modulo 2. It requires no more typing, and there is some advantage to understanding through computing a (trivial) numeric attribute using a numerical operation rather than a bitwise operation. No need to know what a bit is, what bit 0 is .....

    Bitwise operations do have some implementation-defined or undefined characteristics on signed types too.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    RE:

    Quote Originally Posted by CommonTater View Post
    The easiy way to detect an odd or even number is by testing bit 0...
    Code:
    if (number & 1)
      odd++;
    else
      even++;
    Didn't get the Logic... what does ''if (number & 1) means?''


    i used ''if(no%2 == 1)''

    help plz///

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Quote Originally Posted by fredsilvester93
    what does ''if (number & 1) means?
    You could read up on bitwise operators, but for your purposes what you used works just as well.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by fredsilvester93 View Post
    Didn't get the Logic... what does ''if (number & 1) means?''


    i used ''if(no%2 == 1)''

    help plz///
    It's capitalizing on how integers are stored in C ... an integer is just a binary number.

    In the binary numbering system all odd numbers have a 1 at bit position 0.

    0 = 00000000
    1 = 00000001 <-- odd
    2 = 00000010 < -- even
    3 = 00000011 <--- odd
    and so on...

    So if we AND the number with 1 (i.e. bit 0 = 1) we will get a 1 or a 0 indicating odd or even.

    01010011
    AND
    00000001
    ======
    00000001 <-- odd

    11011000
    AND
    00000001
    ======
    00000000 <-- even

    Since an if() statement responds to TRUE (1) or FALSE (0) we can easily use the result of number AND 1 to trigger it.
    Last edited by CommonTater; 11-20-2011 at 06:20 AM.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    RE:

    Quote Originally Posted by CommonTater View Post
    It's capitalizing on how integers are stored in C ... an integer is just a binary number.

    In the binary numbering system all odd numbers have a 1 at bit position 0.

    0 = 00000000
    1 = 00000001 <-- odd
    2 = 00000010 < -- even
    3 = 00000011 <--- odd
    and so on...

    So if we AND the number with 1 (i.e. bit 0 = 1) we will get a 1 or a 0 indicating odd or even.

    01010011
    AND
    00000001
    ======
    00000001 <-- odd

    11011000
    AND
    00000001
    ======
    00000000 <-- even

    Since an if() statement responds to TRUE (1) or FALSE (0) we can easily use the result of number AND 1 to trigger it.

    ok . like if (no & 1 == 0) will do even++ of no is 2????

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by fredsilvester93 View Post
    ok . like if (no & 1 == 0) will do even++ of no is 2????
    Any even number. That's the whole point... that last bit is 1 for all odd numbers, 0 for all even numbers.
    The thing is we have to strip the rest off so we can look at that one bit in isolation, so we use AND.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    82
    oooooh gr8... got the concept after applying.. thanks alot allllllll........ it a gr8 concept....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while loop problem
    By matt345 in forum C Programming
    Replies: 8
    Last Post: 01-12-2011, 11:49 AM
  2. loop problem
    By eagle0eyes in forum C Programming
    Replies: 7
    Last Post: 05-29-2010, 02:14 PM
  3. cin loop problem
    By Mr Fancy in forum C++ Programming
    Replies: 3
    Last Post: 07-03-2007, 08:13 AM
  4. Problem with for loop
    By irsmart in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2006, 04:26 PM
  5. do while loop problem
    By nelinda in forum C Programming
    Replies: 1
    Last Post: 11-30-2003, 08:29 AM

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