Home > Net >  Until the user enters a 0, the program will keep asking for numbers. then compute the average of jus
Until the user enters a 0, the program will keep asking for numbers. then compute the average of jus

Time:11-25

In my program, it doesn't work if a user input a negative. It will still be included in the count.

#include <stdio.h>

int main() {
    double number, sum = 0;
    float average;
    int count;
    
    do {
        printf("Enter a number: ");
        scanf("%lf", &number);
        count  ;
        if (number > 0)
            sum  = number;
        average = sum / (count - 1);    
    } while (number != -1);

    printf("Average is %.1lf", average);

    return 0;
}

CodePudding user response:

In your program you are counting all numbers independent on whether they are positive or negative

do {
    printf("Enter a number: ");
    scanf("%lf", &number);
    count  ;
    //...

Also it does not make a sense to calculate the average within the do while loop.

average = sum / (count - 1);

And it is unclear why the variable average has the type float instead of double.

float average;

And you forgot to initialize the variable count.

Pay attention to that the user can enter neither positive number.

And as it follows from the title of your question you are going to enter integer numbers not double.

The program can look the following way

#include <stdio.h>

int main( void ) 
{
    double sum = 0.0;
    double average = 0.0;

    size_t count = 0;
    
    while( 1 )
    {
        printf( "Enter a number: " );
        
        int number;
  
        if ( scanf( "%d", &number ) != 1 || number == 0 ) break;

        if ( number > 0 )
        {
            sum  = number;
              count;
        }
    }

    if ( count != 0 ) average = sum / count;    

    printf( "Average is %f\n", average );

    return 0;
}

CodePudding user response:

Firstly, you didn't initialize your count variable. That said, you should increment the count variable only when a non negative value is encountered. Unrelated but I'd recommend you calculate the average outside the loop, as below:

#include <stdio.h>
int main()
{
    double number, sum = 0;
    float average;
    int count = 0;

    do
    {
        printf("Enter a number: ");
        scanf("%lf", &number);

        if (number > 0)
        {
            sum  = number;
            count  ;
        }

    } while (number != 0); //should it stop with 0 or 1? assuming 0
    average = sum / count;
    printf("Average is %.1lf\n", average);

    return 0;
}

CodePudding user response:

First of all, always enable your compiler's warnings. I use -Wall -Wextra -pedantic with gcc and clang. This would have caught the first of the problems listed below if nothing else.

There are many problems.

  • count is not initialized.

  • Negative numbers aren't included in the sum as you claim, but they do affect the average because you increment count for negative numbers too.

  • The assignment asks for you to loop until you get zero, but you loop until you get -1.

  • The formula for average isn't sum / (count - 1).

  • average is calculated over and over again for no reason.

  • You don't handle the case where no inputs are entered, leading to a division by zero.

  • average is a float, which is odd since you it's built from double values.

  • You should check the value returned by scanf for errors or end of file.

  • You don't emit a line feed after your output.

#include <stdio.h>

int main(void) {
    int count = 0.0;    
    double sum = 0.0;
    while (1) {
        printf("Enter a number: ");
        double number;
        if ( scanf("%lf", &number) < 1 )  // Invalid input or EOF.
            break;
        if ( number == 0.0 )
            break;
        if ( number < 0.0 )
            continue;

        count  ;
        sum  = number;
    }

    if (count) {
       double average = sum / count;
       printf("Average is %.1lf\n", average);
    } else {
       printf("Nothng to average\n");
    }

    return 0;
}
  • Related