Public Group

[C] Telephone Number Word Generator

This topic is 3688 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Here's the setup: Online C class. I'm having issues with one of my assignments Here's the instructions: NOTE - the books it's from is C How to Program 5th edition, chapter 11.
/****************************************************************
*Exercise 11.13:  Telephone Number Word Generator.  Standard	*
*telephone keypads contain thte digits 0 through 9.  The	*
*numbers 2 through 9 each have three letters associated with	*
*them, as inidcated by the following table:			*
*2	A B C		6	M N O		         *
*3	D E F		7	P R S			*
*4	G H I		8	T U V			*
*5	J K L		9	W X Y			*
*Write a C program that, given a seven-digit number, writes	*
*to a file every possible seven-letter word corresponding to	*
*that number.  There are 2187 (3 to the seventh power) such	*
*words.  Avoid phone numbers with the digits 0 and 1.		*
*							*
*Cooper's Instructions: Letters in the output file are		*
*uppercase.  Do not accept phone numbers with a zero or one.	*
*When the user runs the program, prompt them for a phone	*
*number.  If it is invalid, exit with an appropriate comment.   *
****************************************************************/


The part I'm getting stuck at is printing out all of the different word combinations. According to the text for each 7 digit number there are 2187 possible words. I've tried about 3 or 4 different ideas and thrown out about 5 more (all of which involve 5+ for loops and 6+ different counters). So after 5 straight hours of trying to do this assignment I find myself stuck. I'm having trouble visualizing how to go about solving this. So I've come to GameDev for some help. I'm not looking for an answer, but a nudge in the right direction. Something to help me understand how I should go about solving this. Because at the moment all my ideas seem to involve too many for loops, and too many integer counters, and it all just seems so jumbled and messy. I'm also going to email my professor, but he isn't exactly prompt at answering emails. He teachers like 6 other classes so...yeah. Any ideas, hints, tips? Thanks for your time.

Share on other sites
Try counting from 0 to 2187 in base 3 (and displaying each base 3 digit alphabetically)

Does that give you any ideas?

Share on other sites
Somewhat, but I've never actually counted (in coding form) in anything but base 10.

Also I didn't mention this above, but the way that I have been handling the letter assigned to the different numbers on the phone is using arrays for each number. Such as:

char c_Two[3] = {'A', 'B', 'C'};
char c_Three[3] = {'D', 'E', 'F'};
etc...

Is this a good way to handle the letters or would it be easier to handle if I just put all the letters in a single array? Or does it even matter?

Share on other sites
you could make a multi-dimensional array (idk if c/C++ even can) to make it look more like a phone.

personally i think you should make a loop. using the multidimensional array could save u coding, and work just as fast. since now instead of counting through each array, you count thru one, and for every hit, itd just take another 2 write statements. (hopefully this made sense...)

Share on other sites
I would suggest a recursive solution. You could create a function that accepts the phone number, a pointer to some data structure you are storing answers in, and a current "index" into the phone number. The function could recurse on each possible letter at the digit specified by the index, to get all possible words.

Since this is homework, I can't show you an implementation, but that's an approach you should look at, I think.

Share on other sites
In reguards to smitty's post, for that I would need some sort of base check to make sure that it's not infinite. Could that be a counter check such as 2187 - the number of words to be stored in the file? Recursive functions were only briefly covered in the text we have and I don't have any experience with them so I might not be understanding this correctly.

Share on other sites
Quote:
 Original post by KoolchampIn reguards to smitty's post, for that I would need some sort of base check to make sure that it's not infinite. Could that be a counter check such as 2187 - the number of words to be stored in the file? Recursive functions were only briefly covered in the text we have and I don't have any experience with them so I might not be understanding this correctly.
It's not infinite... you would quit recursing when the index is the last digit's index. If you haven't studied much recursion then perhaps it isn't the answer you are expected to give.

Share on other sites
Since we've gone over recursion somewhat my professor wouldn't care, but like I said I haven't used it before and it still kind of confuses me, so puts me off from using it somewhat. But it's definetely an option and I'll continue to mull it over.

Share on other sites
For an input of zero digits, there is one possible output, of zero length.

For an input of N > 0 digits, there are three times as many outputs as for (N - 1) digits, each of N length. We construct them by, for each of three possible symbols for the first digit, for each result for the other digits, outputting the symbol followed by the result.

Share on other sites
Unfortunetly I'm a little slow on the uptake when it comes to math (which doesn't bode well for me to become a programmer). So let me see if I understand this correctly:

For a seven digit number, the seventh digit will loop threw the three letter alternating them each time for each word, such as:
A
B
C
A
B
C
etc...

Then the sixth digit (N - 1) will alternate the letter every 3 iterations of the seventh digit, such as:
A
A
A
B
B
B
C
C
C
etc...

Am I even in the ballpark with this thinking? Or as is most likly the case am I way off?

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• Forum Statistics

• Total Topics
633677
• Total Posts
3013284
×