我需要获取给定数组的水平和垂直直方图。以下是我尝试过的 C 代码。有什么方法可以改进这个或任何简单的替代方法吗?
#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;
}