Lookup table help

This is a discussion on Lookup table help within the C++ Programming forums, part of the General Programming Boards category; Can you read this kind of way a lookup table or it has to be a multidimensional array mixing the ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    753

    Lookup table help

    Can you read this kind of way a lookup table or it has to be a multidimensional array mixing the corresponding values one after the other?

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    static const char lookup[17] = "0123456789:;<=";
    
    static const char *Hex[] ={
    "0x60","0x61","0x62","0x63","0x64",
    "0x65","0x66","0x67","0x68","0x69",
    "0x71","0x72","0x73","0x74","0x75"
    };
    
    int main()
    {
        cout << Hex[lookup[13]] << endl;
    
           return 0;
    }
    Last edited by Ducky; 10-27-2011 at 11:03 AM.
    Compiler MSVC++ 2010 with Code::Blocks.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    That's C++ and should have been posted in the C++ forum.

    Did you try this at all? Why don't you add a return 0; and a closing brace to your code, compile it and see what happens. Then, if you still have questions, come back and ask.

    EDIT: I'm actually not sure what exactly you're asking or what you're trying to do here. Perhaps you're asking about parallel arrays? See if this helps: http://en.wikipedia.org/wiki/Parallel_array.
    Last edited by anduril462; 10-27-2011 at 10:35 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    Quote Originally Posted by anduril462
    That's C++ and should have been posted in the C++ forum.
    Yeah, and hence moved.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    753
    Ok, im sorry i thought it was clear enough.

    So what i would like to do is returning the corresponding value in the Hex array when I call the lookup array.
    Its crashing for the moment.

    Thank you.
    Compiler MSVC++ 2010 with Code::Blocks.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    So if you have lookup[13], then just do Hex[13].

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    Quote Originally Posted by Ducky
    So what i would like to do is returning the corresponding value in the Hex array when I call the lookup array.
    Use a std::map or std::unordered_map instead. The problem now is that to find the corresponding value in the Hex array, you need to search for it in the lookup array first, and then based on the index found, obtain the element in the Hex array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    753
    Yes I know about the way of searching but they told me that if you use a lookup table you dont need to compare the values "search" one by one, you can just read the value at the index of the array.
    Well thats the point of lookup tables that you dont have to search for it.

    Im sure you know what i mean, its something simple but i cant seem to figure it out. Maybe i should use a multidimensional array.

    And id like to do it with arrays thats why I posted it in the C forum.
    Compiler MSVC++ 2010 with Code::Blocks.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    Quote Originally Posted by Ducky
    Well thats the point of lookup tables that you dont have to search for it.
    Hence your lookup table is not really a lookup table. Now, if you had a way to compute the index given the character, then indeed you would have a lookup table. Since you don't, the next best option is to use a hash table to get something similiar, hence my suggestion of std::unordered_map.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    753
    Ok so you definitely need a way to compute the index.
    Thanks, then ill try it with std::unordered_map.
    Compiler MSVC++ 2010 with Code::Blocks.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    I think your problem is that you think you need two lookup tables for this and hence you've got 'lookup' AND 'hex'. All you actually need is just the 'hex' table, but you need to calculate the right index. To do that, you can just subtract the value that you want the first table index to correspond to before doing the lookup.
    In this case you want the first mapping to be from '0' to "0x60", so you subtract '0' from what you are looking up. To lookup '4' you then do:
    Code:
    cout << Hex['4' - '0'] << endl;
    Note that you cannot have gaps in your lookup table, you must specify a mapping for every value between other values.

    Next, are you sure that you're mapping to the desired strings? It looks like it starts with the octal representation of a char, but using a hexadecimal prefix, and going wrong after "0x67".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    753
    Yes iMalc, somebody showed me how to do it in the meantime, its like you say it, I only need one lookup table and it got to go from 0 to 255 without gaps.

    So finally its possible without calculating the index.
    Maybe I didnt explain well enough what I wanted to Laserlight.

    Thanks for the help everyone!
    Compiler MSVC++ 2010 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lookup table mappings
    By robi in forum C Programming
    Replies: 5
    Last Post: 10-19-2011, 12:25 PM
  2. quick log lookup table
    By R.Stiltskin in forum C++ Programming
    Replies: 19
    Last Post: 12-04-2008, 11:39 AM
  3. Making a lookup table?
    By username101 in forum C Programming
    Replies: 7
    Last Post: 05-09-2008, 09:10 AM
  4. using a shared lookup table
    By zxcv in forum C Programming
    Replies: 1
    Last Post: 02-20-2008, 11:10 PM
  5. Table Lookup Processing
    By muffin in forum C Programming
    Replies: 1
    Last Post: 08-29-2001, 12:07 PM

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