Can anyone help me with this-- C++ Memorable Numbers program

Dec 9, 2011 at 5:03pm
Memorable Numbers
Standard telephone keypads contain the digits 0 through 9. The numbers 2 through 9 each have three letters associated with them, as is indicated by the following table:

Digit Letter Digit Letter
2 A B C 6 M N O
3 D E F 7 P Q R S
4 G H I 8 T U V
5 J K L 9 W X Y Z

Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might use the correspondence indicated in the above table to develop the seven-letter word “NUMBERS.”

Businesses frequently attempt to get telephone numbers that are easy for their clients to remember. If a business can advertise a simple word for its customers to dial, then no doubt the business will receive a few more calls. Each seven-letter word corresponds to exactly one seven-digit telephone number. The restaurant wishing to increase its take-home business could surely do so with the number 825-3688 (i.e., “TAKEOUT”). Each seven-digit phone number corresponds to many separate seven-letter words. Unfortunately, most of these represent unrecognizable juxtapositions of letters. It’s possible, however, that the owner of a barber shop would be pleased to know that the shop’s telephone number, 424-7288, corresponds to “HAIRCUT.” A veterinarian with the phone number 738-2273 would be pleased to know that the number corresponds to “PETCARE.”

Write a 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.



I have to do this for a presentation to show what we do in our C++ programming class to people that may want to take this class. I need some help on how to set this up. Please can anyone help me with this?
Last edited on Dec 9, 2011 at 5:04pm
Dec 9, 2011 at 7:17pm
Sure smells like homework.

This is trivial with regex. Build the expression, then test it to a word list. If you don't want to use regex:

Get a word list in text file version. If it is just for seven digit words, first trim the list to only seven digit words.

The easiest algorithm to implement would be test every string in your word list to see if it is a match. Start at the leftmost digit and work your way right. If the leftmost char is a match for the leftmost digit, then work your way right, until you determine that every char is a match for every number.
Dec 11, 2011 at 3:56am
Hmm.. I'm currently learning C++ right now (freshman in college) so I'm eager to give anything a shot to boost my capabilites. Don't flame me if I write code like a scrub, please. BUT, tips are always appreciated:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#inlcude <iostream>
#include <cctype>

using namespace std;

int main() {

     //I know that the numbers '9' and '7' have four letters, but I'd rather not create a
     //two-dimensional array with the dimensions of 8 * 4 and waste space
     char Number_Letter[8][3] = {{'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'I'}, {'J', 'K', 'L'}, {'M', 'N', 'O'}, {'P', 'Q', 'R'}, {'T', 'U', 'V'}, {'W', 'X', 'Y'}};
    
     //We have a to initialize the char array "Number_Name" with 8 elements since
     // the \0 terminator is stored in the last element
     char Number_Name[8];
     int Final_Number[7];

     cout << "Input a name: ";
     cin >> Number_Name;

     //We have to capitalize the char array "Number_Name" to be able to make comparisons
     //with the "Number_Letter" array. The "toupper" function is defined in the cctype library.
     Number_Name = toupper(Number_Name);

     for (int Index = 0, Name = 0; Index < 8, Name < 7; Index++) {
          for (int Letter = 0; Letter < 3; Letter++) {
               if (Number_Name[Name] == Number_Letter[Index][Letter]) {
                    Final_Number[Name] = Index + 2;
                    Name++;
                    Letter = 0;
               }
               if (Number_Name[Name] == 'S') { 
                    Final_Name[Name] = 7;
                    Name++;
                    Letter = 0;
               }
               if (Number_Name[Name] == 'Z')
                    Final_Name[Name] = 9;
                    Name++;
                    Letter = 0;
               }
          }
     }
     
     cout << "Your number is: " << Final_Number << endl;
     cin.get();
     return 0;
}


I haven't been able to test it (I'm pretty sure there's a few errors like when you have to capitalize "Number_Name", printing the char array, and inputting into "Number_Name", but overall I think it's correct) because at the moment I'm in a hotel with no compiler and whatnot. Anyways, this is just for educational and experimental purposes on my behalf. Shitty code, though, and it could use a lot more comments for better understanding. I avoided using numbers '0' and '1' like you asked so... I hope that wasn't a bad thing. Also, like I said, since I have no way of testing it at the moment, I'm not completely sure if it'll work :)





Last edited on Dec 13, 2011 at 1:52pm
Dec 11, 2011 at 4:17am
@Jason: Don't just give solutions. Sometimes you get OPs who will just steal your stuff and leave; not learning anything from it.
Dec 11, 2011 at 5:40am
Here's a tip Jason: Don't do other people's homework for them!
Dec 11, 2011 at 1:28pm
I honestly don't mind for this one. I only did it to get some practice in since I have finals next week. I shall heed that advice in the future, though.
Last edited on Dec 13, 2011 at 1:53pm
Dec 12, 2011 at 5:02pm
Thanks Jason and Intrexa! You pointed me in the right direction of what to do.

And it's not homework. If you clearly read, it says that I have to do this for a presentation to people who may want to take the class.
Last edited on Dec 12, 2011 at 5:02pm
Dec 12, 2011 at 5:13pm
Right. An because you said so it is 100% true. Not saying it isn't in your case, but there's clear evidence that other (not you) lazy people (again, maybe not you) do say things like that when in fact is not true.

An even if it weren't academic homework, I am still against doing other people's work. I'll gladly help out with concepts and corrections, but I usually never hand out a complete solution.

Again, pardon us if this is not your case. Don't make it personal. We just operate under safe parameters.
Dec 12, 2011 at 5:17pm
You can give a poor man a fish every day of your life, or you can teach him how to catch the fish himself and then move on.

The whole reason we don't like giving solutions is that it makes it much harder to get people to understand how to do things for themselves. And having to rely on others is a scary situation to be in...

http://www.cplusplus.com/articles/DjGEy60M/
Last edited on Dec 12, 2011 at 5:19pm
Dec 12, 2011 at 6:34pm
Yeah I can understand where you guys are coming from. I didn't mean for it to sound like homework. I was just hoping to be pointed in the right direction on where to start this thing.
Sorry if I did anything wrong.
Dec 12, 2011 at 9:34pm
Don't worry, you did nothing wrong ;)
It was Jason who went against the preferred standards here...
Topic archived. No new replies allowed.