Horizontale und vertikale Histogramm aus einem array in C

Ich brauche, um die horizontale und vertikale Histogramm eines gegebenen Feldes. Im folgenden ist der C - code, den ich ausprobiert habe. Gibt es eine Möglichkeit das zu verbessern oder eine einfache Alternative Methode?

#include <stdio.h>
#define size 10
int main() {
  int array[size] = {2, 5, 7, 8, 10, 16, 7, 4, 3, 4};
  int i, j, bigCount, temp;

 /* To get Horizontal Histogram */
  for (i = 0; i < size; ++i) {
    printf("\n%3d|", i);
    for (j = 0; j < array[i]; ++j)
      printf(" #");
  }

  /* To find the biggest count */
  bigCount = array[0];
  for (i = 0; i < size; ++i)
    if(array[i] > bigCount)
      bigCount = array[i];
  temp = bigCount;
  printf("\n\n");

  /* To get Vertical Histogram */
  for (i = 0; i < bigCount; ++i) {
    printf("\n%3d|", bigCount - i);
     for (j = 0; j < size; ++j)
       if (array[j] < temp)
     printf("   ");
       else {
     printf("  #");
     --array[j];
       }
     --temp;
  }

 /* printing the x-axis */
  printf("\n    ");
  for (i = 0; i < size; ++i) 
    printf("  -", i);
  printf("\n    ");
  for (i = 0; i < size; ++i) 
    printf("%3d", i);
  printf("\n");
  return 0;

}

  • Was ist "leicht"?
  • "einfach" in dem Sinne, dass mit weniger Anzahl von Variablen, Schleifen, Funktionen etc.. Mit reduzierten Zeilen code insgesamt..
InformationsquelleAutor noufal | 2013-06-23
Schreibe einen Kommentar