简体   繁体   English

#Sequence的平均值

[英]Average of # Sequence

I'm having trouble figuring out the loop in the average function. 我在查找平均函数中的循环时遇到了麻烦。 User enters sequence and if the number of numbers in the sequence is greater than 0, output avg of the numbers and repeat for another sequence. 用户输入序列,如果序列中的数字大于0,则输出数字的平均值并重复另一个序列。 If the sequence has no numbers, exit program. 如果序列没有数字,退出程序。

I understand that I'm telling the average function to return 0 when sum = 0 and that's why it exits after the 1st sequence (I think). 我明白,当sum = 0时,我告诉平均函数返回0,这就是为什么它在第一个序列之后退出(我认为)。

Any suggestions as to how to avoid that? 有关如何避免这种情况的任何建议? Pseudocode if possible! 伪代码如果可能的话!

#include<stdio.h>
#include<stdlib.h>

double average();

int main () 
{
    while( average() ) 
    {
    }
}//end main

double average() 
{
    double n, sum = 0;
    int count = 0;

    printf ( "Enter sequence: " );
    while( scanf( "%lf", &n ) )
    {
        sum += n;
        count++;
    }

    if( sum > 0 )
    {
        printf( "average is %.2f\n", sum/(double)count );
        return 1;
    }
    else
        return 0;
    }
}

Here is my output: 这是我的输出:

Enter sequence: 3 4 5 x
    average: 4.00
Enter sequence: Press any key to continue . . .
#include <stdio.h>
#include <stdlib.h>

int average(void);

int main(void) 
{
    while (average()) 
        ;
    return 0;
}

int average(void) 
{
    double n, sum = 0;
    int count = 0;

    printf("Enter sequence: ");
    while (scanf( "%lf", &n ) == 1)
    {
        sum += n;
        count++;
    }
    int c;
    while ((c = getchar()) != EOF && c != '\n')
        ;

    if (sum > 0)
    {
        printf("average is %.2f\n", sum / count);
        return 1;
    }
    else
        return 0;
}

This reads anything on the line up to the newline after a conversion fails, thus setting you up for reading the next sequence. 这会在转换失败后读取换行符中的任何内容,从而为读取下一个序列做好准备。 The loop test for scanf() is improved; scanf()的循环测试得到改进; it will exit on EOF, too. 它也会在EOF上退出。 The cast in the division was unnecessary; 分裂中的演员是不必要的; the compiler has to convert count to double because sum is a double , even without you telling it to do so explicitly. 编译器必须将count转换为double因为sum是一个double ,即使你没有告诉它明确地这样做。 The return type of average() is not a double; average()的返回类型不是double; it is a boolean, which is classically spelled int . 它是一个布尔值,通常拼写为int In C99 or later (which the code assumes you have; if you don't, you're stuck on Windows and need to move int c; to the top of the function), then you could #include <stdbool.h> and use a return type of bool and replace return 1; 在C99或更高版本中(代码假设你有;如果你没有,你就被困在Windows上并且需要移动int c;到函数的顶部),那么你可以#include <stdbool.h>和使用返回类型的bool并替换return 1; by return true; 通过return true; and replace return 0; 并替换return 0; by return false; 通过return false; .

I think you can initially get input for the variable named count asking for the user to enter the total no of numbers in sequence,and then get those values in sequence. 我想你最初可以获得名为count的变量的输入,要求用户按顺序输入数字的总数,然后按顺序获取这些值。 And if the count is 0,then exit the program.. else continue finding average. 如果计数为0,则退出程序..否则继续查找平均值。

This is because in your case, you have to enter a non numeric char each time to end the sequence. 这是因为在您的情况下,每次都必须输入一个非数字字符来结束序列。

Well I ended up with this and its working perfectly: 好吧,我最终得到了这个并且它完美地工作:

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

double average();

int main () {
    while( average() ) {}
}//end main

double average() {
    double n, sum = 0;
    int count = 0;

    printf ( "Enter sequence: " );
    getchar();
    while( scanf( "%lf", &n ) ){
        sum += n;
        count++;
    }//end while

    if( n > 0 ){
        printf( "    average: %.2f\n", sum/(double)count );
        return 1;
    }//end if
    else
        return 0;
}//end function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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