0

This program is expecting the user to input a 7-digit number (except 1 and 0), and any digit has a corresponding set of letters (2=ABC, 3=DEF, 4=GHI, 5=JKL, 6=MNO, 7=PRS, 8=TUV, 9=XYZ, as found on phones in the USA). Finally, it should output all 2187 possible sequences of letters.

ex: input 2345678 output should be ADGJMPT ADGJMPU ADGJMPV ADGJMRT ADGJMRU ADGJMRV ADGJMST..........CFILOSV

but my output always is AAAAAAA AAAAAAB AAAAAAC..........CCCCCCC

(I also have trouble in checking number. I first set a loop and array, if (che[1] != 1 && che[0] != 1) break; but sometimes it doesn't break.)

Can you explain what's wrong?

#include<stdio.h>

int main(){
    int che[50] = { 0 };

    int a, b, c, d, e, f, g, i, r, q, number, check;

    char word2[7];

    char word1[8][3] = {
            { '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 (1)
    {
        printf("Enter seven digit(except 0 and 1):");
        scanf("%d", &number);

        check = number;
        for (; number != 0; number /= 10)
        {
            q = number % 10;
            che[q] = 1;
        }
        if (che[1] != 1 && che[0] != 1) break;
    }
    number = check;

    for (i = 6; number == 0; i--)
    {
        r = number % 10;
        if (r == 2){ word2[i] = 0; }
        if (r == 3){ word2[i] = 1; }
        if (r == 4){ word2[i] = 2; }
        if (r == 5){ word2[i] = 3; }
        if (r == 6){ word2[i] = 4; }
        if (r == 7){ word2[i] = 5; }
        if (r == 8){ word2[i] = 6; }
        if (r == 9){ word2[i] = 7; }
        number /= 10;
    }

    for (a = 0; a < 3; a++){

        for (b = 0; b < 3; b++){

            for (c = 0; c < 3; c++){

                for (d = 0; d < 3; d++){

                    for (e = 0; e < 3; e++){

                        for (f = 0; f < 3; f++){

                            for (g = 0; g < 3; g++){
                                printf("%c%c%c%c%c%c%c ",word1[word2[0]][a], word1[word2[1]][b], word1[word2[2]][c], word1[word2[3]][d], word1[word2[4]][e], word1[word2[5]][f], word1[word2[6]][g]);
                            }
                        }
                    }
                }
            }
        }
    }

    return 0;
}
1
  • codeSmellHomework++; Commented Nov 14, 2014 at 13:33

2 Answers 2

2

Main problem is here:

for (i = 6; number == 0; i--)

The loop condition is the opposite of the one it should be. You want to keep iterating on the number until you reach 0 (by successively dividing it by 10).

It should be

for (i = 6; number != 0; i--)

or

for (i = 6; i >= 0; i--)

In addition mind that

    if (r == 2){ word2[i] = 0; }
    if (r == 3){ word2[i] = 1; }
    if (r == 4){ word2[i] = 2; }
    if (r == 5){ word2[i] = 3; }
    if (r == 6){ word2[i] = 4; }
    if (r == 7){ word2[i] = 5; }
    if (r == 8){ word2[i] = 6; }
    if (r == 9){ word2[i] = 7; }

is equivalent to

 if (r >= 2 && r <= 9)
   word2[i] = r - 2;
Sign up to request clarification or add additional context in comments.

1 Comment

So appreciate for your help !!! I really want to raise your reputation but my reputation so low ,SORRY!!
1

I think this works as you expected: For some convenience in debugging, I changed some IO format:

Input separated number 2-9 divided by space and end with -1.

The input sequence can be of any length, just end with -1.

For example, input:2 3 4 2 -1

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

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

void translate(char * headStr,int * pattern,int pos_to_do)
{
    if(pattern[pos_to_do]<0)
    {
        printf("%s\n",headStr);
        return;
    }
    char str[3][20];
    int i;
    for(i=0;i<3;i++)
    {
        strcpy(str[i],headStr);
        char str_this[2];
        str_this[0]=word1[ pattern[pos_to_do] ][i];
        str_this[1]='\0';
        strcat(str[i],str_this);
        translate(str[i],pattern,pos_to_do+1);
    }
    return;
}

int main(){
    int che[20];

    int number, check,len;

    while (1)
    {
    printf("Enter digits(except 0 and 1):");
    len=0;
    scanf(" %d", &number);
    while(number>=2)
    {
        che[len]=number-2;
        len++;
        scanf("%d", &number);
    }
    che[len]=-1;
    char str_start[]="";
    translate(str_start,che,0);
    }
    return 0;
}

Test output:

Enter digits(except 0 and 1):2 3 4 -1
ADG
ADH
ADI
AEG
AEH
AEI
AFG
AFH
AFI
BDG
BDH
BDI
BEG
BEH
BEI
BFG
BFH
BFI
CDG
CDH
CDI
CEG
CEH
CEI
CFG
CFH
CFI
Enter digits(except 0 and 1):

2 Comments

you are so professional!!! thanks for your advise,too.I am glad to learn your code.
Then award him the answer. If you don't people will soon stop helping you (with your homework)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.