Need help in solving this problem

This is a discussion on Need help in solving this problem within the C Programming forums, part of the General Programming Boards category; I need to write a C program to print the label alphabets given the label name. In other words the ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    Need help in solving this problem

    I need to write a C program to print the label alphabets given the label name.
    In other words the program should do the following:

    Input Output
    ==== ====
    1 A
    2 B
    ----
    -----
    26 Z
    27 AA
    28 AB
    ----
    ----
    51 AY
    52 AZ
    53 BA
    ---
    ---
    702 ZZ
    703 AAA
    704 AAB
    etc....

    I am unable to come up with a logic... I tried different logics but they are not working. Any idea on how I can proceed.

    Thanks in Advance for your help.

    Regards
    Chari

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    We can help you with C, but we can't help you think. You need to figure out how to solve the problem. If I gave you the number 215, can you (personally, yourself) work out what the answer should be?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Ok, these kinds of problem scream out to use a while loop, so let's see how it might work with that:

    This works just like the odometer on your car, with three being the far right hand "wheel", and two being one wheel to the left of that, and etc.



    pseudo-code

    Code:
    set char's one, two, and three  to @ //one less than A
    get a number from the user, or a file.
    
    while(number > 0)
      number--;
      three++;
      if(three > 'Z') 
        then increment two and set three = 'A'
    
    Extend the three if statement, to include two, so if two > 'Z', the one
    wheel, will be incremented, and two and three reset to 'A'
    
    and loop back  
    
    
    if one equals '@' then one = 32
    if two equals '@') then two = 32
    
    print num was: %d,  which is: %c%c%c num, one, two, three.

    If you don't have an ascii table for quick reference, grab one free from Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    When you're programming, you'll refer to it frequently.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Adak,

    Thanks for your help. I was able to complete this. Your logic helped me to write this program.
    I never thought that this logic can be this much simple.
    Thanks again for your help.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    If you're a computer engineering guy, this is just a change of base from base 26 to base 10

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're quite welcome Lokachari, and BdON003 is right on the change of base.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Sorry I am not a computer science guy. I have no idea on how to do that using base26. I tried a program but is not working :
    ------------------------------------------------
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    void main()
    {

    int i, number, user_input, index, remainder, digit[10];
    char final[10], tmp[10];
    int base26 = 26;
    char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


    printf("Enter the number: (0 to quit)\n");
    scanf("%d", &user_input);

    number = user_input;

    index = 0;
    while(number != 0)
    {
    remainder = number % base26;
    digit[index] = remainder;
    printf("digit[%d] = %d\n", index, remainder);
    number = number / base26;
    index++;
    }

    printf("index = %d\n", index);
    strcpy(final, "");
    for (i = (index - 1); i >= 0; i--)
    {
    printf("i = %d\n", i);
    //if (digit[i] == 0) digit[i] = 26;
    //if (i == 0) digit[i]++;
    //if (digit[i] == 0) digit[i]++;
    sprintf(tmp, "%c", (digit[i] + 64));
    printf("tmp = %s\n", tmp);
    strcat(final, tmp);
    }
    printf("\nFinal answer = %s\n", final);


    return;
    }
    ----------------------------------------------

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I played with that idea of mod and etc., myself. I didn't like it.

    As you "pick" off the digits, one by one, you have to add that digit to the sum for that column, and see if it's greater than or == to the base number.

    When it is >= the base number, you need to carry over the base (26), to the next column to it's left, by incrementing whatever is now in that column, by one. Also, subtract 26 from the column you were summing up, of course.

    If you use the code I posted above, you'll have the program done in about 10 lines of code, with simple copy and paste (and slight editing) for the two and one column "wheels".

    All in one compact while loop that even the riders on the short ... nevermind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. JOB: C/C++ Developer with problem solving skills
    By VoltRecruiter in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 01-26-2006, 12:25 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. Sign-up Thread: Problem Solving #1
    By ygfperson in forum Contests Board
    Replies: 15
    Last Post: 01-26-2003, 02:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21