Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

c++ Arrays Challenge

44 views
Skip to first unread message

Jesse James

unread,
Apr 29, 2003, 9:58:00 PM4/29/03
to
I'm writng this program for my C++ class
if anybody is up to the challenge of helping me I'll appreciate it
very much,

The problem with Program number 1 is that I need to somehow determine the
number of letters each word has and display/keep count of it.
I'm not sure how to do this..

1 - Write a program that prompts the user to enter a word. Store the word in
a one-dimensional character array that can store a maximum of 10 characters
and the null character. After storing the word in the array, the program
should then prompt the user to enter a letter. The program should count how
many times the letter appears in the word, then display the value on the
screen. The program should include a loop that allows the user to enter as
many words as desired. It should also include a loop that allows the user to
enter as many letters as desired. For example, if the user enters the word
ball, the program should allow the user to count how many times the letter a
appears in the word, how many times the letter x appears in the word, and so
on. When the user is finished with one word, the program should allow him or
her to enter another word.

David White

unread,
Apr 29, 2003, 10:36:03 PM4/29/03
to
Jesse James <Pri...@please-nospam.com> wrote in message
news:1eydnaDL7vU...@comcast.com...

> I'm writng this program for my C++ class
> if anybody is up to the challenge of helping me I'll appreciate it
> very much,

What do you mean by 'help'?

> The problem with Program number 1 is that I need to somehow determine the
> number of letters each word has and display/keep count of it.
> I'm not sure how to do this..

How to do what exactly?

> 1 - Write a program that prompts the user to enter a word. Store the word
in
> a one-dimensional character array that can store a maximum of 10
characters
> and the null character. After storing the word in the array, the program
> should then prompt the user to enter a letter. The program should count
how
> many times the letter appears in the word, then display the value on the
> screen. The program should include a loop that allows the user to enter as
> many words as desired. It should also include a loop that allows the user
to
> enter as many letters as desired. For example, if the user enters the word
> ball, the program should allow the user to count how many times the letter
a
> appears in the word, how many times the letter x appears in the word, and
so
> on. When the user is finished with one word, the program should allow him
or
> her to enter another word.

There are a number of things you need to do, which are given in the
question:
- define a character array
- prompt the user for a word
- retrieve and store the user's input
- prompt the user for a letter
- count the occurrences of the letter in the word
- display the count on the screen
- do the above in the loops specified
There are other things the question implies, such as how the user indicates
that he has finished with a word.

Exactly which parts do you need help with, or were asking for someone to
write the whole thing?

David

osmium

unread,
Apr 30, 2003, 12:48:13 AM4/30/03
to
Jesse James writes:

> I'm writng this program for my C++ class
> if anybody is up to the challenge of helping me I'll appreciate it
> very much,
>
> The problem with Program number 1 is that I need to somehow determine the
> number of letters each word has and display/keep count of it.
> I'm not sure how to do this..

I don't think that *is* the intent. The end of the word is always indicated
by the null character. Don't try to collect data from the word, just store
it and examine it each time a new query character is entered.

Jesse James

unread,
Apr 29, 2003, 11:12:38 PM4/29/03
to
I'm not sure how to put a letter into an array.
I mean how can you take for example the word "apple" and put each letter
into an array.
I've never used an array in this form before.

thanx
"David White" <n...@email.provided> wrote in message
news:FzGra.3417$e8.1...@nasal.pacific.net.au...

David White

unread,
Apr 29, 2003, 11:48:36 PM4/29/03
to
Jesse James <Pri...@please-nospam.com> wrote in message
news:ruOdnXtmmI7...@comcast.com...

> I'm not sure how to put a letter into an array.
> I mean how can you take for example the word "apple" and put each letter
> into an array.
> I've never used an array in this form before.
>
> thanx

As osmium said, I don't think that's what you were asked to do. You just
have to store the word, such as "apple", in a character array as it is, and
then count the number of times the letter entered by the user appears in it.
That is, count the letter _after_ each time the user enters one. So you
don't have to do anything before the user enters a letter except store the
word in its present form. What you proposed is more complicated than
necessary, and, I'm pretty sure, not what your teacher/book expects you to
do.

David

Jesse James

unread,
Apr 30, 2003, 1:19:24 AM4/30/03
to
Well that's all great, if I had any clue on how to go about it.
I some how need to do a loop that reads all the individual letters and keep
a count.

If you know how to do this can you write that piece of code. or point me
somewhere where I can learn this,

TX


"David White" <n...@email.provided> wrote in message

news:HDHra.4297$e8.1...@nasal.pacific.net.au...

David White

unread,
Apr 30, 2003, 2:18:46 AM4/30/03
to
Jesse James <Pri...@please-nospam.com> wrote in message
news:lNycnUJ10be...@comcast.com...

> Well that's all great, if I had any clue on how to go about it.
> I some how need to do a loop that reads all the individual letters and
keep
> a count.
>
> If you know how to do this can you write that piece of code. or point me
> somewhere where I can learn this,

As I said, you don't need to keep a count. You just count the letter when
it's entered, display it, then throw it away. Here is the skeleton of a
program that does what you want. This is probably far more help than
students normally get here, since the whole idea is for you to do it
yourself. But you will have to do the rest.


int main()
{
while(true) // Outer infinite loop
{
const int maxLetters = 10;
char word[maxLetters+1]; // +1 for null character

// Place code to prompt user for word here
// Place code to input word here
// Place code to conditionally break out of loop here
// (e.g., on empty word)

while(true) // Inner infinite loop
{
char letter;

// Place code to prompt user for letter here
// Place code to input letter here
// Place code to conditionally break out of loop here

int count = 0; // Count starts at zero
int i = 0; // Start at first letter in word
while(word[i] != '\0') // Loop till null (end of word) found
{
// Place code here to add 1 to count if current
// letter in word matches input letter
// Place code to move to next letter here
}
// Place code to output count here
}
}
return 0;
}

David

Jon Bell

unread,
Apr 30, 2003, 2:42:54 AM4/30/03
to
In article <1eydnaDL7vU...@comcast.com>,

Jesse James <Pri...@please-nospam.com> wrote:
>
>1 - Write a program that prompts the user to enter a word. Store the word in
>a one-dimensional character array that can store a maximum of 10 characters
>and the null character. After storing the word in the array, the program
>should then prompt the user to enter a letter. The program should count how
>many times the letter appears in the word, then display the value on the
>screen. The program should include a loop that allows the user to enter as
>many words as desired. It should also include a loop that allows the user to
>enter as many letters as desired. For example, if the user enters the word
>ball, the program should allow the user to count how many times the letter a
>appears in the word, how many times the letter x appears in the word, and so
>on. When the user is finished with one word, the program should allow him or
>her to enter another word.

Tip: Don't try to write the whole thing all at once.

First, write a program that just asks the user to enter a word, then
displays it, then loops back and asks the user for another word, etc.
That's the first loop that the description mentions. Get this working to
your satisfaction before going any further.

Then, add code to the program so that for each word, it asks the user to
enter a letter, displays the number of times that letter occurs in the
word, then loops back and asks for another letter, etc. All this code
should be contained completely inside the loop that you wrote for the
first stage. IN fact, if you've covered functions already, you could make
a function out of it and call that function inside the loop for the first
stage. That will impress your instructor, believe me! ;-)

--
Jon Bell <jtbe...@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA

Jesse James

unread,
Apr 30, 2003, 9:16:18 PM4/30/03
to
Hey Jon, are you a teacher? I've seen your post around this group and they
are very good.

thanks.


Jon Bell

unread,
May 6, 2003, 3:07:01 PM5/6/03
to
In article <ruOdnXtmmI7...@comcast.com>,

Jesse James <Pri...@please-nospam.com> wrote:
>I'm not sure how to put a letter into an array.

char word[11]; // has space for 10 chars plus a terminating null
cout << "Gimme a word: "
cin >> word;

Now, if you enter the word 'apple', the array will contain

word[0] --> 'a'
word[1] --> 'p'
word[2] --> 'p'
word[3] --> 'l'
word[4] --> 'e'
word[5] --> '\0'
word[6] through word[10] --> undefined; treat them as random garbage

Note carefully:

1. Reading an entire sequence of data into an array in one swell foop
works *only* for arrays of char. For arrays of ints, doubles, widgets,
etc., you have to read one item at a time in a loop.

2. The code above gives undefined results if you try to enter a word that
contains more than 10 chars. In the context of your assignment you're
probably not supposed to worry about this, but in Real World Programming
you most certainly *do* have to worry about things like this. That's why
Real C++ Programmers Who Know What They're Doing will usually use the
standard string data type, unless they have a very good reason to do
otherwise:

#include <string>

using namespace std;

// blah blah blah

string word;
cout << "Gimme a word: ";
cin >> word;

In this case 'word' automatically expands as necessary to accommodate
whatever the user types in. No need to worry about overflow.

kizo...@gmail.com

unread,
Oct 10, 2015, 11:38:40 AM10/10/15
to
hello.
h

red floyd

unread,
Oct 10, 2015, 4:26:56 PM10/10/15
to
On 10/10/2015 8:38 AM, kizo...@gmail.com wrote:
[blatant "do my homework for me" redacted]
>

Your answer may be found here:

https://www.cs.rit.edu/~mjh/docs/c++-faq/how-to-post.html#faq-5.2

Please show what you have done so far. Do not expect us to do
your assignment for you.


bartekltg

unread,
Oct 10, 2015, 4:38:33 PM10/10/15
to
His homework was to go to the usenet and write a post.
He has failed because he has answered 'hallo' to a post from
2003 about c++ homework;-)

bartekltg

0 new messages