472,989 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

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(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*/

Mar 23 '06 #1
13 1763
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.
Mar 23 '06 #2
//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

Mar 23 '06 #3
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.
Mar 23 '06 #4
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.

Mar 23 '06 #5

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


Mar 23 '06 #6
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*/

Mar 23 '06 #7

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

Mar 23 '06 #8
I'm so sorry that I said the wrong words.
Thank you , again.

Mar 23 '06 #9
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.
Mar 23 '06 #10
return mark ==0; ?????????

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

Mar 23 '06 #11
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.

Mar 23 '06 #12

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?

Mar 23 '06 #13
I've learnt it. Thaks for your suggest , Chris.
And thank you guys.

Mar 24 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: BigAl | last post by:
I am using the following code in a servlet to send data back to a J2ME application:
24
by: Andrew Koenig | last post by:
PEP 315 suggests that a statement such as do: x = foo() while x != 0: bar(x) be equivalent to while True:
9
by: Ben | last post by:
I have two 'Do While Not' statements, that are getting information from the same recordset. If I comment out the first one I can get the results for the second one, and vice-versa. Why is this...
75
by: Greg McIntyre | last post by:
I have a Python snippet: f = open("blah.txt", "r") while True: c = f.read(1) if c == '': break # EOF # ... work on c Is some way to make this code more compact and simple? It's a bit...
36
by: invni | last post by:
I have a nested while. How do I go from the inner while to the beginning of the outer while? Can this be done without using goto? while_1() { some codes here while_2() { if true go to the...
147
by: Michael B Allen | last post by:
Should there be any preference between the following logically equivalent statements? while (1) { vs. for ( ;; ) { I suspect the answer is "no" but I'd like to know what the consensus is
3
by: Steve Richfie1d | last post by:
Now that it finally works, I'm in the process of converting my large rapid-prototyped Access app into production VB, one DDE-linked piece at a time. One piece now already in VB takes ~2 seconds to...
5
by: =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?= | last post by:
Good evening all, I am trying to write a process that uses a while loop to cycle multiple files from an array throught the StreamReader Process. The whole thing works using: Dim...
1
by: =?ISO-8859-2?Q?Grzegorz_Kokosi=F1ski?= | last post by:
Hi! I waste a lot of my time figuring why this snip of code doesn`t work. And still without any solution ;/ Does anybody know why this handler cannot handle SIGINT signal? What did I wrong? ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.