Thread: "Easy" Array question

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    9

    Arrow "Easy" Array question

    How can I write an array that lists the elements as alpha letters instead of numbers?

    Like this:

    Code:
    Element               Value
    a                          45
    b                          5
    c                           655
    NOT like this:

    Code:
    Element               Value
    0                          45
    1                          5
    2                          655
    Last edited by bond_006; 05-02-2010 at 03:48 PM.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Do you mean store chars instead of ints? If so, then you just denote the type when creating the array.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    oops, just posted before you edited. I don't think you can do what you want (I may be wrong as i am new to programming), but the 0,1,2 etc is the element within the array.

    Maybe somebody else could clarify.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    No, I want the array to store integer values, I just want to display the Element number of the array as an alpha letter (character) instead of an integer.

    Kind of like this:
    Code:
    Element          Value
    a                  45
    b                  22
    c                  7554

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    int nums[4] = {1, 2, 3, 4};
    
    for ( int i = 0; i < sizeof( nums ) / sizeof( int ); ++i )
    {
       std::cout << nums[i] << std::endl;
    }
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    A simple ascii table look up will suffice. The character 'a' starts at 97 on the table. Just add that number to each of the element indexes and voila. Also a typecast will be in order to display it as a character.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Do you want to store the real ASCII values of the characters, or those numbers that you made up?
    If you want the real ASCII values, just have an array of chars and if you need the ASCII number, just cast each char to an int when you display it.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  3. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM