如何正确显示直方图? [英] How do I display my histogram properly ?
本文介绍了如何正确显示直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好我正试图为我的选举显示直方图。
我认为我的代码有效,但在第10级它似乎改变了我的所有选票,因为我认为有2数字......
再次感谢您的时间;)
我尝试过的事情:
Hi everyone i'm trying to display a histogram for my "election".
I think my code works but at the 10th rank its seem to shift all my votes because i think there is 2 numbers ...
Thank you again for your time ;)
What I have tried:
#include <stdio.h>
#include <stdlib.h>
int main() {
char character ;
int TAB[12] = {0} ;
int vote = 0;
int I;
printf("Please enter a character:\n");
scanf(" %c", &character);
printf("Please enter votes\n");
while(1) {
scanf("%d", &vote);
if (vote == -1) {
break;
}
TAB[vote-1]++;
}
printf("Histogram :\n");
/* Search for the maximum value */
int MAX=0;
for (I=0; I<12; I++)
{
if(TAB[I]>TAB[MAX]) MAX=I;
}
int maximum = TAB[MAX];
while (maximum > 0) {
for (I = 0; I < 12; I++) {
if (TAB[I] == maximum) {
printf("%c ",character);
TAB[I] = (TAB[I] - 1) ;
}
else {
printf(" ");
}
}
maximum= maximum - 1;
printf("\n");
}
for (I = 0; I < 13; I++) {
printf("%d ",I+1);
}
printf("\n");
MAX = MAX + 1;
switch (MAX)
{
case 1:
printf("Ido was elected!");
break;
case 2:
printf("Alon was elected!");
break;
case 3:
printf("Asaf was elected!");
break;
case 4:
printf("Daniella was elected!");
break;
case 5:
printf("Daniel was elected!");
break;
case 6:
printf("Dimitri was elected!");
break;
case 7:
printf("Gasob was elected!");
break;
case 8:
printf("Ido was elected!");
break;
case 9:
printf("Najib was elected!");
break;
case 10:
printf("Nir was elected!");
break;
case 11:
printf("Omer was elected!");
break;
case 12:
printf("Yair was elected!");
break;
case 13:
printf("Yarah was elected!");
break;
}
return 0;
}
推荐答案
您需要阅读 printf 文档。
You need to read the printf documentation carefully.
printf("%2d ",I+1);
应该可以帮到你。
提示:你可以将这些名字放入 char *数组,所以你不需要这么大的开关。
should help you.
tip: you can put the names into a char* array, so you dont need such a big switch.
试试
Try
#include <stdio.h>
#define CANDIDATES 12
int main()
{
// array used to get the candidate name by its index
const char * candidate[CANDIDATES]=
{
"Ido", "Alon", "Asaf", "Daniella", "Daniel", "Dimitri",
"Gasob", "Najib", "Nir", "Omer", "Yair", "Yarah"
};
char character ;
int imax;
int votemax;
int vote[CANDIDATES] = {0};
do
{
printf("Please enter a character:\n");
} while ( scanf("%c", &character) != 1);
printf("Now please enter votes int the 1..%d range, -1 to exit\n", CANDIDATES);
while (1)
{
int v;
while ( scanf("%d", &v) != 1) {}
if ( v == -1)
break;
--v;
if ( v >= 0 && v <CANDIDATES)
++vote[v];
else
printf("Warning, invalid vote %d\n", v);
}
{// find the index of the candidate most voted
int i;
imax = 0;
for ( i = 1; i<CANDIDATES; ++i)
if (vote[imax] < vote[i])
imax = i;
}
votemax = vote[imax];
{// print the histogram
int row;
int i;
for (row = 0; row < votemax; ++row)
{
for (i=0; i<CANDIDATES; ++i)
{
char c = vote[i] >= (votemax-row) ? character : ' ';
printf(" %c ", c);
}
printf("\n");
}
for (i=0; i<CANDIDATES; ++i)
printf("%-3d", (i+1));
}
printf("%s is elected\n", candidate[imax]);
return 0;
}
这篇关于如何正确显示直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文