Results 1 to 4 of 4

Thread: Logic problem

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Logic problem

    I am stuck on a logic problem.

    I have the following struct:
    Qt Code:
    1. struct MeasurementData
    2. {
    3. bool complete = false;
    4. std::bitset<3> mode;
    5. std::bitset<12> frame_counter;
    6. std::bitset<16> expo_time;
    7. std::bitset<16> expo_time_max;
    8. std::bitset<24> ntc_val0_ref_2_4V;
    9. std::bitset<24> ntc_val0_ref_2_1V;
    10. std::bitset<24> ntc_val0_temp_2_4V;
    11. std::bitset<24> ntc_val0_temp_2_1V;
    12. };
    To copy to clipboard, switch view to plain text mode 

    Where the field frame_counter is a progressive number incremented after each measurement (starting from value 0).
    To fill in the fields of the structure I analyze an input file where each row contains the values that I have to insert in the structure.
    So I create a vector of structures and I create a new struct each time I parse a new row

    Qt Code:
    1. std::vector<MeasurementData> frames;
    2. std::string line;
    3. while (std::getline(input, line))
    4. {
    5. processLine(line, frames); // <- Here I create a new struct instance and add it to the vector
    6. }
    7. input.close();
    To copy to clipboard, switch view to plain text mode 

    My need is to find the missing frame_counter values when processing the corresponding file line

    For example:
    In my file I have 48 rows, this implies the creation of 48 struct instances and the frame_counter should start at 0 and end at 47 but in my case I have 3 missing measurement

    frame_counter = 0 is missing
    frame_counter = 9 is missing
    frame_counter = 12 is missing

    I determine it by postprocessing the vector of structs. Is not what I want, I would determine it at the "file line processing" time but I can not find a good logic that solves my problem.

    Below is how I do it now:
    Qt Code:
    1. std::vector<MeasurementData> frames;
    2. std::vector<int> counters;
    3.  
    4. for (const auto& frame : frames) // frames is my structs vector
    5. {
    6. int counter = (int)(frame.frame_counter.to_ulong());
    7. counters.push_back(counter);
    8. }
    9.  
    10. // And search for missing frames (assuming vector is ordered)
    11. for (int i = 0; i < counters.size(); i++)
    12. {
    13. if (!std::binary_search(counters.begin(), counters.end(), i))
    14. {
    15. std::cout << "Frame " << i << " is missing" << std::endl;
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 
    Franco Amato

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Logic problem

    In my file I have 48 rows
    Is this the same kind of ASCII file you were reading in the last post you made about this kind of thing?

    If it is, why are you using these bitset structures to hold the values after you have parsed the lines? I think you are seriously confused about the range of values that the numbers in your file can have and how to represent them in your code.

    If you are reading an ASCII number from the file where that number can have a range of 0 - 4096, there is no need to represent it in your code as bitset<12>. A normal C++ unsigned 16-bit integer is adequate. A value with a 24-bit range (0 - 16777215) can be held in a 32-bit unsigned integer.

    You have your choice of everything from unsigned char to unsigned long long, and you can do any sort of standard arithmetic processing with them, something you can't do with bitset.

    My need is to find the missing frame_counter values when processing the corresponding file line
    So for your original question:

    1) Create an "expectedFrameCounter" variable outside the loop and initialize it to zero.
    2) Have your processLine() method return the frame_counter value it reads from the line
    3) Compare it to "expectedFrameCounter".
    4) If they match, no problem
    5) If they don't match, then add "expectedFrameCounter" to a list of "missing frames"
    (If more than one frame can be skipped, then add all of the values from "expectedFrameCounter" to the value returned by processLine() - 1).
    6) Set "expectedFrameCounter" to the value returned by processLine + 1 and continue.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Logic problem

    The algorithm you proposed does not work (I had thought of something similar at the beginning taking the loop index as expected frame counter)

    Here the explanation:

    Step 0) expected frame_counter: 0, real frame_counter 1 -> missing frame 0? YES, Your algorithm says YES <= OK
    Step 1) expected frame_counter: 1, real frame_counter 2 -> missing frame 1? NO, Your algorithm says YES
    Step 2) expected frame_counter: 2, real frame_counter 3 -> missing frame 2? NO, Your algorithm says YES
    Step 3) expected frame_counter: 3, real frame_counter 4 -> missing frame 3? NO, Your algorithm says YES
    Step 4) expected frame_counter: 4, real frame_counter 5 -> missing frame 4? NO, Your algorithm says YES
    Step 5) expected frame_counter: 5, real frame_counter 6 -> missing frame 5? NO, Your algorithm says YES
    Step 6) expected frame_counter: 6, real frame_counter 7 -> missing frame 6? NO, Your algorithm says YES
    Step 7) expected frame_counter: 7, real frame_counter 8 -> missing frame 7? NO, Your algorithm says YES
    Step 8) expected frame_counter: 8, real frame_counter 10 -> missing frame 8? NO, Your algorithm says YES
    Step 9) expected frame_counter: 9, real frame_counter 11 -> missing frame 9? YES, Your algorithm says YES <= ok
    ...
    etc
    ...

    As you can see, by analizing just 10 lines of the file I should get only 2 missing frames but I get all the lines
    Franco Amato

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Logic problem

    6) Set "expectedFrameCounter" to the value returned by processLine + 1 and continue.
    Are you doing this? After Step 0), if frame 0 is missing, then the expected frame counter for the next frame should be 2, not 1. At Step 0) the actual frame counter is 1, so the next expected frame would be 2 (real frame counter + 1), not 1 as you say in Step 1).

    You have also made an error starting with Step 1) about what value the missing frame should have. In Step 0), frame 0 is the missing one, frame 1 was found instead. In Step 1), you should be looking for frame 2, since that should be the next one after the frame that was found in the previous step. Instead, you ask "missing frame 1?" which is wrong. It should be "missing frame 2?" and the answer is NO.

    If you had implemented the algorithm correctly, the results would be:

    Step 0) expected frame_counter: 0, real frame_counter 1 -> missing frame 0? YES, Your algorithm says YES <= OK
    Step 1) expected frame_counter: 2, real frame_counter 2 -> missing frame 2? NO, Your algorithm says NO
    Step 2) expected frame_counter: 3, real frame_counter 3 -> missing frame 3? NO, Your algorithm says NO
    Step 3) expected frame_counter: 4, real frame_counter 4 -> missing frame 4? NO, Your algorithm says NO
    Step 4) expected frame_counter: 5, real frame_counter 5 -> missing frame 5? NO, Your algorithm says NO
    Step 5) expected frame_counter: 6, real frame_counter 6 -> missing frame 6? NO, Your algorithm says NO
    Step 6) expected frame_counter: 7, real frame_counter 7 -> missing frame 7? NO, Your algorithm says NO
    Step 7) expected frame_counter: 8, real frame_counter 8 -> missing frame 8? NO, Your algorithm says NO
    Step 8) expected frame_counter: 9, real frame_counter 10 -> missing frame 9? YES, Your algorithm says YES <= ok
    Step 9) expected frame_counter: 11, real frame_counter 11 -> missing frame 11? NO, Your algorithm says NO
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    franco.amato (29th September 2022)

Similar Threads

  1. Three Valued Logic
    By Milica in forum Qt Programming
    Replies: 1
    Last Post: 4th December 2014, 22:44
  2. [QThread][QQueue] - logic problem !
    By Abdelhadi in forum Newbie
    Replies: 4
    Last Post: 20th April 2014, 15:01
  3. qt quick using c++ for the logic
    By fearu in forum Qt Quick
    Replies: 9
    Last Post: 17th January 2013, 12:34
  4. need help in getting a logic to work
    By eva2002 in forum General Programming
    Replies: 3
    Last Post: 29th January 2010, 09:30
  5. QSQLITE logic error
    By cyberboy in forum Qt Programming
    Replies: 11
    Last Post: 10th February 2008, 20:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.