为什么while()不起作用? [英] Why the while() doesn't work?

查看:121
本文介绍了为什么while()不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试进行泡泡排序。但是主要的while()不起作用。

有人可以告诉我为什么吗?非常感谢~~~

这里是代码


#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 ++;

休息;

}

a ++;

}


if(mark == 0 )

返回1;

其他

返回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");

i nt bu [SIZE] = {0};

int i = 0,temp = 0,j = 0,checkpoint = 0;


srand((无符号)时间(NULL));

printf(" i =%d \ n",i);


//这里是问题!!!

while(i< =(SIZE-1))

{

printf(" in while - i =% d",i);

temp = rand();

checkpoint = check_repeat(i,temp,bu);

if(b) checkpoint == 1)

bu [i] = temp;

else

继续;

i ++; <


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");


返回0;


} / * end main * /


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

推荐答案

Scorpio写道:>
Scorpio wrote:>
//这里是问题!!!
而(i< =(SIZE) -1))
{
printf(" in while - i =%d",i);
temp = rand();
checkpoint = check_repeat(i ,temp,bu);
if(checkpoint == 1)
bu [i] = temp;

继续;
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



再看看其他地方(它通常被认为更好,更安全

在if的分支上使用大括号,即使只有一个语句)。

在这种情况下我会怎么样?它增加了吗?如果你不确定,

继续检查。


-

Ian Collins。


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.


//这就是问题!!!

while(i< =(SIZE-1))

{

printf(" in while - i =%d",i);

temp = rand();

checkpoint = check_repeat( i,temp,bu);

if(checkpoint == 1)替换为

if(checkpoint == 1)

bu [i ] = temp;

bu [i ++] = temp;

否则

继续; {

i ++;

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) replace with
if(checkpoint==1)
bu[i] = temp;
bu[i++]=temp;
else else
continue; {
i++;
i++;

continue;

}
}//end while


代码中断写道:
//这里是问题!! !
while(i< =(SIZE-1))
{/ / printf(" in while - i =%d",i);
temp = rand ();
checkpoint = check_repeat(i,temp,bu);
if(checkpoint == 1)替换为
if(checkpoint == 1)
bu [i] = temp;
bu [i ++] = temp;
否则
继续; {
i ++;
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) replace with
if(checkpoint==1)
bu[i] = temp;
bu[i++]=temp;
else else
continue; {
i++;
i++;

continue;

}
}//end while



好​​吧,我甚至不能读这个,请不要张贴标签,请在回复时引用

一些上下文。


-

Ian Collins。


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

--
Ian Collins.


这篇关于为什么while()不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆