Velocity Reviews > Re: This blows my mind.

# Re: This blows my mind.

ext_u
Guest
Posts: n/a

 08-21-2003
I do know what a histogram is.

My point was that how in the world do they expect me to make on having
just shown me how to set the values for arrays ( and doing a skim job
at that ).

A couple of you showed me some code for creating a histogram using
values and commands that havent even been covered in the book yet. So
I guess you proved my point even more. I am still going at the book
tho. Trying to figure out how to do it one thing at a time.

I posted my code in another thread, I was having trouble getting the
program to recognize what an actual word was.

Thanks,
Henry

Steve Zimmerman
Guest
Posts: n/a

 08-21-2003
ext_u wrote:

> I do know what a histogram is.
>
> My point was that how in the world do they expect me to make on having
> just shown me how to set the values for arrays ( and doing a skim job
> at that ).
>
> A couple of you showed me some code for creating a histogram using
> values and commands that havent even been covered in the book yet. So
> I guess you proved my point even more. I am still going at the book
> tho. Trying to figure out how to do it one thing at a time.
>
> I posted my code in another thread, I was having trouble getting the
> program to recognize what an actual word was.
>
> Thanks,
> Henry
>

I went through just what you're going through. Persevere; it's
worth it.

--Steve

/* just enter lower case alphabet, then Control-d when done */

#include <stdio.h>
#include <ctype.h>

#define MAXHIST 15 /* max length of histogram */

/* print horizontal histogram freq. of different characters */
main()
{
int c, i;
int len; /* length of each bar */
int maxvalue; /* maximum value for cc[] */
int cc[26]; /* character counters */

for (i = 97; i < 123; ++i)
cc[i] = 0;
while ((c = getchar()) != EOF)
if (c < 123)
++cc[c];
maxvalue = 0;
for (i = 97; i < 123; ++i)
if (cc[i] > maxvalue)
maxvalue = cc[i];

for (i = 97; i < 123; ++i) {
if (isprint(i))
printf("%5d - %c - %5d : ",
i, i, cc[i]);
else
printf("%5d - - %5d : ",
i, cc[i]);
if (cc[i] > 0) {
if ((len = cc[i] * MAXHIST / maxvalue) <= 0)
len = 1;
} else
len = 0;
while (len > 0) {
putchar ('*');
--len;
}
putchar('\n');
}
}

Bruno Desthuilliers
Guest
Posts: n/a

 08-21-2003
ext_u wrote:
> I do know what a histogram is.
>
> My point was that how in the world do they expect me to make on having
> just shown me how to set the values for arrays ( and doing a skim job
> at that ).
>
> A couple of you showed me some code for creating a histogram using
> values and commands that havent even been covered in the book yet. So
> I guess you proved my point even more.

No, because you don't need the functions and constructs used in the code
you're talking about to do this exercise. I did it a few years ago, only
using what you're supposed to know at this stage (and well, I didn't
know much more at that time...). The only things you need to know to
'draw' an histogram on a text output is how to print a character on a
text output, and how to loop.

Bruno

Richard Heathfield
Guest
Posts: n/a

 08-22-2003
Steve Zimmerman wrote:

Yes, it can be very dangerous seeking help here, since there's no guarantee

> I went through just what you're going through. Persevere; it's
> worth it.

That, at least, I can agree with.

> for (i = 97; i < 123; ++i)

Assuming a particular character set here is not only non-portable but also
completely unnecessary. Henry hasn't mentioned what system he is using, as
far as I know, so I see no justification for making non-portable
assumptions about his character set. I also see no reason for selecting out
this particular range of characters. The spec says nothing about them.

If you respond, please show that you're all growed up, by responding
"contributions" to this thread). Thank you.

--
Richard Heathfield : http://www.velocityreviews.com/forums/(E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

ataru@nospam.cyberspace.org
Guest
Posts: n/a

 08-22-2003
Steve Zimmerman <(E-Mail Removed)> broke the eternal silence and spoke thus:

> int cc[26]; /* character counters */

> for (i = 97; i < 123; ++i)
> cc[i] = 0;

This doesn't look right... cc has 26 elements, so accessing elements 97
through 123 of cc sounds like a bad plan. Perhaps you wanted cc[i%26]?

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |