Solved

Help on file processing

Posted on 2003-11-30
5
395 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:fongyl4
5 Comments
 
LVL 1

Expert Comment

by:Gratch06
ID: 9847065
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
 
LVL 20

Expert Comment

by:jmcg
ID: 9847656
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
 
LVL 3

Expert Comment

by:guynumber5764
ID: 9852662
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
 

Author Comment

by:fongyl4
ID: 9877777
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
 
LVL 3

Accepted Solution

by:
guynumber5764 earned 100 total points
ID: 9879277
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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question