 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
WreckingCru Guest
|
Posted: Thu Oct 16, 2003 12:36 am Post subject: Word Histogram in C++?? |
|
|
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
|
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Thu Oct 16, 2003 12:53 am Post subject: Re: Word Histogram in C++?? |
|
|
WreckingCru wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Thu Oct 16, 2003 1:10 am Post subject: Re: Word Histogram in C++?? |
|
|
"WreckingCru" <pranavkabra (AT) hotmail (DOT) com> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
David B. Held Guest
|
Posted: Thu Oct 16, 2003 6:49 am Post subject: Re: Word Histogram in C++?? |
|
|
"WreckingCru" <pranavkabra (AT) hotmail (DOT) com> wrote
| Quote: | [...]
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
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ([url]http://www.grisoft.com)[/url].
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
|
|
| Back to top |
|
 |
Mattias Ekholm Guest
|
Posted: Sat Oct 18, 2003 9:44 am Post subject: Re: Word Histogram in C++?? |
|
|
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 Wahler wrote:
| Quote: | "WreckingCru" <pranavkabra (AT) hotmail (DOT) com> wrote in message
news:40b762ae.0310151636.4433e7fa (AT) posting (DOT) google.com...
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
|
#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;
}
|
|
| Back to top |
|
 |
osmium Guest
|
Posted: Sat Oct 18, 2003 2:03 pm Post subject: Re: Word Histogram in C++?? |
|
|
Mattias Ekholm writes:
| Quote: | 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.
| Quote: |
/Mattias
Mike Wahler wrote:
"WreckingCru" <pranavkabra (AT) hotmail (DOT) com> wrote in message
news:40b762ae.0310151636.4433e7fa (AT) posting (DOT) google.com...
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
#include <stdio.h
#include
#include
#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;
}
|
|
|
| Back to top |
|
 |
TechNovice Guest
|
Posted: Thu Oct 23, 2003 2:14 am Post subject: Re: Word Histogram in C++?? |
|
|
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 :-)
"WreckingCru" <pranavkabra (AT) hotmail (DOT) com> wrote
| Quote: | 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
|
|
|
| Back to top |
|
 |
Alan Morgan Guest
|
Posted: Thu Oct 23, 2003 2:18 am Post subject: Re: Word Histogram in C++?? |
|
|
In article <40b762ae.0310151636.4433e7fa (AT) posting (DOT) google.com>,
WreckingCru <pranavkabra (AT) hotmail (DOT) com> wrote:
| Quote: | 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
--
Defendit numerus
|
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|