简体   繁体   中英

Simple Vertical Histogram in C

The exercise was to print a histogram of the different characters in the input. At the bottom is my code where I break the different characters into 3 categories a,b, or other, to extrapolate to more categories of characters if the code is successful. But it doesn't have the output I want, for example, for input ab cd I expect

  x
xxx
---
abo
  t
  h
  e
  r

but all I get is

---
abo
  t
  h
  e
  r

Here's my attempt:

#include<stdio.h>

main()
{
int i,c,b, max;
int array[3] = {0,0,0};


while( (c = getchar()) != EOF){
    if(c== 'a')
    ++array[0];
    else if(c== 'b')
    ++array[1];
    else if(c=='\n' ||c=='\t' || c==' '){}
    else ++array[2];
    }

array[0]=max;
i=0;

 while(i<3){
    if(max>=array[i])
        {}
    else array[i]=max;

    ++i;
 }

i=0;
max=b;
while(i<b){
    if(array[0]>=max){
        putchar('x');}
    else putchar(' ');


    if(array[1]>=max) {
        putchar('x');}
    else putchar(' ');


    if (array[2]>=max){
        putchar('x\n');}
    else putchar(' \n');
    --max;
    ++i;


    }

printf("---\nabo\n  t\n  h\n  e\n  r");


}

There's a good bit wrong with your code.

  1. Never do - main()

    You should be using int main(void){...} see this answer .

  2. You're hard coding numbers into your code. Provide constant variables instead. There are logical exceptions to this but numbers generally have meaning so state it.
    unsigned const int LETTERS = 3;

  3. You take the time to increment array[0] counting the occurrences of 'a' , but then assign it the value of max directly afterwards without using that value or storing it somewhere else.

  4. You assign array[0] after the first while(){...} but max has not been initialized, so it's "garbage".

  5. Here - if(max>=array[i]){} you don't do anything in the body??

  6. You assign max the value of b - again "garbage"!

  7. You should return ...; from your main function. You'll have to see for yourself the options you have there. Note: If you follow "1." you'll have no choice but to follow this one.

  8. Your formatting, if not in your "real" code, in the post is less than desirable (ie; it's not very easily read.

Fix those things and you'll probably fix the problem.

Change

  • array[0] = max to max = array[0] (otherwise max will take some garbage value)

  • max = b to b = max (otherwise b will take some garbage value)

  • putchar('x\\n') to { putchar('x'); putchar('\\n')} { putchar('x'); putchar('\\n')} .
    (It is important to note here, that putchar() prints only one character at once but you used putchar('x\\n') to print two characters x and \\n together!)

then your program will give desired output.
Here is your working code with changes mentioned above:

#include<stdio.h>

int main()
{
    int i,c,b, max;
    int array[3] = {0,0,0};


    while( (c = getchar()) != EOF){
       if(c == 'a')
            ++array[0];
       else if(c == 'b')
            ++array[1];
       else if(c =='\n' || c =='\t' || c == ' ')
            ;
       else ++array[2];
    }

 max = array[0];
 i=0;

 while(i<3){
    if(max < array[i])
        max = array[i];
i++;        
}

i=0;
b = max;
while(i < b){
    if(array[0] >= max)
        putchar('x');
    else 
        putchar(' ');


    if(array[1] >= max)
        putchar('x');
    else 
        putchar(' ');


    if (array[2] >= max)
    {
        putchar('x');
        putchar('\n');
    }
    else 
        putchar('\n');
    --max;
     ++i;


  }

printf("---\nabo\n  t\n  h\n  e\n  r");

return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM