Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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;
}
Posted
Updated 18-Nov-18 8:17am
Comments
Richard MacCutchan 18-Nov-18 3:38am    
"I think my code works" ... "i think there is 2 numbers"
Thinking is all very well, but you need to explain exactly what the problem is.

You need to read the printf documentation carefully.
C++
printf("%2d ",I+1);
should help you.

tip: you can put the names into a char* array, so you dont need such a big switch.
 
Share this answer
 
Try
C
#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;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900