Thread: finding average of even numbers at first occurence of odd input

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    10

    finding average of even numbers at first occurence of odd input

    I am trying to write a c program to read n numbers and reading of numbers should stop at the first occurence of odd numbers and find average of even numbers.
    sample input:- 2,4,6,12,18,7
    sample output:- 42


    what i wrote is

    Code:
    
    
    Code:
    #include<stdio.h>
    int main()
    {
        int i,num,sum=0,cou;
        float avg;
        while(i>0)
        {
        printf("enter the integers");
        scanf("%d",&num);
        if(num%2!=0)
        break;
        else
        printf("%d",num);
       }
       sum=sum+num;
       cou++;
       avg=sum/cou;
       printf("average of given numbers is %f",avg);
       return 0;
       }
    Can you please suggest what went wrong in the code ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,485
    Lines 15 and 16 should be inside your loop.

    > avg=sum/cou;
    Beware of integer division.

    - make some other variables floats to begin with.
    - cast one of them to a float before dividing.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    70
    Try this

    Code:
    #include <stdio.h>
    int i=1, cou;
    int num,sum=0;
    int avg;
    
    
    int main()
    {
        do
        {
        printf("enter the integers: ");
        scanf("%d",&num);
        if(num%2!=0){
        break;
        }else{
        sum+=num;
        }
       }while(i);
       
       avg=sum/2;
       printf("average of given numbers is %d",avg);
       return 0;
       }

  4. #4
    Registered User
    Join Date
    Jan 2021
    Posts
    10

    This is not working for the output i want

    Quote Originally Posted by Nikosant03 View Post
    Try this

    Code:
    #include <stdio.h>
    int i=1, cou;
    int num,sum=0;
    int avg;
    
    
    int main()
    {
        do
        {
        printf("enter the integers: ");
        scanf("%d",&num);
        if(num%2!=0){
        break;
        }else{
        sum+=num;
        }
       }while(i);
       
       avg=sum/2;
       printf("average of given numbers is %d",avg);
       return 0;
       }
    this is not working !!!!

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    70
    You,re sure?

    It seems to work.. GDB online Debugger | Code, Compile, Run, Debug online C, C++

    What are your expectations?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,485
    Well sum/2 isn't going to give you an average, unless you only type in two numbers.
    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.

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    70
    oops sorry I ignored the "average"

    Code:
    #include <stdio.h>
    
    
    int i=1, cou;
    int num,sum=0;
    float avg;
     
     
    int main()
    {
        do
        {
        printf("enter the integers: ");
        scanf("%d",&num);
        if(num%2!=0){
        break;
        }else{
            cou++;
        sum+=num;
        }
       }while(i);
        
       avg=(float)sum/cou;
       printf("average of given numbers is %.2f",avg);
       return 0;
       }

  8. #8
    Registered User
    Join Date
    Jan 2021
    Posts
    10
    finding average of even numbers at first occurence of odd input-whatsapp-image-2021-01-22-2-55-38-pm-jpeg
    thats the proper question

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,223
    I think the question has an error: it requests for the "average of even numbers", but provides sample input that results in sample output that is the sum of even numbers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jan 2021
    Posts
    10

    Thanks !!!! this is correct the given question's samples were incorrect !!!

    Quote Originally Posted by nikosant03 View Post
    oops sorry i ignored the "average"

    Code:
    #include <stdio.h>
    
    
    int i=1, cou;
    int num,sum=0;
    float avg;
     
     
    int main()
    {
        do
        {
        printf("enter the integers: ");
        scanf("%d",&num);
        if(num%2!=0){
        break;
        }else{
            cou++;
        sum+=num;
        }
       }while(i);
        
       avg=(float)sum/cou;
       printf("average of given numbers is %.2f",avg);
       return 0;
       }

    you are genius !!

  11. #11
    Registered User
    Join Date
    Jan 2021
    Posts
    10
    Thank You for the assertion you were correct the sample input and output given there were wrong and misleading !!

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,223
    Quote Originally Posted by Punju
    Thank You for the assertion you were correct the sample input and output given there were wrong and misleading !!
    Ah, right.

    I'm afraid that nikosant03's example is not very good though:
    • It makes use of global variables without good reason. (Well, this program is sufficiently trivial that the use of global variables doesn't matter, but if you aren't skilled enough to correctly write this program on your own with good style, then you should not be declaring your own global variables.)
    • It does not check the return value of scanf. Granted, it might be reasonable to assume valid input here, but it is still good practice to check.
    • It makes use of a do while loop in an awkward construction, i.e., using the i variable that is effectively a constant when it is unnecessary to begin with. A more conventional way to write that loop would be to use a while (1) or for ( ;; ) construct, relying on the break statement within the loop body to terminate the loop.
    • It does not account for the possibility that there are no even numbers entered.
    • The indentation needs work.


    Looking at the sample input, even though the corresponding sample output is wrong, I might suggest something like this:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int count = 0;
        int sum = 0;
        int num;
        printf("Enter even numbers followed by an odd number: ");
        while (scanf("%d", &num) == 1 && num % 2 == 0)
        {
            sum += num;
            ++count;
        }
    
        if (count > 0)
        {
            printf("Average of the even numbers = %.2f\n", (double)sum / count);
        }
        else
        {
            puts("No even numbers were input.");
        }
    
        return 0;
    }
    The idea is that the user can enter the numbers all on the same line, so you print the prompt once then keep looping while the expected integer input is valid and even. So this makes a reasonable assumption about how to handle invalid input.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Feb 2019
    Posts
    70
    That's the difference between a pro and a beginner . Room for improvement!!!

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. Help finding average?
    By Chinnie15 in forum C Programming
    Replies: 5
    Last Post: 11-10-2011, 08:59 AM
  4. help in finding average of a class of numbers
    By hastefan2001 in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2009, 01:11 PM
  5. Finding an Average
    By kidguru in forum C Programming
    Replies: 4
    Last Post: 02-24-2002, 06:25 PM

Tags for this Thread