# Logic problem

• 07-25-2005
earth_angel
Logic problem
hey,

I have a 'sorting' problem and I can't wrap my head around it to properly set up the logic.

The program I'm working on takes one file (binary format) and converts it to another. The input file has data in the following format: it's a 2d array. At the beginning of every row there is a flag that indicates which variables are present in this row and then the variable values.
ex: 3 variables: A, B AND C
111 1 2 3
100 1
001 3
011 2 3

I have code that allows the user to select which variables he'd like to view. So if the user says I want to see variable B only, I make a mask = 010, and then when I read in the flag I check if the bit is on in the mask as well, then I copy the variable somewhere and do other stuff to it. I also keep track of which variables the user selected in an array, so in this case it would have one element: B

Now, the problems arise when the user selects the same variable to be viewed twice. So the selected items array has two items, say: B B.

The mask is still made as 010, because that's the only variable to be copied. But I need to some how allow it to check again, if the variable is selected twice.

I'm not looking for code, just sujestions on how to straten out the logic.

Oh, I hope this makes sense.

Thanks for the input in advance.
• 07-25-2005
quzah
Why don't you just check the input line for dupes?

Quzah.
• 07-25-2005
valenok
I would solve this problem this way:
User filter I would represent as an array of variables to see:
enum {VAR_NONE, VAR_A, VAR_B, VAR_C};

So, the "B B" filter would look like:
int filter[] = {VAR_B, VAR_B, VAR_NONE};

And the pseudo-code:
Code:

```  int values[3];   for (every_input_line) {     /* If the var is not present, fill -1 there */     fill_in_the_values_array();     for (i = 0; filter[i] != VAR_NONE; i++)       if (values[filter[i]] == -1)           printf("N/A")       else           printf("%d", values[filter[i]]);   }```
This way, user may specify any sequence of variables, repeating them many times.
• 07-25-2005
earth_angel
> Why don't you just check the input line for dupes?

If can check it for copies, but that won't solve the problem if I want to display it twice.

what I came up with is to read in the input file flag, check which position is turned on. if say position 2 is on, then connect it to a variable name ("B"), and go through the user selected array to see if "B" appears. If it does, copy the value into that position in the values matrix. The only problem will be if a user selected variable doesn't appear in that row, there won't be anything entered into the matrix at that possition.

Quote:

int filter[] = {VAR_B, VAR_B, VAR_NONE};
Is this supposed to be an array of ints containing the position of VAR_A, etc in the flag? I don't quit get that.
• 07-25-2005
quzah
Since your example appeared to be bit toggling, I gave you an answer that works for that. If it's there an even number of times, it's off. If it's there an odd number, it's on.

Your example was lacking at best. You never provide an example of what it looks like when one is displayed twice. As a matter of fact, your example prevents that from ever happening. Here's why:
Quote:

ex: 3 variables: A, B AND C
111 1 2 3
100 1
001 3
011 2 3
It is impossible for any number to be represented twice. You can't have the same flag set twice, since they're all the same length, and are only ever on or off. You can't provide input for B to be there twice in 3 characters worth of input, just using 0 and 1.

Quzah.
• 07-25-2005
earth_angel
The example given, and the bit you quoted, is the input file. That's the original file. Now the user selects which variables from that file she wants to view. Then I create another matrix which contains the values for the variable that the user selected only. there is more to it and why I'm trying to do this. I've simplified it a bit just to get the part that I don't understand.

So based on the above input file, if the user selects to use variables B and B. I would make a 2D array to contain the values (from the input file) for those variables. Like a table:
PHP Code:

``` var name:             B         B Values:            2         2                       2         2                       2         2  ```
and so on.
• 07-26-2005
valenok
Quote:

Originally Posted by earth_angel
>
Is this supposed to be an array of ints containing the position of VAR_A, etc in the flag? I don't quit get that.

Yeah. Basically, what you do, is assigning to every variable a unique
index number.
Then you represent a user filter as an array of indexes.
Then, for each row, you prepare a values array. values[var_index]
contains a value for certain variable var.

Having two arrays, values[] and filters[], you can do what you want.