Link to home
Start Free TrialLog in
Avatar of fongyl4
fongyl4

asked on

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.
Avatar of Gratch06
Gratch06

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
}
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.
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.
Avatar of fongyl4

ASKER

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?                                  






   




                                           
ASKER CERTIFIED SOLUTION
Avatar of guynumber5764
guynumber5764

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial