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

1. ## 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: 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 = { 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 = { 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 = { 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 = { 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 = { 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. 2. * moved to C++ programming forum * 3. Originally Posted by laserlight * moved to C++ programming forum *
Alright, I posted their my fault just because the solution could be in c. 4. 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. 5. @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. 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. 7. straight forward !
Understand.

appreciated . Popular pages Recent additions #### Tags for this Thread

array, int, mapped, multiply, table 