|
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[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("\noriginal-->\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*/ | |
Share:
|
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[i] = 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.
--
Ian Collins. | | |
//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 | | |
code break 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) replace with if(checkpoint==1) bu[i] = 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.
--
Ian Collins. | | |
Scorpio wrote: 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[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++;
You most likely want the increment of i to occur as a part of the else
clause. | | |
Scorpio wrote: 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[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);
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[i] = 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[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*/ | | |
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[b])
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[i]=rand();
if(i>=1)
checkpoint = check_repeat(i,bu[i],bu);
if (checkpoint == 0)
continue;
else
i++;
}//end while
printf("\noriginal-->\n");
for(i=0;i<=SIZE-1;i++)
printf("%d\n",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*/
printf("after-->\n");
for (i=0;i<=SIZE-1;i++)
printf("%d\n",bu[i]);
printf("\n");
check_double(bu);
return 0;
}/* end main*/ | | |
Scorpio wrote: 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.
--
BR, Vladimir | | |
I'm so sorry that I said the wrong words.
Thank you , again. | | |
Scorpio wrote: if (mark == 0) return 1; else return 0;
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. | | |
return mark ==0; ?????????
I didn't write these. what do you mean? I don't understand!! | | |
Scorpio wrote: 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. | | |
Scorpio wrote: 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? | | |
I've learnt it. Thaks for your suggest , Chris.
And thank you guys. | | This discussion thread is closed Replies have been disabled for this discussion. Similar topics
4 posts
views
Thread by BigAl |
last post: by
|
24 posts
views
Thread by Andrew Koenig |
last post: by
|
9 posts
views
Thread by Ben |
last post: by
|
75 posts
views
Thread by Greg McIntyre |
last post: by
|
36 posts
views
Thread by invni |
last post: by
|
147 posts
views
Thread by Michael B Allen |
last post: by
|
3 posts
views
Thread by Steve Richfie1d |
last post: by
|
5 posts
views
Thread by =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?= |
last post: by
|
1 post
views
Thread by =?ISO-8859-2?Q?Grzegorz_Kokosi=F1ski?= |
last post: by
| | | | | | | | | | |