Thread: Average of odd and even

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    1

    Question Average of odd and even

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int num,x;
    
    
        int evenCount = 0;
        int oddCount = 0;
        int evenSum = 0;
        int oddSum = 0;
        float evenAvg = 0;
        float oddAvg = 0;
    
    
    
        while (num !=0)
        {
              printf("Enter a number (0 to stop): ");
              scanf("%d", &num);
    
    
              if (x == 0)
    
    
                 return 0;
    
    
              else
              {
                  if ( num % 2 != 0)
                  {
                       oddCount++;
                       oddSum = num + oddSum;
                  }
                  else if( num % 2 == 0)
                  {
                      evenCount++;
                      evenSum = evenSum + num;
                  }
              }
        }
        
        printf("\n");
    
    
        evenAvg = evenSum / evenCount;
        oddAvg = oddSum / oddCount;
    
    
        printf("Odd Average %.1f\n", oddAvg);
        printf("Odd Count: %d\n", oddCount);
        
        printf("\n");
        
        printf("Even Average %.1f\n", evenAvg);
        printf("Even Count: %d\n",evenCount - 1);
    
    
        printf("\n");
    
    
        return 0;
    }
    I'm new to C and trying to learn.

    My problem is that the average is wrong and when I don't put any odd numbers it crashes. how do I fix this?

    average is supposed to be the sum of number divided by the how many numbers there are

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Remember that you initialized the values with 0. Hence there can be a case of divide by 0.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    When you divide two integers, the result is an integer. You should cast at least one of them into a float if you want the result to be a float.
    No, the compiler can't read your mind to do it on its own.
    Devoted my life to programming...

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    If your using the %lf format type, it's generally better to use
    type double for your decimal numerical values. Also you declare
    num but do not initialize it with zero. Your loop test will only be true
    if num is zero, so it's a good idea to declare num as such, to avoid
    possible logical errors down the line.
    Double Helix STL

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If your using the %lf format type, it's generally better to use
    type double for your decimal numerical values.
    I agree, though actually, he seems to be formatting %f to .1 precision. but even so %Lf is for long doubles. The lowercase l should only really be used with long integers, c for wchar_t arguments, or s for wchar_t strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-25-2015, 04:10 PM
  2. Replies: 27
    Last Post: 02-14-2015, 11:17 AM
  3. Average
    By slatka in forum C Programming
    Replies: 6
    Last Post: 01-11-2008, 01:02 AM
  4. help with average
    By tmoney$ in forum C Programming
    Replies: 3
    Last Post: 05-09-2003, 05:46 PM
  5. Average age in here....
    By Musicdip in forum A Brief History of Cprogramming.com
    Replies: 51
    Last Post: 07-02-2002, 05:56 AM

Tags for this Thread