Results 1 to 12 of 12

Thread: "do while problem"

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: "do while problem"

    Something else just came to mind. For your program, I was assuming that you did it earlier, but did you make sure to include
    Qt Code:
    1. #include <time.h>
    To copy to clipboard, switch view to plain text mode 
    to your includes and
    Qt Code:
    1. srand(time(0));
    To copy to clipboard, switch view to plain text mode 
    to the beginning of your code execution? If you do not initialize your random number seed, then you will be generating
    the same random numbers every time your code executes.

  2. #2
    Join Date
    May 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: "do while problem"

    Qt Code:
    1. #include<stdlib.h>
    2. #include<qmessagebox.h>
    3. #include<time.h>
    4. void MainWindow::ButtonHandler()
    5. {
    6. int math[20][20];
    7. for (int row = 1; row < 20; row++) {
    8. for (int col = 1; col < 20; col++) {
    9. math[row][col]=0;}}
    10. int aux2=0;
    11. int aux3=0;
    12. srand(time(NULL));
    13. for(int i=0;i<100;i++)
    14. {
    15. bool set = false;
    16. while(!set)
    17. {
    18. aux2=int( rand()%20);
    19. aux3=int( rand()%20);
    20. if(math[aux2][aux3]==0)
    21. {
    22. math[aux2][aux3] = 1;
    23. set = true;
    24. }
    25. }
    26.  
    27. }
    28. int demonstration=0;
    29. for (int row = 1; row < 20; row++) {
    30. for (int col = 1; col < 20; col++) {
    31. if(math[row][col]==1)demonstration++;}}
    32. msg->setText(QString::number(demonstration));
    33. msg->show();
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 

    and it dosn't all time give 100!!!(i'v made this program just to show you...)
    Last edited by Hardstyle; 17th June 2010 at 21:08.

  3. #3
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: "do while problem"

    1) Arrays in C/C++ always start from '0' not '1'
    So in your loops is: for ( int row = 0; row < 20; ++row) { ... }

    2) In lines 16-25 of the posted code, the moment you find a value of 0 and make it 1 you have also set=true which will terminate the loop. That is the loop will run only 1 time cause everything in the array is set to 0 and so the 1st value it'll find will be 0!!! So instead of checking the value set, make a variable that counts to 40 and then terminate the loop.

    3) You **probably** not have exactly 40 values set to 1 because there's a possibility that aux2 and aux3 take values that points to an element already set to 1. If you have 39 elements set to 1 there's a ~10% possibility to select an element set to 1.
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

  4. #4
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: "do while problem"

    2) In lines 16-25 of the posted code, the moment you find a value of 0 and make it 1 you have also set=true which will terminate the loop. That is the loop will run only 1 time cause everything in the array is set to 0 and so the 1st value it'll find will be 0!!! So instead of checking the value set, make a variable that counts to 40 and then terminate the loop.

    That loop will run until a value is set to 1. It is SUPPOSED to terminate after setting exactly one value that is a 0 to 1. The fact that it gets called 100 times should guarantee that 100 seperate 0's are changed to 1. I think that the main reason that Hardstyle isn't always getting 100 1's is because he's ignoring a whole bunch of values by starting his arrays at 1 instead of 0. Of course you aren't going to find all 100 1's if you only check part of the array!

    General structure of a for loop that iterates through an array should be:

    for(int i = 0; i < count; i++)

    NOT

    for(int i = 1; i < count; i++)
    Last edited by SneakyPeterson; 18th June 2010 at 08:20.

  5. #5
    Join Date
    May 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: "do while problem"

    1) Arrays in C/C++ always start from '0' not '1'
    So in your loops is: for ( int row = 0; row < 20; ++row) { ... }

    2) In lines 16-25 of the posted code, the moment you find a value of 0 and make it 1 you have also set=true which will terminate the loop. That is the loop will run only 1 time cause everything in the array is set to 0 and so the 1st value it'll find will be 0!!! So instead of checking the value set, make a variable that counts to 40 and then terminate the loop.

    3) You **probably** not have exactly 40 values set to 1 because there's a possibility that aux2 and aux3 take values that points to an element already set to 1. If you have 39 elements set to 1 there's a ~10% possibility to select an element set to 1.
    2) In lines 16-25 of the posted code, the moment you find a value of 0 and make it 1 you have also set=true which will terminate the loop. That is the loop will run only 1 time cause everything in the array is set to 0 and so the 1st value it'll find will be 0!!! So instead of checking the value set, make a variable that counts to 40 and then terminate the loop.

    That loop will run until a value is set to 1. It is SUPPOSED to terminate after setting exactly one value that is a 0 to 1. The fact that it gets called 100 times should guarantee that 100 seperate 0's are changed to 1. I think that the main reason that Hardstyle isn't always getting 100 1's is because he's ignoring a whole bunch of values by starting his arrays at 1 instead of 0. Of course you aren't going to find all 100 1's if you only check part of the array!

    General structure of a for loop that iterates through an array should be:

    for(int i = 0; i < count; i++)

    NOT

    for(int i = 1; i < count; i++)

    dude i know that in c++ array like all programing languages starts with 0

    and SneakyPeterson's is right about the "2)"
    the "3)" is why i use that loop ...if the aux2 and aux3 gets a value that is allrdy 1 it will redo the random thing until it will find a value that is 0....


    can u please stop answering newbish and give answers only if you have any idea about the problem?

  6. #6
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: "do while problem"

    Quote Originally Posted by Hardstyle View Post
    dude i know that in c++ array like all programing languages starts with 0
    So then why are you starting your arrays at 1?

    Quote Originally Posted by Hardstyle View Post
    and SneakyPeterson's is right about the "2)"
    the "3)" is why i use that loop ...if the aux2 and aux3 gets a value that is allrdy 1 it will redo the random thing until it will find a value that is 0....
    can u please stop answering newbish and give answers only if you have any idea about the problem?
    While Archimedes was incorrect about the function of the loop, he did correctly diagnose your original problem of starting your array at the wrong point and thus, missing several int's when counting the number of set 1's. Please note that because you start at 1 in your first array as well, you never completely initialize your array properly. Your last comments seem a bit hostile to people who are trying to help you solve your problem and in my opinion, have done so at this point.

  7. The following user says thank you to SneakyPeterson for this useful post:

    Hardstyle (18th June 2010)

Similar Threads

  1. Replies: 1
    Last Post: 7th April 2010, 21:46
  2. Replies: 3
    Last Post: 15th February 2010, 17:27
  3. Replies: 3
    Last Post: 8th July 2008, 19:37
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.