读出数组元素会给出奇怪的数字;

hYd*_*Yde 0 c c++ arrays

大约一年前,当我开始编程时,我正在玩C中的数组.

这是我写的代码:

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

#define WARY 1000

int main() {
  srand(time(0));  // Initialisierung des Algorithmus für rand()
  int a = 0, zahl = 0, rep = 0, wertmin, wertmax, eins, zwei, drei, vier, fuenf,
      sechs, sieben, acht, neun, zehn;
  int zary[WARY];
  for (a = 0; a < WARY; ++a) {
    zary[a] = rand() % 10 +
              1;  // Befüllen des Arrays mit Zufallszahlen (Bereich von 1-10)
  }
  for (a = 0; a < WARY; ++a)  // Ausgabe aller Elemente des Arrays
  {
    printf("Inhalt von zary[%i]", a);
    printf("ist: %i \n", zary[a]);
  }

  a = 0;  // Initialisierung min/max mit Element der 1.Stelle des Arrays
  wertmax = zary[a];
  wertmin = zary[a];

  for (a = 0; a < WARY; ++a)  // Durchsuchen des Arrays nach Min/Max
  {
    if (wertmin > zary[a]) wertmin = zary[a];
    if (wertmax < zary[a]) wertmax = zary[a];
  }

  printf("Das kleinste Element der Tabelle ist %i\n", wertmin);
  printf("Das groesste Element der Tabelle ist %i\n", wertmax);

  for (a = 0; a < WARY; ++a)  // Zählen der Häufigkeit aller Elemente des Arrays
  {
    switch (zary[a]) {
      case 1:
        eins++;
        break;
      case 2:
        zwei++;
        break;
      case 3:
        drei++;
        break;
      case 4:
        vier++;
        break;
      case 5:
        fuenf++;
        break;
      case 6:
        sechs++;
        break;
      case 7:
        sieben++;
        break;
      case 8:
        acht++;
        break;
      case 9:
        neun++;
        break;
      case 10:
        zehn++;
        break;
      default:
        printf("Fehler\n");
    }
  }
  printf(
      "Elemente des Arrays sind:\n %i (1)er\n; %i (2)er\n; %i (3)er\n; %i "
      "(4)er\n; %i (5)er\n; %i (6)er\n; %i (7)er\n; %i (8)er\n; %i (9)er\n; %i "
      "(10)er\n",
      eins, zwei, drei, vier, fuenf, sechs, sieben, acht, neun, zehn);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这段代码的最后用意是用1-10之间的随机数填充数组,然后读出数组的元素以及它们出现的频率.

它工作得相当好,但每次它说数量超过4,000的4000倍.

我的错误在哪里?

小智 5

您需要eins,zwei,drei,vier,fuenf,sechs,sieben,acht,neun,zehn在switch阻止之前将所有值初始化为0 .

eins = 0;
Run Code Online (Sandbox Code Playgroud)

等等.