mercredi 2 décembre 2020

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;
}

Aucun commentaire:

Enregistrer un commentaire