简体   繁体   中英

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.

#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); here you are taking Datenbank but I believe that you meant to use scanf("%d", &messwert) instead

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

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

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

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

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)

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

you can do that using a for loop

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

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

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