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*/ 13 1780
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 thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: BigAl |
last post by:
I am using the following code in a servlet to send data back to a J2ME
application:
|
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:
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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?
...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |