problem with infinite loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kalar
    New Member
    • Aug 2007
    • 82

    problem with infinite loop

    Hello, i have a problem with an infinite loop.
    I have got 4 arrays , each of them, have 5 integers
    the elements are from random funtcion, I don't want to have same elements in the arrays so i make an if in line 18.The problem:some times the code prints me the 5 elements of d[i] and some times not(it starts to run but it doesn't do no anything- i think tha is an infinite loop)
    Code:
        int a[5];
        int b[5];
        int c[5];
        int d[5];
    
       i = 0;
       k=0;
       z=0;
        while(i<6)
        {
                
               t = rand()%100 +1 ;
               if (t!=100 && t!=1)
               {
                
                 while(z<6)
                 {
                   if(a[z]!=t && b[z]!=t)
                   {
                     
                     k=1;
                   }
                   else k=0;
                   z++;
                 }
                 if(k==1)
                 {
                   if(c[i]>t)
                   {
                     d[i]=t;
                     i++;
                   }
                 }
                 }
               
               
        } 
        printf("answers\n");
        for(i = 0 ; i < 5 ; i++)
        printf("%d\n" , d[i]);
  • aviraldg
    New Member
    • Sep 2007
    • 21

    #2
    Originally posted by kalar
    Hello, i have a problem with an infinite loop.
    I have got 4 arrays , each of them, have 5 integers
    the elements are from random funtcion, I don't want to have same elements in the arrays so i make an if in line 18.The problem:some times the code prints me the 5 elements of d[i] and some times not(it starts to run but it doesn't do no anything- i think tha is an infinite loop)
    Code:
        int a[5];
        int b[5];
        int c[5];
        int d[5];
    
       i = 0;
       k=0;
       z=0;
        while(i<6)
        {
                
               t = rand()%100 +1 ;
               if (t!=100 && t!=1)
               {
                
                 while(z<6)
                 {
                   if(a[z]!=t && b[z]!=t)
                   {
                     
                     k=1;
                   }
                   else k=0;
                   z++;
                 }
                 if(k==1)
                 {
                   if(c[i]>t)
                   {
                     d[i]=t;
                     i++;
                   }
                 }
                 }
               
               
        } 
        printf("answers\n");
        for(i = 0 ; i < 5 ; i++)
        printf("%d\n" , d[i]);
    1.The loop on line numbers 16-25 is like writing:
    Code:
    if(a[5]!=t && b[5]!=t)
    {
            k=1;
    }
    else k=0;
    2.Please , never , never use letters for variables , for sanity's sake!
    3.The if construct on line 26 is also messed up, also writable as:
    Code:
    if(c[i]>t && k==1)
    {
    d[i]=t;
    i++;
    }
    which is unreachable , since c[0],c[1]..c[5] are all empty(0).

    Comment

    • kalar
      New Member
      • Aug 2007
      • 82

      #3
      Originally posted by aviraldg
      1.The loop on line numbers 16-25 is like writing:
      Code:
      if(a[5]!=t && b[5]!=t)
      {
              k=1;
      }
      else k=0;
      why this? I have z variable and before the end of loop i make it z++ , first z=0, then z=1 ....

      3.The if construct on line 26 is also messed up, also writable as:
      which is unreachable , since c[0],c[1]..c[5] are all empty(0).
      i forgot to say that arrays a,b,c have already values(if you mean this) and i want to fill the d array

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        If an array has five elements, the five index values are 0, 1, 2, 3 and 4. so 5 is far out.

        kind regards,

        Jos

        Comment

        • kalar
          New Member
          • Aug 2007
          • 82

          #5
          i fix this
          Code:
           while(z<5)   and while (i<5)
          , but still have the same problem.Some times it prints me the array and some times not

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by kalar
            i fix this
            Code:
             while(z<5)   and while (i<5)
            , but still have the same problem.Some times it prints me the array and some times not
            That is not syntactically valid C (nor C++) code.

            kind regards,

            Jos

            Comment

            • kalar
              New Member
              • Aug 2007
              • 82

              #7
              Originally posted by JosAH
              That is not syntactically valid C (nor C++) code.
              Of course i didn't write like something like this, i want to tell that i fix both of the loops that i have on my code


              Something else. This is not the full code.
              I want to have 20 random numers (they are different from each other).In a[i] i have 5 numbers and in b[i] i have 5 numbers but they are smaller than a[i](i mean a[1] should be > b[1] , a[2]>b[2], a[3]>b[3]....) The same thing for c and d arrays.
              Am i in the right way or give me a better idea?
              thanks

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by kalar
                why this? I have z variable and before the end of loop i make it z++ , first z=0, then z=1 ....
                The reason is that with each iteration of the z loop the value of k is overwritten so only the final loop iteration (with z=4 now) actually effects the value of k that is later used in the if statement.

                It is not at all clear what you are trying to achieve with this code, however I suspect your loop is a result of not necessarily incrementing i. This only happens if

                t != 100
                and
                t != 1
                and
                a[4] != t
                and
                b[4] !=t
                and
                c[i] > t

                We do not even know what the value of c[i] is from the code so ignoring all the other constraints if c[i] <= 1 for 0 <= i < 5 this will result in an infinite loop because i will stop incrementing.

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Originally posted by kalar
                  Am i in the right way or give me a better idea?
                  How about you just get 20 random numbers not equal to each other in a single array and then sort then by value using qsort.

                  Then d can be the first 5, c can be the second 5, b can be the third 5 can a can be the last 5.

                  Comment

                  • kalar
                    New Member
                    • Aug 2007
                    • 82

                    #10
                    Originally posted by Banfa
                    How about you just get 20 random numbers not equal to each other in a single array and then sort then by value using qsort.

                    Then d can be the first 5, c can be the second 5, b can be the third 5 can a can be the last 5.
                    Thanx Banfa. I will try to fix my code.
                    Your thought with qsort, i think that doesn't fit in my situation because i should have too many if. I want in array a numbers 10-100, in array b 1-90 and the elements of b should be smaller than a (b[1]<a[1],b[2]<a[2]...). I want the same thing for arrays c and d, So as i am thinking it now , i can't found a way to do it with one array and quicksort

                    Comment

                    • kalar
                      New Member
                      • Aug 2007
                      • 82

                      #11
                      i can't edit my post
                      I fix my code and it seems that WORKS fine. I run it over 40 times and it works.
                      But how can i check if it is realy correct? I want to put this code into a game so i want to be sure that i will not have an infinite loop again? Can i check it with another way than running it 40,60..... times?
                      Thanks

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #12
                        Originally posted by kalar
                        i can't edit my post
                        I fix my code and it seems that WORKS fine. I run it over 40 times and it works.
                        But how can i check if it is realy correct? I want to put this code into a game so i want to be sure that i will not have an infinite loop again? Can i check it with another way than running it 40,60..... times?
                        Thanks
                        The problem is that you have run the algorithm 40 times checking 40 data combinations but there are around 20 million possible data combinations (by my estimate).

                        Another problem for this code is because it uses the rand function you have no real control over the input to the algorithm, you can't check with specific boundary values or attempt a check with all possible values.

                        In fact because it uses the rand function in theory you could get the a long series of 1s, how do you differentiate between an finite loop and a series of 1s long enough to tie you computer up for an hour looking for the set of numbers you want? (Admittedly an occurrence so unlikely you can probably afford to ignore the chance of it ever happening).



                        You could check you algorithm by inspection (or better yet get someone else to) looking for values or sequences that would break it and checking the logic of each step.

                        If you can divorce the algorithm from the data capture (i.e. not have the rand embedded in the heart of the algorithm then you can feed the algorithm known sequences of numbers in an attempt to break it.

                        In truth it wont be easy, testing is normally several orders of magnitude more complex than the original coding of the algorithm.

                        Comment

                        Working...