Like Tree5Likes
  • 1 Post By laserlight
  • 1 Post By samf
  • 1 Post By samf
  • 1 Post By samf
  • 1 Post By anduril462

odd even count error

This is a discussion on odd even count error within the C Programming forums, part of the General Programming Boards category; I'm writing a C program that reads integars from file "integars.dat". The program should show how many numbers you have ...

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

    odd even count error

    I'm writing a C program that reads integars from file "integars.dat". The program should show how many numbers you have entered, how many among them were even, and how many were odd. Also the average of integer numbers that were odd.


    I wrote everything accordingly, but I still don't understand where I'm messing up.


    Code:
    #include <stdio.h>
    int
    
    
    main (void)
    
    
    {
        FILE *in;
        int number, s, count, w, j, status, sum, n;
        double y, average;
    
    
        in = fopen ("integars.dat", "r");
    
    
        count = 0; 
        sum = 0;
        average = y;
    
    
    
        status = fscanf (in, "%d", &number);
        while (status != EOF)
           
        {
            status = fscanf(in, "%d", &s);
            for (j=1; j<=s; j++)
    
            {
                count = count + 1;
                fscanf (in, "%lf", &y);
                average = (double)sum/number;
    
             }
     
                  fscanf(in, "%d", &w);
                  if (n%2==0){
                  printf ("There are %d even numbers.\n", w);
    
              }
              else 
              printf ("There are %d odd numbern.\n", w);
         }
    
       status = fscanf (in, "%d", &number);
     
    
    fclose (in);
    
    
       printf ("There are %d numbers in the file.\n", count);
    
        return (0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Are you the same person who started the odd even number thread?

    Anyway:
    • Use descriptive variable names.
    • Why do you read into both number and s?
    • Why do you use nested loops?


    You really only need one variable to read into, and then you need to keep track of:
    • the number of numbers entered
    • the number of even numbers entered
    • the number of odd numbers entered (or use subtraction)
    • the sum of the odd numbers
    neveragn likes this.
    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

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,484
    Step 1 one use meaningful variables names; DO NOT use three variables for the exact same purpose.

    Note: "Sum" is normally calculated in-side a loop; "Average" is normally done below the loop that did "Sum".

    Post an example "integars.dat" file; it is obvious you have no idea what it contains or you have no idea how to write a loop to read it.

    Edit: Slow posting once more.

    Tim S.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    I thought that you need to use different variables. I'm completely new to this, so I'm totally lost.

    "integars.dat"
    3
    7
    2
    11
    4

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    In this case, it is fine to overwrite the variable used for reading. In some other cases, you might read into an array instead because you need all the values read even after the loop that does the reading and initial processing.
    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

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Code:
        in = fopen ("integars.dat", "r");
    It would also be a good idea of getting into the habit of checking the return value when opening a file.

    Code:
        #include <string.h>
        #include <errno.h>
        in = fopen ("integars.dat", "r");
        if (in == NULL) {
           fprintf(stderr, "Error opening file integars.dat. - %s", strerror(errno));
           exit(1);
        }
    neveragn likes this.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    My program shouldn't consist of any error checkers. Just nested loops, arrays and the odd even is just not coming out right. I dont even undrstand how to get the average of the odd numbers.

    Help please :S
    It's beginners stuff, and I cant get past it even.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    Quote Originally Posted by neveragn View Post
    My program shouldn't consist of any error checkers. Just nested loops, arrays and the odd even is just not coming out right.
    If your professor is teaching you not to check for errors, then you have a crappy teacher. If that's your own, personal rule, then you need to change it. You'll be totally baffled as to the answers you get if your program can't read the numbers because it couldn't open the file in the first place. At least put them in there for your sake. You can remove them before you turn in your program if need be.

    I dont even undrstand how to get the average of the odd numbers.
    Can you determine if a number is odd or even by hand? How do you do that? Can you find the average of a set of numbers by hand? What are the steps? You should never start coding until you understand your problem and know how to solve it by hand.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Just follow laserlight's advice:

    You'll need a variable for each of the items laserlight detailed.

    For example:
    Code:
    int oddNumberCount;
    int evenNumberCount;
    int totalEntered; /* total number of ALL integers read from the file */
    You should only have one loop in your code to read in the file. You have two. You do not need the nested for loop.
    Calculate your average AFTER you have read, tallied, and summed in all your integers.

    You already have the test in your code to determine if a number is odd or even, so when you encounter an odd number, increment oddNumberCount by 1, else an even number increment evenNumberCount by 1.
    Regardless if the number is even or odd, increment totalEntered by 1, then add the number to your sum variable.

    After you have finished reading in all the numbers and exit the while loop, then you can calculate the average.
    neveragn likes this.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Code:
    #include <stdio.h>
    int
    main (void)
    {
     FILE *in;
     int odd, even, sum, number,n;
     double average;
     
     in = fopen ("integars.dat", "r");
     
     
      while(fscanf(in, "%d", &number) == 1){
      n++;
     
       if (number%2==0)
        even++;
      
       else{
      
        sum += number;
        odd++;
       }
      
     } 
     
     fclose (in);
            printf("There are %d even integars in the file", even);
      printf("There are %d odd integars in the file", odd);
      printf("There are %d integars in the file.\n", sum);
      average = sum/odd;
      printf("The average of odd integars in the file is:  ", average);
     
     return (0);
    I'm deffinetly failing this course:S

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Don't panic. You are so close!

    You have to tackle this one problem at a time.

    When you compile this you probably received a warning about too many arguments for format. You need to put a "%f" in there to print your average.

    Think about what you are saying and printing.
    In line 29 you are saying you x integers in the file, yet you are printing the sum of the integer, NOT how many integers there are.
    neveragn likes this.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    Quote Originally Posted by neveragn View Post
    I'm deffinetly failing this course:S
    With that attitude, you will definitely fail. You can do this though. You're really close now, just a few minor issues.
    • You need to initialize odd, even, sum and n to zero before you increment or add to them.
    • You need to check the return value of fopen and exit if it failed (see below).
    • You should put a new line (\n) at the end of each printf, so you can read the output more easily.
    • You need a format specifier for average. It's a double, so you need to put a %f.
    • Since sum and odd are integers, sum/odd does integer division, so you need to cast either sum or odd to a double (also see below).

    Code:
    in = fopen("integers.dat", "r");
    if (in == NULL) {
        perror("Failed to open integers.dat");
        return(1);
    }
    ...
    average = (double) sum / odd;
    neveragn likes this.

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    omg! It's actually working!! .. Thank you so soo much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Uppercase/Lowercase/Word COunt/Char count
    By Charak in forum C Programming
    Replies: 7
    Last Post: 02-23-2011, 07:16 AM
  2. Replies: 10
    Last Post: 11-18-2008, 10:52 PM
  3. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  4. Replies: 2
    Last Post: 05-05-2002, 01:38 PM

Tags for this Thread


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