Thread: Pre-Mapping issue (could be in C or C++ )

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    34

    Pre-Mapping issue (could be in C or C++ )

    I'm trying to implement a function in C++(or c) that gets two inputs, one input is an array of integers value (binary values just zero or one) , the second input is an integer value one of those values: 1 , 2, 4, 8.
    the function returns the correct pre-mapped pattern according to this table:
    Pre-Mapping issue (could be in C or C++ )-news-jpg

    I mean by pre-mapped pattern, a pattern that it was before mapping it according to the table !.
    to elaborate more, I'm giving examples: (Remap is the name of the function, we always read the given input array from left to right ..)
    Code:
    Ramp(given array, DSSS value) - the function format.
    
    first example: Remap({0,1} , 1 ) => returns an integer array according to the table {0, 1};
     (we see that 0 is mapped to value 0 , 1 is mapped to value 0 if the multiply=1)
    
    second example: Remap({1 ,1 , 0 , 0} , 2 ) => returns an integer array according to the table {1 ,0}; (we see that 11 is mapped to value 1 , 00 is mapped to value 0 if the multiply=2, so the output is {1,0})
    
    third example: Remap({1 ,1 , 0 , 0 , 0 , 0 , 1 , 1} , 4 ) => returns an integer array according to the table {0 ,1}; (we see that 1100 is mapped to value 0 , 0011 is mapped to value 1 if the multiply=4 so output is {0,1})
    
    third example: Remap({1 ,1 , 0 , 0 , 1 , 1, 0 , 0} , 8) => returns an integer array according to the table {0}; (we see that 11001100 is mapped to value 0 if the multiply=8 so output is {0} )
    
    the given array is always in its rules according to the table, this means there's no case like the patterns in the given array aren't the same as the patterns on the table, for instance this array {001} with multiply 2 isn't possible .. !
    
    In brief we are returning the pre-mapped pattern according to the attached table of mapping. (the attached table is given by default in the question itself) 
    


    My implementation code C++ is:

    Code:
    
    
    Code:
    #include <iostream>
    #include <vector>
    std::vector<int> Remap(int* givenArray, int DSSS)
    {
        std::vector<int> ret;                                                      // result's utput
        switch (multiply) {
            case 1:    {
                int l[2] = { 0,1 };                                               // for first opition in the table
                for (int i = 0; i < 2; i++)
                    ret.push_back(l[i]);
            }
            break;
            case 2:    {
                int l[4] = { 0 ,0 ,1 ,1 };                                           //for second opition in the table
                for (int i = 0; i < 4; i++)
                    ret.push_back(l[i]);
            }
            break;
            case 4:    {
                int l[8] = { 0 ,0,1 ,1 ,1 ,1 ,0 ,0 };                                  // for third opition in the table
                for (int i = 0; i < 8; i++)
                    ret.push_back(l[i]);
            }
                break;
            case 8: {
                int l[16] = { 0 ,0,1,1,1,1,0,0 ,1,1,0,0,0,0,1,1 };                // for fourth opition in the table
                for (int i = 0; i < 16; i++)
                    ret.push_back(l[i]);
            }
            break;
        }
        return ret; // returning the correspond pre-mapped array according to given DSSS value to the function main 
    }
    
    
    int main()
    {
        int MappedPattern[4] = { 0,0,1,1 };
        std::vector<int> PremappedPattern = Remap(MappedPattern, 2); // it should return {0,1} because 00 -> 0 , 
         //11->1 according to the table over multiply =2
            return 0;
    }
    ````
    What's wrong with my code? any help please in my implementation for the problem? honestly Im stuck on this, so it would be appreciated if there's any assistance to solve my problem or any other algorithms suggestions for my problem!
    I was thinking to use hashtable but I don't know if it's efficient or not.
    THANKS ALOT.
    Last edited by JohnnyOmari; 12-31-2020 at 03:16 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,304
    * moved to C++ programming forum *
    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

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    34
    Quote Originally Posted by laserlight View Post
    * moved to C++ programming forum *
    Alright, I posted their my fault just because the solution could be in c.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,304
    If you're going to assume that the input is correct, then this seems rather trivial. For example:
    Code:
    #include <iostream>
    #include <vector>
    
    std::vector<int> unmap(const std::vector<int>& input, unsigned int dsss)
    {
        std::vector<int> result;
        switch (dsss)
        {
        case 1:
            return input;
        case 2:
            for (std::size_t i = 0; i < input.size(); i += 2)
            {
                result.push_back((input[i] == 0) ? 0 : 1);
            }
            break;
        case 4:
            for (std::size_t i = 0; i < input.size(); i += 4)
            {
                result.push_back((input[i] == 0) ? 1 : 0);
            }
            break;
        case 8:
            for (std::size_t i = 0; i < input.size(); i += 8)
            {
                result.push_back((input[i] == 0) ? 1 : 0);
            }
            break;
        }
        return result;
    }
    
    int main()
    {
        auto result = unmap(std::vector<int>{0, 0, 1, 1, 1, 1, 0, 0}, 2);
        for (auto n : result)
        {
            std::cout << n;
        }
        std::cout << std::endl;
    }
    The idea is to observe that when the selector (DSSS, but what does DSSS mean?) is say 4, you're analysing groups of 4 consecutive integers. Then, since there are only two possibilities, and the very first integer in each group is different for the possibilities, therefore you only need to check the very first integer in each group in order to classify it.
    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

  5. #5
    Registered User
    Join Date
    Nov 2020
    Posts
    34
    @laser
    appreciate for your help, I need to understand the algorithm, what I understand from your last comment the the treck in the question is in DSSS.
    may you explain the algorithm by more elaborate example? thanks alot.
    Understanding the concept/the algorithm is more significant for me than just writing code(at the end it's syntax)


    thanks

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,304
    Quote Originally Posted by JohnnyOmari
    may you explain the algorithm by more elaborate example?
    Okay, let's try this:
    input = 1 1 0 0 0 0 1 1
    selector = 4

    So, first we examine 1 1 0 0. We see that the very first integer is 1. This means that the result for this group is 0. Then, we examine 0 0 1 1. We see that the very first integer is 0. This means that the result for this group is 1. This is because the very first integer uniquely determines the result.

    If you still don't understand, let's try this: suppose that YES maps to true and NO maps to false. Now, I show you a piece of paper with either YES or NO written on it, but I hide the letters of the word such that you can only see Y and refuse to let you see the rest. Placing a loaded gun to your head, I demand that you tell me the mapping, and you only have one chance to get it right or I'll pull the trigger. Is the mapping of this word to true or false? Obviously it is to true, because between YES and NO, the only word that begins with Y is YES, which maps to true.
    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
    Nov 2020
    Posts
    34
    straight forward !
    Understand.

    appreciated .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mapping in C++
    By jack_carver in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2009, 10:00 AM
  2. help using strings and mapping
    By trprince in forum C Programming
    Replies: 29
    Last Post: 12-01-2007, 04:01 PM
  3. username to uid mapping
    By rotis23 in forum C Programming
    Replies: 1
    Last Post: 10-30-2002, 04:17 AM
  4. Mapping
    By Scorpion-ice in forum Game Programming
    Replies: 0
    Last Post: 07-02-2002, 01:32 PM

Tags for this Thread