Why the while() doesn't work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scorpio

    Why the while() doesn't work?

    hi,
    I'm trying to run a bubble-sort. But the while() in main doesn't work.
    Can somebody tell me why? thanks a lot~~~
    here is the code

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define SIZE 20

    int check_repeat(in t a, int randnum, int randarray[])
    {
    int z=0,mark=0;
    while(z<a)
    {
    if (randnum == randarray[z])
    {
    mark++;
    break;
    }
    a++;
    }

    if (mark == 0)
    return 1;
    else
    return 0;
    }

    void check_double(in t sorted[])
    {
    int a=0,b=0;

    while(a <= SIZE-1)
    {
    b= a + 1;
    if (sorted[a] > sorted[b])
    printf("**error of (sorted[%d] > sorted[%d])\n",a,b);
    a++;
    }
    }

    int main()
    {
    printf("start!! \n");
    int bu[SIZE]={0};
    int i=0,temp=0,j=0, checkpoint=0;

    srand((unsigned )time(NULL));
    printf("i=%d\n" ,i);

    //HERE IS THE PROBLEM!!!
    while(i<=(SIZE-1))
    {
    printf("in while--i=%d ",i);
    temp=rand();
    checkpoint = check_repeat(i, temp,bu);
    if (checkpoint == 1)
    bu[i] = temp;
    else
    continue;
    i++;
    }//end while

    printf("\norigi nal-->\n");
    for(i=0;i<=SIZE-1;i++)
    printf(" %d ",bu[i]);
    printf("\n");

    for (i=0;i<=SIZE-2;i++)
    for (j=i+1;j<=SIZE-1;j++)
    {
    if(bu[i]>bu[j])
    {
    temp=bu[i];
    bu[i]=bu[j];
    bu[j]=temp;
    }/* end if*/
    } /* end i-for*/

    check_double(bu );

    printf("after-->\n");
    for (i=0;i<=SIZE-1;i++)
    printf(" %d ",bu[i]);
    printf("\n");

    return 0;

    }/* end main*/

  • Ian Collins

    #2
    Re: Why the while() doesn't work?

    Scorpio wrote:>[color=blue]
    > //HERE IS THE PROBLEM!!!
    > while(i<=(SIZE-1))
    > {
    > printf("in while--i=%d ",i);
    > temp=rand();
    > checkpoint = check_repeat(i, temp,bu);
    > if (checkpoint == 1)
    > bu[i] = temp;
    > else
    > continue;
    > i++;
    > }//end while[/color]
    Look again at the else (it's generally considered better and is safer to
    use braces on the branches of an if, even with only one statement).
    What happens to i in this case? Is it incremented? If you're not sure,
    check up on continue.

    --
    Ian Collins.

    Comment

    • code break

      #3
      Re: Why the while() doesn't work?

      //HERE IS THE PROBLEM!!!
      while(i<=(SIZE-1))
      {
      printf("in while--i=%d ",i);
      temp=rand();
      checkpoint = check_repeat(i, temp,bu);
      if (checkpoint == 1) replace with
      if(checkpoint== 1)
      bu[i] = temp;
      bu[i++]=temp;
      else else
      continue; {
      i++;
      i++;

      continue;

      }
      }//end while

      Comment

      • Ian Collins

        #4
        Re: Why the while() doesn't work?

        code break wrote:[color=blue]
        > //HERE IS THE PROBLEM!!!
        > while(i<=(SIZE-1))
        > {
        > printf("in while--i=%d ",i);
        > temp=rand();
        > checkpoint = check_repeat(i, temp,bu);
        > if (checkpoint == 1) replace with
        > if(checkpoint== 1)
        > bu[i] = temp;
        > bu[i++]=temp;
        > else else
        > continue; {
        > i++;
        > i++;
        >
        > continue;
        >
        > }
        > }//end while
        >[/color]
        Well I can't even read this, please don't post tabs and please quote
        some context when replying.

        --
        Ian Collins.

        Comment

        • santosh

          #5
          Re: Why the while() doesn't work?

          Scorpio wrote:[color=blue]
          > hi,
          > I'm trying to run a bubble-sort. But the while() in main doesn't work.
          > Can somebody tell me why? thanks a lot~~~
          > here is the code
          >
          > #include <stdio.h>
          > #include <stdlib.h>
          > #include <time.h>
          > #define SIZE 20
          >
          > int check_repeat(in t a, int randnum, int randarray[])
          > {
          > int z=0,mark=0;
          > while(z<a)
          > {
          > if (randnum == randarray[z])
          > {
          > mark++;
          > break;
          > }
          > a++;
          > }
          >
          > if (mark == 0)
          > return 1;
          > else
          > return 0;
          > }
          >
          > void check_double(in t sorted[])
          > {
          > int a=0,b=0;
          >
          > while(a <= SIZE-1)
          > {
          > b= a + 1;
          > if (sorted[a] > sorted[b])
          > printf("**error of (sorted[%d] > sorted[%d])\n",a,b);
          > a++;
          > }
          > }
          >
          > int main()
          > {
          > printf("start!! \n");
          > int bu[SIZE]={0};
          > int i=0,temp=0,j=0, checkpoint=0;
          >
          > srand((unsigned )time(NULL));
          > printf("i=%d\n" ,i);
          >
          > //HERE IS THE PROBLEM!!!
          > while(i<=(SIZE-1))
          > {
          > printf("in while--i=%d ",i);
          > temp=rand();
          > checkpoint = check_repeat(i, temp,bu);
          > if (checkpoint == 1)
          > bu[i] = temp;
          > else
          > continue;
          > i++;[/color]

          You most likely want the increment of i to occur as a part of the else
          clause.

          Comment

          • John Bode

            #6
            Re: Why the while() doesn't work?


            Scorpio wrote:[color=blue]
            > hi,
            > I'm trying to run a bubble-sort. But the while() in main doesn't work.[/color]

            Define "doesn't work."
            [color=blue]
            > Can somebody tell me why? thanks a lot~~~
            > here is the code
            >
            > #include <stdio.h>
            > #include <stdlib.h>
            > #include <time.h>
            > #define SIZE 20
            >
            > int check_repeat(in t a, int randnum, int randarray[])
            > {
            > int z=0,mark=0;
            > while(z<a)
            > {
            > if (randnum == randarray[z])
            > {
            > mark++;
            > break;
            > }
            > a++;[/color]

            Actually, *this* is your problem. As written, this doesn't make any
            sense; the loop will continue until a overflows. I think you mean to
            increment z here instead of a.
            [color=blue]
            > }
            >
            > if (mark == 0)
            > return 1;
            > else
            > return 0;
            > }
            >
            > void check_double(in t sorted[])
            > {
            > int a=0,b=0;
            >
            > while(a <= SIZE-1)
            > {
            > b= a + 1;
            > if (sorted[a] > sorted[b])
            > printf("**error of (sorted[%d] > sorted[%d])\n",a,b);
            > a++;
            > }
            > }
            >
            > int main()
            > {
            > printf("start!! \n");
            > int bu[SIZE]={0};
            > int i=0,temp=0,j=0, checkpoint=0;
            >
            > srand((unsigned )time(NULL));
            > printf("i=%d\n" ,i);
            >
            > //HERE IS THE PROBLEM!!!
            > while(i<=(SIZE-1))
            > {
            > printf("in while--i=%d ",i);[/color]

            Add a newline to the format string, otherwise it won't get flushed.
            [color=blue]
            > temp=rand();
            > checkpoint = check_repeat(i, temp,bu);
            > if (checkpoint == 1)
            > bu[i] = temp;
            > else
            > continue;
            > i++;
            > }//end while
            >[/color]

            FWIW, it *is* working, albeit *very* slowly. As I said above,
            increment z instead of a in the check_repeat() function.
            [color=blue]
            > printf("\norigi nal-->\n");
            > for(i=0;i<=SIZE-1;i++)
            > printf(" %d ",bu[i]);
            > printf("\n");
            >
            > for (i=0;i<=SIZE-2;i++)
            > for (j=i+1;j<=SIZE-1;j++)
            > {
            > if(bu[i]>bu[j])
            > {
            > temp=bu[i];
            > bu[i]=bu[j];
            > bu[j]=temp;
            > }/* end if*/
            > } /* end i-for*/
            >
            > check_double(bu );
            >
            > printf("after-->\n");
            > for (i=0;i<=SIZE-1;i++)
            > printf(" %d ",bu[i]);
            > printf("\n");
            >
            > return 0;
            >
            > }/* end main*/[/color]

            Comment

            • Scorpio

              #7
              Re: Why the while() doesn't work?

              Hi guys,
              First, thank you for all of you about answer my question.
              And now, I have found where the problem is. Here is the
              final code. There is still bug in it,maybe. If you found it,
              please re-post here. Thank you again.
              ---------------------------------------------------------------------------------
              #include <stdio.h>
              #include <stdlib.h>
              #include <time.h>
              #define SIZE 100

              int check_repeat(in t a, int randnum, int randarray[])
              {
              int z=0,mark=0;
              while(z<a)
              {
              if (randnum == randarray[z])
              {
              mark++;
              break;
              }
              z++;
              }

              if (mark == 0)
              return 1;
              else
              return 0;
              }

              void check_double(in t sorted[])
              {
              int a=0,b=0;

              while(a <= SIZE-2)
              {
              b= a + 1;
              if (sorted[a] > sorted[b])
              printf("\n**err or of (sorted[%d] > sorted[%d])\n",a,b);
              a++;
              }
              }

              int main()
              {
              printf("start!! \n");
              int bu[SIZE]={0};
              int i=0,temp=0,j=0, checkpoint=1;

              srand((unsigned )time(NULL));
              while(i<=SIZE-1)
              {
              bu[i]=rand();

              if(i>=1)
              checkpoint = check_repeat(i, bu[i],bu);

              if (checkpoint == 0)
              continue;
              else
              i++;
              }//end while

              printf("\norigi nal-->\n");
              for(i=0;i<=SIZE-1;i++)
              printf("%d\n",b u[i]);
              printf("\n");

              for (i=0;i<=SIZE-2;i++)
              for (j=i+1;j<=SIZE-1;j++)
              {
              if(bu[i]>bu[j])
              {
              temp=bu[i];
              bu[i]=bu[j];
              bu[j]=temp;
              }/* end if*/
              } /* end i-for*/

              printf("after-->\n");
              for (i=0;i<=SIZE-1;i++)
              printf("%d\n",b u[i]);
              printf("\n");

              check_double(bu );

              return 0;

              }/* end main*/

              Comment

              • Vladimir S. Oka

                #8
                Re: Why the while() doesn't work?


                Scorpio wrote:[color=blue]
                > Hi guys,
                > First, thank you for all of you about answer my question.
                > And now, I have found where the problem is. Here is the
                > final code. There is still bug in it,maybe. If you found it,
                > please re-post here. Thank you again.[/color]

                <snip "maybe" buggy code>

                Why would we want to test for you? I don't remember joining for the
                test department of your company. OTH, if you're prepared to pay a day's
                consulting fees, I may reconsider.

                --
                BR, Vladimir

                Comment

                • Scorpio

                  #9
                  Re: Why the while() doesn't work?

                  I'm so sorry that I said the wrong words.
                  Thank you , again.

                  Comment

                  • Chris Dollin

                    #10
                    Re: Why the while() doesn't work?

                    Scorpio wrote:
                    [color=blue]
                    > if (mark == 0)
                    > return 1;
                    > else
                    > return 0;[/color]

                    Hell's teeth, man, what's wrong with `return mark == 0;`?

                    --
                    Chris "x.f(y) == f(x, y) == (x, y).f" Dollin
                    The shortcuts are all full of people using them.

                    Comment

                    • Scorpio

                      #11
                      Re: Why the while() doesn't work?

                      return mark ==0; ?????????

                      I didn't write these. what do you mean? I don't understand!!

                      Comment

                      • santosh

                        #12
                        Re: Why the while() doesn't work?

                        Scorpio wrote:[color=blue]
                        > return mark ==0; ?????????
                        >
                        > I didn't write these. what do you mean? I don't understand!![/color]

                        Please quote context in your replies. The regulars, most of whom do
                        *not* use Google, may not be able to always access previous posts. So
                        you should quote the post to which you're replying as I've done above
                        for your post. Do take time to read the material at the following URLs:

                        <http://cfaj.freeshell. org/google/>
                        <http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>
                        <http://www.safalra.com/special/googlegroupsrep ly/>

                        Coming to your post, yes, you *did* write the construct in your post.
                        Look it up thread. Chris is just suggesting a concise form of returning
                        the values you're doing via your IF ELSE construct. Look up relational
                        expressions in your C text.

                        Comment

                        • John Bode

                          #13
                          Re: Why the while() doesn't work?


                          Scorpio wrote:[color=blue]
                          > return mark ==0; ?????????
                          >
                          > I didn't write these. what do you mean? I don't understand!![/color]

                          He means, why did you write

                          if (mark == 0)
                          return 1;
                          else
                          return 0;

                          instead of

                          return mark==0;

                          which accomplishes the same thing in a more concise and readable
                          manner?

                          Comment

                          • Scorpio

                            #14
                            Re: Why the while() doesn't work?

                            I've learnt it. Thaks for your suggest , Chris.
                            And thank you guys.

                            Comment

                            Working...