Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Mein Programm soll die Summe der Zahlen berechnen und zusammen mit dem Mittelwert beim Programmende ausgegeben werden. Funktioniert aber nicht !!!

Ich bitte um Hilfe !!!

 #include "pch.h"
 #include <iostream>
 #include <stdio.h>
 #include <ctype.h>

 int main()
{
     int x, anzahl;
     double zahl, summe = 0.0, mittelw;

     printf("\n\t Statistik\n");
     printf("\nWieviel Werte wollen Sie eingeben: ");
     scanf_s("%i", &anzahl);
     printf("\n");

     for (x = 1; x <= anzahl; x = x + 1)
     {
         printf("Bitte %i. Zahl eingeben: ", x);
         scanf_s("%1.f", &zahl);
         summe = summe + zahl;
     }
     mittelw = summe / anzahl;
     printf("\n\nSumme der Zahlen    = %f", summe);
     printf("\nMittelwert der Zahlen = %f", mittelw);
}

Dankeschön !!!

 

vor 3 Minuten schrieb Bidgaff:

Funktioniert aber nicht !!!

Ist keine aussagefähige Fehlermeldung.

BTW: zur Verwendung mehrfacher Ausrufezeichen sei hier einfach mal auf die diversen bekannten Zitate von Terry Pratchett verwiesen.

Mir erschließt sich die Verwendung von scanf() auch nicht ganz, zumal mir die Formatierung %1.f für &zahl auch nicht richtig erscheint.

In einer x-beliebigen Shell kann ich das Programm so aber auch nicht ausführen. Verwende ich statt scanf_s() aber std::cin >> anzahl resp. std::cin >> zahl, kann ich beliebig viele Werte eingeben. Wichtig wäre dann aber natürlich noch das Exception-Handling, falls jemand z.B. einen Buchstaben eingibt.

Über den Sinn oder Unsinn von printf() ließe sich natürlich auch streiten.

int main()
{
     int x, anzahl;
     double zahl, summe = 0.0, mittelw;

     printf("\n\t Statistik\n");
     printf("\nWieviel Werte wollen Sie eingeben: ");
     std::cin >> anzahl;
     printf("\n");

     for (x = 1; x <= anzahl; x++)
     {
         printf("Bitte %i. Zahl eingeben: ", x);
         std::cin >> zahl;
         summe = summe + zahl;
     }
     mittelw = summe / anzahl;
     printf("\n\nSumme der Zahlen    = %f", summe);
     printf("\nMittelwert der Zahlen = %f", mittelw);
}

 

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.