0

I have a problem that I need some help in figuring out. I was hoping I could get a few pointers on a better way to approach what i'm doing. My main issue is a few lines below (//This is whats hanging me up) and described at the bottom of page.

I need to permutate all possible outcomes of a phone number: (not just dictionary words)

I.E. 222-2222

Should output a list 3^7 long with all the possible permutations of a,b,c

I.E.

AAAAAAA
AAAAAAB
AAAAAAC
AAAAABA   // THIS IS WHATS HANGING ME UP
AAAAABB
AAAAABC
AAAAACA   // HERE TOO AND SO ON

MY CODE (purposely shortened for testing) GIVES ME:

AAAA
AAAB
AAAC
AABC
AACC
ABCC
ACCC
BCCC
CCCC

I'm a beginning programming student so my knowledge goes as far as using for, while, if, statements and grabbing individual chars from the array.

Here's what my code looks like so far: (this is a part of a function. Code missing)

char alphaFunc(char n[]){

int d1=n[0]-48;
int d2=n[1]-48;
int d3=n[2]-48;
int d4=n[3]-48;
int d5=n[4]-48;
int d6=n[5]-48;
int d7=n[6]-48;
int a=0,b=0,c=0,d=0,e=0,f=0,g=0;

int i=0;

char charArray[10][4]={ {'0','0','0'},{'1','1','1'},{'A','B','C'},
        {'D','E','F'},{'G','H','I'},{'J','K','L'},{'M','N','O'},
        {'P','R','S'},{'T','U','V'},{'W','X','Y'}  };



while(i <=14){

    printf("%c%c%c%c\n", charArray[d1][a],
            charArray[d2][b],charArray[d3][c],charArray[d4][d],
            charArray[d5][e],charArray[d6][f],charArray[d7][g]);
    g++;

    if(g==3){
        g=2;
        f++;
    }
    if(f==3){
        f=2;
        e++;
    }
    if(e==3){
        e=2;
        d++;
    }

I'm not exactly looking for someone to do this for me I just need a little help in figuring out which sort of statement will work b/c when you have a digit get to CharArray[d-][a] location [3] and reset it to [0] it sends you to a different part of the loop. (hope that makes sense).

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • If that is a duplicate... It didn't help me solve what I'm after. :( – Lennard Linkogle Apr 20 '13 at 02:05
  • The links I provided show how to generate combinations of items from a list (you asked for "pointers to a better way", and I posted links to two). You can find the issue you're having with your code by using the debugger to step through it. Set a breakpoint on the line you have a question about, run with debugging until you stop at the breakpoint, and single-step through the code watching the variables to see what's happening with them. (t would help you if you used more readable variable names, too.) – Ken White Apr 20 '13 at 02:13
  • Thank you for your help. I was more looking for a quick hint on what type of loop I need to use IE (for, while, if..) and since this is a online class that is one step above beginning programming I don't know how to use a debugger b/c we log in to a linux server. – Lennard Linkogle Apr 20 '13 at 02:33
  • I have used gdb for some assembly programming. where I used: as -gstabs -o file1.o file1.s then ld -o ... ... – Lennard Linkogle Apr 20 '13 at 02:34
  • In the IDE you're using (you don't say what it is), there should be an option to `Debug`. I can't provide any more specifics, because you're not providing any. Are you using `Mono` or `C#.net` on Windows? What are you using to edit/run your code? – Ken White Apr 20 '13 at 02:37
  • @Lennard Linkogle The questions linked by Ken White are exactly what you are looking for. – Patashu Apr 20 '13 at 02:56
  • Are you sure you meant to tag this with C#, and not C++? You're using `printf()`... – Jon Senchyna Apr 20 '13 at 03:33
  • 2
    The way to do this if you're a beginner is to make *nested loops*. You'll be nesting them seven deep. Each loop runs over the possible letters associated with each digit. – Eric Lippert Apr 20 '13 at 03:40
  • I don't believe this is a duplicate. I tried using the code in that question and it didn't solve my problem at all. Would've helped if this wasn't marked as a dupe. – Sid Nov 27 '14 at 15:38

1 Answers1

1

Since the values of charArray are constant, I would recommend making it a global variable, rather than declaring it in your function. In addition, since some numbers have 4 letters, whereas others have 3, you may want to look into using a jagged array to represent it.

As far as printing the permutations you can get from a phone number, I think recursion is going to be your friend. Assuming you can store the phone number in an int array, the following should work:

public void printPermutations(int[] phoneNumber)
{
   printPermutations(phoneNumber, 0, String.Empty);
}

private void printPermutations(int[] phoneNumber, int index, string permutation)
{
   if(index >= phoneNumber.Length)
   {
       // If we've reached the end, print the number
       printf(permutation + "\n");
   }
   else
   {
       // Otherwise, generate a permutation for each
       // character this digit can be
       int digit = phoneNumber[index];
       char[] chars = charArray[digit];
       for (int i = 0; i < chars.Length; i++)
       {
           printPermutations(phoneNumber, index+1, permutation + chars[i]);
       }
   }
}
Jon Senchyna
  • 7,867
  • 2
  • 26
  • 46