Why the while() doesn't work?

S

Scorpio

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(int 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(int sorted[])
{
int a=0,b=0;

while(a <= SIZE-1)
{
b= a + 1;
if (sorted[a] > sorted)
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 = temp;
else
continue;
i++;
}//end while

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

for (i=0;i<=SIZE-2;i++)
for (j=i+1;j<=SIZE-1;j++)
{
if(bu>bu[j])
{
temp=bu;
bu=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);
printf("\n");

return 0;

}/* end main*/
 
I

Ian Collins

Scorpio wrote:>
//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 = temp;
else
continue;
i++;
}//end while

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.
 
C

code break

//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 = temp;
bu[i++]=temp;
else else
continue; {
i++;
i++;

continue;

}
}//end while
 
I

Ian Collins

code said:
//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 = temp;
bu[i++]=temp;
else else
continue; {
i++;
i++;

continue;

}
}//end while

Well I can't even read this, please don't post tabs and please quote
some context when replying.
 
S

santosh

Scorpio said:
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(int 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(int sorted[])
{
int a=0,b=0;

while(a <= SIZE-1)
{
b= a + 1;
if (sorted[a] > sorted)
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 = temp;
else
continue;
i++;


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

John Bode

Scorpio said:
hi,
I'm trying to run a bubble-sort. But the while() in main doesn't work.

Define "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(int a, int randnum, int randarray[])
{
int z=0,mark=0;
while(z<a)
{
if (randnum == randarray[z])
{
mark++;
break;
}
a++;

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.
}

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

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

while(a <= SIZE-1)
{
b= a + 1;
if (sorted[a] > sorted)
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);


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


FWIW, it *is* working, albeit *very* slowly. As I said above,
increment z instead of a in the check_repeat() function.
printf("\noriginal-->\n");
for(i=0;i<=SIZE-1;i++)
printf(" %d ",bu);
printf("\n");

for (i=0;i<=SIZE-2;i++)
for (j=i+1;j<=SIZE-1;j++)
{
if(bu>bu[j])
{
temp=bu;
bu=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);
printf("\n");

return 0;

}/* end main*/
 
S

Scorpio

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(int 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(int sorted[])
{
int a=0,b=0;

while(a <= SIZE-2)
{
b= a + 1;
if (sorted[a] > sorted)
printf("\n**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=1;

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

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

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

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

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

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

check_double(bu);

return 0;

}/* end main*/
 
V

Vladimir S. Oka

Scorpio said:
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.

<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.
 
S

Scorpio

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

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

santosh

Scorpio said:
return mark ==0; ?????????

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

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/googlegroupsreply/>

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.
 
J

John Bode

Scorpio said:
return mark ==0; ?????????

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

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top