Logic problem

This is a discussion on Logic problem within the C Programming forums, part of the General Programming Boards category; hey, I have a 'sorting' problem and I can't wrap my head around it to properly set up the logic. ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    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.
    Everything is relative...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just check the input line for dupes?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    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.

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    > 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.

    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.
    Everything is relative...

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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:
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    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.
    Last edited by earth_angel; 07-25-2005 at 11:30 AM.
    Everything is relative...

  7. #7
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logic
    By LordBronz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2006, 05:41 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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