• C

Help on file processing

Hi, I'm a beginner programmer in C programming. I had this problem in my homework. I don't really know how to approach this question. Can anyone please guide me in this question? I'm given a standard telephone pad containing the digits 0 to 9(actually 2 to 9) just like this:
       2      ABC
       3      DEF
       4      GHI
       5      JKL
       6      MNO
       7      PRS
       8      TUV
       9      WXY
I'm asked to write a program that writes a file every possible seven-letter word corresponding to that number. For example, when I input BIGBOYS, the phone number will generate 2442697.
fongyl4Asked:
Who is Participating?
 
guynumber5764Commented:
You've still described two problems (letters to digits and digits to letters).  Are you supposed to do both?  If so, tackle digits to letters first.  It is much easier and, if you use a map like:

char KeypadLetters[8][3] = {{'A','B','C'},{'D' ...

you may find that that solution naturally leads to the solution of the harder problem.

I probably should have used the name "result" instead of "letters" in my example.  To see what to do next, replace "wordGenerator[n]" with "result[n]" in all the loops.  Then get it to compile.

The "letters" ("results") array in the above example would be the buffer to store the result so that inside the innermost loop you can just print all 7 letters at once.  Each one would be a single letter calculated based on the digit and loop iteration.

0
 
Gratch06Commented:
pseudo code...

string getNumericalValue(string inputString){
    string tempString;
    Loop through each character of inputString{
        switch(char in string)
            case 'a':
            case 'b':
            case 'c':
                 tempString += '2';
                 break;
            .....etc for each character.
            case default:
                 tempString += '?';     // this is for the "unknown" characters such as q or z (not on numpad)
    }
    return tempString
}
0
 
jmcgOwnerCommented:
Many telephone keypads have added back in the letters that were traditionally missing:

       2      ABC
       3      DEF
       4      GHI
       5      JKL
       6      MNO
       7      PQRS
       8      TUV
       9      WXYZ

=======================

You've described two different problems, which may indicate why you have difficulty getting started. In your example you describe a simple decoding of alphabetical phone "number" into digits. This is the task that Gratch06 has outlined for you in pseudocode. I'd have done it with a table lookup. but either way it's quite a simple task.

What's a little harder is the "word generator" direction: given a 7-digit phone number, come up with all the possible "spellings". For that, you might be tempted to look at algorithms that generate all the possible permutations of a set. This problem has some similarities. You could, for instance, write a recursive function that calls itself 3 (or 4, if you use the expanded keypad mapping above) times, once for each letter assigned to the "current digit", passing the remaining digits and the thus-far accumulated "word".

I hope that's enough of a homework hint. If you get stuck, show us the code you have written so far and we'll be glad to help.
0
 
guynumber5764Commented:
To clarify what jmcg said:

>>> I'm asked to write a program that writes a file every possible seven-letter word corresponding to that number.
(change digits to letters)
 
and
>>> when I input BIGBOYS, the phone number will generate 2442697.
(change letters to digits)

are opposite problems.

As he says, the second problem is fairly simple since there is only one answer.  The first one is fairly involved since their are around 3^7 possible combinations.  This would usually be written using a recursive algorithm (a function that calls itself) but could also be written using nested for loops (one loop for each digit position...one iteration for each letter)

for(i0=0; i0<3; i0++)
{
    letters[0] = ...
    for(i1=0;i1<3;i1++)
     {
            etc.  


E.
0
 
fongyl4Author Commented:
I'm sorry, I've accidentally pressed something and everything is gone. Just in case the earlier comment was deleted, here I sincerely apologized for the late comment. The actual question was to change the letteers to digits and to write a program that generate every possible seven-letter word corresponding to the numbers that the users had entered(ignoring the number 0 and 1). This is what I have so far:

#include<stdio.h>

struct wordGenerator
{
      int number;
      char alpha[10];
};

int main()
{
      int i;
      struct wordGenerator[10]=("","","2","3","4","5","6","7","8","9");
}

int wordGenerator()
{
                int i;
                for(i=0;0<3;i++)
                  {
                     wordGenerator[0] = "";
                     for(i=0;0<3;i++)
                        {
                           wordGenerator[1] = "";
                           for(i=0;i<3;i++)
                           {
                             wordGenerator[2] = "A,B,C";
                              for(i=0;i<3;i++)
                                  {
                                   wordGenerator[3] = "D,E,F";
                                   for(i=0;i<3;i++)
                                       {
                                        wordGenerator[4] = "G,H,I";
                                         for(i=0;i<3;i++)
                                              {
                                               wordGenerator[5] = "J,K,L";
                                                for(i=0;i<3;i++)
                                                    {
                                                      wordGenerator[6] = "M,N,O";
                                                      for(i=0;i<3;i++)
                                                          {
                                                           wordGenerator[7] = "P,R,S";
                                                           for(i=0;i<3;i++)
                                                               {
                                                                 wordGenerator[8] = "T,U,V";
                                                                 for(i=0;i<3;i++)
                                                                      {
                                                                       wordGenerator[9] = "W,X,Y";
                                                                       for(i=0;i<3;i++)
                                                                       }
                                                                }
                                                           }
                                                      }
                                                }
                                        }
                                  }
                          }
                  }
 }  
How do I proceed from here?                                  






   




                                           
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.