简体   繁体   中英

Sum and average for n numbers with -1 as exit integer

Hello I have a small problem with my code and I want to understand it. My task is to write a program that take the sum and average for n numbers with while loops or nested while loops with if conditions, when you want to exit the code you should enter -1. The code is down below. The problem I have is that I can't get it to exclude the -1 from the calculation . What am I doing wrong. And it is suppose to be a simple code.

int main(void) {

    int count;
    float sum, average, number;
    sum = 0;
    count = 0;

    printf("Calculate sum and average (-1 to quit)\n");

    while (number!=-1) {
        scanf("%f", &number);
        sum = sum + number;
        count = count + 1;
    }
    average = sum / count;
    printf("Sum=%f", sum);
    printf(" Average=%f", average);
}

in the while block, you read the number and then add it to your average calculation, and only then after the iteration repeats you're checking if it's different than -1 to stop the iteration:

there are obviously different ways to solve it:

one way can be to use while(true) and in the while block after you read the number add an if statement to compare it with -1 and break out of the loop

while (true) {
    scanf("%f", &number);

    if (number == -1) break;

    sum = sum + number;
    count = count + 1;
}

Reordering the different steps could solve this:

int main(void) {

    int count;
    float sum, average, number=0.0f;
    sum = 0;
    count = -1;

    printf("Calculate sum and average (-1 to quit)\n");

    while (number!=-1) {
        sum = sum + number; // first time no effect with 0
        count = count + 1;  // first time "no effect" counting to 0
        scanf("%f", &number); // will not be counted or summed if -1
    }
    average = sum / count;
    printf("Sum=%f", sum);
    printf(" Average=%f", average);
}

Think it through with immediatly entered -1:

  • sum is 0+0, suitable for no numbers being added up
  • count is -1+1==0, suitable for no numbers
  • dividing by 0 should be avoided, but that is a separate issue

Think it through with one number:

  • number is read in with sum==0 and count==0
  • is not -1, so loop iterates again
  • sum and count are updated meaningfully
  • -1 one is entered
  • loop ends without processing -1

By the way, comparing a float for identity is risky. You would be safer if you could compare more "generously", eg while (number>-0.5) .

I think you should take the input at the last of the loop as it will get check in the next iteration. And when -1 comes up it will not be added.

    #include <stdio.h>
    int main(){
    int count=0;
    float sum=0, average=0, number=0;
    
    
    printf("Calculate sum and average (-1 to quit)\n");
    
        while(number!=-1){
            sum = sum + number;
            count = count + 1;
            scanf("%f", &number);
        }
            average = sum / count;
            printf("Sum=%f", sum);
            printf(" Average=%f", average);
    
    }

Code needs to test number as -1 before including it in the summation. OP's code test almost does this right. OP's code test for -1 before number is even assigned leading to undefined behavior (UB).

float number;
while (number!=-1) {  // UB!

Also good to test the return code of scanf() for success.

while (scanf("%f", &number) == 1 && number != -1) {
    sum = sum + number;
    count = count + 1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM