Thread: odd even number

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    odd even number

    I'm supposed to make a program that reads off a txt file called integers.txt. It reads the numbers and tells the user "how many numbers you have entered, how many among them were even, and how many were odd. The program must also display the average of integer numbers that were odd." I wrote the code below. It runs but for some reason it keeps saying that I have zero numbers, zero even numbers, zero odd numbers etc. I don't know what's wrong. Can someone please help me out? Thanks.


    Code:
    #include <stdio.h>
    
    
    int
    main (void)
    {
           int number, e, o, s, average;
        
    
    
        FILE *integers;
    
    
        integers = fopen ("integers.txt", "r");
    
    
        /*initialization*/
        e = 0;
        o = 0;
    
    
        /* read number by number until the end of file */
        while (fscanf (integers, "%d", &number) !=EOF) 
        {
        
         if (number%2 == 0)
              e = e + 1;
                 
         else                   
             o = o + 1;            
        
        }     
        fclose (integers);
        /* final report */
        
        s = e + o;
        printf ("There are %d numbers in the file.\n", s);
        printf ("The total number of even numbers numbers is %d.\n", e);
        printf ("The sum of all the odd numbers numbers is %d.\n", o);
        average = o / e;
        printf("The average number of odd number is %d\n.", average);
    
    
            return (0);
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You never check if the file was opened successfully; it might give zero results if the file failed to open.
    Check for "integers == NULL" and output a error message.

    Tim S.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    hey when i put "integers == NULL" in my code, it gives me an error message. What's that suppose to mean?

    I started tweaking the code to figure out what's wrong, i think for some reason the if and the while loops don't seem to be executing properly. Does anyone see anything wrong with it?

    thanks for your help Tim.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    It runs well for me (except for a logic error in line 40), so I suspect it's not opening the file correctly for you. Can you post the contents of integer.txt please?
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    This is exactly how the numbers appear in integers.txt:
    "2 2 8 8 4 2 5 3 9"

    thanks for the help BigH.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by western46 View Post
    Code:
        while (fscanf (integers, "%d", &number) !=EOF) 
        {
    This is known as newbie stuff, because it is a infinite loop if fscanf fails (e.g. a nondigit char occurs). (fscanf returns 0, and filepointer stops)

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    How would you fix it?

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The scanf functions return the number of succesfully converted items. You ask for one each time (just one "%d"), so you keep looping while it returns 1.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Quote Originally Posted by anduril462 View Post
    The scanf functions return the number of succesfully converted items. You ask for one each time (just one "%d"), so you keep looping while it returns 1.
    I don't understand it. Can you please show it

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you make a new account to say that? Why didn't you just use your normal one? Do you know how functions return values? Do you know how to compare values? Do you know How To Ask Questions The Smart Way?


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    that wasn't me. It's probably another person from my class. I've found a new way to do this. So no need anyone to fix this. My new program works now. THANKS FOR EVERYONE'S HELP!!

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    I am completely new to programming and I have a elementary question about how to test for an even number. I understand that ((n%2)==0) tests this because the number needs to be divisible by 2, but I don't understand why the boolean expression ==0 works, e.g 2%2 is not = to 0. I understand that == is a boolean expression, but the book I'm learning from explains it as still evaluating for equality, but provides either true or false as a value. What am I missing? I'm guessing that it has something to do with the remainder from the division being equal to 0, but I don't really understand why that would be? Could someone please explain this to me?

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    % is the modulo operator. It divides two operands and gives you the remainder. 2 divided by 2 is 1 with a remainder of 0. So 2%2 does equal 0.
    Because 2%2 == 0, the statement would return true.
    Last edited by failure67; 11-22-2011 at 08:55 PM.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Please do not hijack and reopen someone else's slightly old thread.
    If you have a question of your own, start your own thread.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-05-2009, 10:21 AM
  2. Input a Hex number and output hex number to a text field
    By zoobaby in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2009, 11:26 AM
  3. Allocation of major number and minor number linux
    By vinayharitsakp@ in forum Linux Programming
    Replies: 4
    Last Post: 12-08-2007, 02:01 PM
  4. Number of words multiplied by the number of lines in Linux
    By sjalesho in forum Linux Programming
    Replies: 2
    Last Post: 11-20-2003, 03:25 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM