Thread: Lookup table help

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

    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.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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
    28,412
    Quote Originally Posted by anduril462
    That's C++ and should have been posted in the C++ forum.
    Yeah, and hence moved.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    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.
    Using Windows 10 with Code Blocks and MingW.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    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.
    Using Windows 10 with Code Blocks and MingW.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Ok so you definitely need a way to compute the index.
    Thanks, then ill try it with std::unordered_map.
    Using Windows 10 with Code Blocks and MingW.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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
    930
    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!
    Using Windows 10 with Code Blocks and MingW.

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, 12:39 PM
  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-21-2008, 12:10 AM
  5. Table Lookup Processing
    By muffin in forum C Programming
    Replies: 1
    Last Post: 08-29-2001, 12:07 PM