473,473 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Jul 19 '05 #1
7 6387
WreckingCru wrote:
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

Jul 19 '05 #2
"WreckingCru" <pr*********@hotmail.com> wrote in message
news:40**************************@posting.google.c om...
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
Jul 19 '05 #3
"WreckingCru" <pr*********@hotmail.com> wrote in message
news:40**************************@posting.google.c om...
[...]
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 (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
Jul 19 '05 #4

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:
"WreckingCru" <pr*********@hotmail.com> wrote in message
news:40**************************@posting.google.c om...
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;
}

Jul 19 '05 #5
Mattias Ekholm writes:
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

Mike Wahler wrote:
"WreckingCru" <pr*********@hotmail.com> wrote in message
news:40**************************@posting.google.c om...
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;
}

Jul 19 '05 #6
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" <pr*********@hotmail.com> wrote in message
news:40**************************@posting.google.c om...
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

Jul 19 '05 #7
In article <40**************************@posting.google.com >,
WreckingCru <pr*********@hotmail.com> wrote:
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
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Oracle3001 | last post by:
Hi All, I am trying to use JAI to build a histogram of an image i have. I have posted the code below, and the error I get at runtime. I have taken the code from the offical java examples, so I am...
1
by: bleh | last post by:
....to include a removeData(datatoremove) function, to mirror the existing addData(datatoadd) function. If anybody knows of somewhere where this has been done already, if you could point me in...
12
by: KraftDiner | last post by:
Hi, I wrote a C++ class that implements an n dimensional histogram in C++, using stl maps and vectors. I want to code this up now in Python and would like some input from this group. The C++...
5
by: Enigma Curry | last post by:
I'm playing around with matplotlib for the first time. I'm trying to make a very simple histogram of values 1-6 and how many times they occur in a sequence. However, after about an hour of...
2
by: Daniel Nogradi | last post by:
How does one do a histogram on only a part of an image? This is what I found in the PIL documentation about histogram( ): """ im.histogram(mask) =list Returns a histogram for those parts of...
5
by: arnuld | last post by:
this is a programme that counts the "lengths" of each word and then prints that many of stars(*) on the output . it is a modified form of K&R2 exercise 1-13. the programme runs without any...
2
by: fatenn | last post by:
I need make from the Oracle Text index of the "word-frequency histogram", this is list of the tokens in this index, where each token contains the list of documents that contain that token and...
1
by: avenger3200 | last post by:
Hello everyone, I am trying to make a histogram for a project in my class and I really need some help. Here is the question that my instructor provided: Create 1000 Random numbers between...
0
by: Kurt Smith | last post by:
On Fri, Jul 25, 2008 at 5:02 PM, aditya shukla <adityashukla1983@gmail.comwrote: the 'bins' argument to pylab.hist() is supposed to be an integer or a list of the bins' lower edges. The default...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.