Word Histogram in C++??

W

WreckingCru

I've been assigned to design a function that takes in a string and
creates a word histogram of the letters.
So - "hello hi" should give frequencies of h:2, l:2 etc....

I've also told to use maps (the container class) to implement it..


Can anyone help me with pseudo code or actual code - beacuse I AM LOST
! :(

Pranav
 
G

Gianni Mariani

WreckingCru said:
I've been assigned to design a function that takes in a string and
creates a word histogram of the letters.
So - "hello hi" should give frequencies of h:2, l:2 etc....

I've also told to use maps (the container class) to implement it..


Can anyone help me with pseudo code or actual code - beacuse I AM LOST
! :(

declare counter array indexed by character

get string

for each "character" of the string
increment counter at element "character"

print results
 
M

Mike Wahler

WreckingCru said:
I've been assigned to design a function that takes in a string and
creates a word histogram of the letters.
So - "hello hi" should give frequencies of h:2, l:2 etc....

I've also told to use maps (the container class) to implement it..


Can anyone help me with pseudo code or actual code - beacuse I AM LOST

As soon as you show it to us, the help will begin. :)

Be sure to ask specific questions when you post
your code.

HTH,
-Mike
 
D

David B. Held

WreckingCru said:
[...]
Can anyone help me with pseudo code or actual code -
beacuse I AM LOST
! :(

This is like a writer asking someone to write a story for
him, so he can then take credit for it as his own work. And
it makes about as much sense to do. If you are a computer
science student, you need to understand this one principle
right away: programming is not about learning a computer
language; it is about problem solving. Once you learn how
to solve a problem, the process of translating the solution
into code should be almost mechanical (with a bit of art
thrown in, if you ask Knuth). Doing your homework for you
isn't going to help you learn how to solve problems. If you
want examples of code, there are thousands of tutorials on
the internet. But your best bet is to simply attempt to solve
the problem yourself, by hand, with a pencil and paper, starting
out with trivial examples, and expanding to harder ones. If
you can manage to do that, writing the corresponding code
will seem fairly easy by comparison. If you can't, you should
consider another profession. ;)

Dave
 
M

Mattias Ekholm

At the end is some C code I use for a similar purpose, the code reads
from stdin and emits a histogram on stdout.

....and I hope for your sake it's not a assignment in school!

/Mattias

Mike said:
As soon as you show it to us, the help will begin. :)

Be sure to ask specific questions when you post
your code.

HTH,
-Mike

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

#define ROWS 7

const char *valid =
"!\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`abcdefghijklmnopqrstuvwxyzåäö{|}~";

int
main (void)
{
static int stat[256];
int max = 0;

setlocale (LC_CTYPE, "sv_SE");

for (int c = getc (stdin); !feof (stdin); c = getc (stdin)) {
++stat[tolower(c)];
}

for (const char *cp = valid; *cp; ++cp) {
if (max < stat[(int) *cp]) {
max = stat[(int) *cp];
}
}

for (int i = ROWS - 1; i >= 0; --i) {
for (const char *cp = valid; *cp; ++cp) {
int val = 2 * ROWS * stat[(int) *cp] / max - 2;
putchar (" . :"[((val > i * 2 - 1) << 1) | (val > (i - 1) * 2)]);
}

putchar ('\n');
}

puts (valid);

return 0;
}
 
O

osmium

Mattias said:
At the end is some C code I use for a similar purpose, the code reads
from stdin and emits a histogram on stdout.

...and I hope for your sake it's not a assignment in school!

The code you provide does not comply with the assignment. "I've also [been]
told to use maps ...".

*Told* is not a hint, it is a part of the assignment.
/Mattias



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

#define ROWS 7

const char *valid =
int
main (void)
{
static int stat[256];
int max = 0;

setlocale (LC_CTYPE, "sv_SE");

for (int c = getc (stdin); !feof (stdin); c = getc (stdin)) {
++stat[tolower(c)];
}

for (const char *cp = valid; *cp; ++cp) {
if (max < stat[(int) *cp]) {
max = stat[(int) *cp];
}
}

for (int i = ROWS - 1; i >= 0; --i) {
for (const char *cp = valid; *cp; ++cp) {
int val = 2 * ROWS * stat[(int) *cp] / max - 2;
putchar (" . :"[((val > i * 2 - 1) << 1) | (val > (i - 1) * 2)]);
}

putchar ('\n');
}

puts (valid);

return 0;
}
 
T

TechNovice

This reminds me of my computer science student days...asking for others to
solve my homework. The best advice I have for you is to do it yourself.

I think the purpose of the excercise is for you to learn how to use maps, so
try to find some good examples on the internet on how to use maps. With this
knowledge you can then create a map of characters and solve your problem.

Another advice, when you post homework questions try to put some code with
it, otherwise you won't get much help; trust me I've been there, I've done
that.

Good luck :)
 
A

Alan Morgan

I've been assigned to design a function that takes in a string and
creates a word histogram of the letters.
So - "hello hi" should give frequencies of h:2, l:2 etc....

I've also told to use maps (the container class) to implement it..


Can anyone help me with pseudo code or actual code - beacuse I AM LOST
! :(

It's too bad your teacher and/or teacher's assistant won't help you and
that this material wasn't covered in class and that the textbook doesn't
go into any of the details of how to read strings and use the map
container.

I don't know how you'll manage.

Perhaps you should drop the class?

Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top