简体   繁体   English

如何计算数组中值的平均值?

[英]How to calculate the average of the values in an array?

I have a program where you can read any number of values through an array.我有一个程序,您可以在其中通过数组读取任意数量的值。 The problem is that my teacher will test the program and I cannot predict how many values she will enter.问题是我的老师会测试这个程序,我无法预测她会输入多少个值。 Therefore I don´t know how to calculate the mean average, because I can't program number1, number2, number3.... Unfortunately, I am still a beginner and therefor I don't know whether you can calculate with the values stored in the array.因此我不知道如何计算平均平均值,因为我无法编程 number1、number2、number3.... 不幸的是,我仍然是初学者,因此我不知道您是否可以使用存储的值进行计算在数组中。

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

int main()
{
    float datenbank[500];
    float messwert = 0;
    int anzahl = 0;

    do {
        printf("Geben sie bitte einen Messwert ein: ");
        scanf("%d", &datenbank);
        datenbank[anzahl] = messwert;
        anzahl++;
        printf("%d Messwerte wurden schon eingegeben\n", anzahl);
        printf("Wollen sie noch einen Messwert eingeben:\n 1) yes\n 2) no\n");

        switch(messwert) {
            case 1:
                yes;
                break;
            case 2:
                no;
                break;
            default:
                printf("Unverstaentliche Antwort");
                break;
        }

        if (messwert = yes) {
            printf("Geben sie bitte einen weiterern Messwert ein: ");
            scanf("%d", &datenbank);
        } else {
            printf("Zahl Nr. %d = %d\n"); 
        }
    }
    return 0;
}

You should be able to get the mean average by dividing the sum on the count of numbers您应该能够通过将总和除以数字计数来获得平均平均值

but in your code you still have many mistakes like scanf("%d", &datenbank);但是在您的代码中,您仍然有很多错误,例如scanf("%d", &datenbank); here you are taking Datenbank but I believe that you meant to use scanf("%d", &messwert) instead在这里,您正在使用 Datenbank,但我相信您的意思是使用scanf("%d", &messwert)代替

Here is other problem the type of messwert is float and you requiring the user to enter an integer you should use scanf("%f", &messwert) Instead这是其他问题,messwert 的类型是浮动的,您需要用户输入 integer 您应该使用scanf("%f", &messwert)代替

and another problem is what comes after this line printf("Wollen sie noch einen Messwert eingeben:\n 1) yes\n 2) no\n") You're asking the user to enter 1 or 2 but didn't make him a place to put the answer you should use scanf("%f", &messwert) after the asking the user to enter 1 or 2另一个问题是这行printf("Wollen sie noch einen Messwert eingeben:\n 1) yes\n 2) no\n")你要求用户输入 1 或 2 但没有让他在要求用户输入 1 或 2 之后放置答案的地方,您应该使用scanf("%f", &messwert)

another problem in your code is switch(messwert) since messwert is float you can't use it in switch you should cast it into int like this switch((int)messwert) if the use did a mistake and entered 1.9 your program should only care about the 1 so if the user entered 1.9 then he answered yes你的代码中的另一个问题是switch(messwert)因为 messwert 是浮点数你不能在 switch 中使用它你应该像这个switch((int)messwert)一样将它转换为 int 如果使用出错并输入 1.9 你的程序应该只关心 1 所以如果用户输入 1.9 然后他回答是

and I'm not sure about the cases you entered there isn't a thing called yes or no in C you should store it in a value you as the programmer give it a meaning so let's assume that yes is 10 and no is 11 it should be而且我不确定您输入的案例在 C 中没有一个叫做 yes 或 no 的东西,您应该将它存储在一个值中,因为程序员给它一个含义所以让我们假设 yes 是 10,no 是 11应该

case 1:
                messwert = 10;
                break;
case 2:
                messwert = 11;
                break;

now I believe you want it to loop until the user enters no you should use while loop not if statements so if (messwert = yes) changes to while(messwert = 10) here is another problem you're using = to compare instead of == which mean that you weren't comparing the if messwert equals 10 you said that change the value in messwert to 10 it should be while(messwert == 10)现在我相信你希望它循环直到用户输入 no 你应该使用 while 循环而不是 if 语句所以if (messwert = yes)更改为while(messwert = 10)这是你使用 = 比较而不是 = 的另一个问题= 这意味着您没有比较 if messwert 等于 10 您说将 messwert 中的值更改为 10 它应该是while(messwert == 10)

when the users enters no this will exit the loop you want to give in mean average of numbers that's good you already have the numbers count and values you only need the sum当用户输入 no 时,这将退出您想要给出的平均数字的循环,这很好,您已经有了数字计数和值,您只需要总和

you can do that using a for loop你可以使用 for 循环来做到这一点

for(int x = 0; x < anzahl; x++)
{
    sum += datenbank[x];
}

and after you finish you just need to divide the sum by anzahl and print the value完成后,您只需将总和除以 anzahl 并打印值

here are some tips which will helps you you don't need both the entered number and if the user want to continue in one variable这里有一些提示可以帮助您不需要输入的数字,如果用户想继续使用一个变量

in the code that I corrected there isn't anything that ask the user to enter another answer if he answered with 3 on do you want to continue question在我更正的代码中,没有任何内容要求用户输入另一个答案,如果他回答 3 on do you want to continue question

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

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