srand(time(NULL));
int rand = Randomiser();
temp = placement[49];
//Loops variables in array to produce scrolling
for(i = 0;i<50; i++)
{
placement[49-i] = placement[49-(i+1)];
}
if(temp==2)
{
temp=temp2;
}
if(rand>90)
{
temp2=temp;
temp=2;
}
placement[0]=temp;
所以我把它改成了这样的东西,现在插入了我的值,但问题是这个值一次插入大约12次。所以6个位置= 2
答案 0 :(得分:0)
它不会忽略if
语句,它只是在内部for
循环的第一次迭代后才计算为false。你的代码:
for( ... )
{
bool controlRod = true;
for( ... )
{
if( ... && controlRod == true )
{
// ...
}
// ...
controlRod = false; // <-- set to false so condition is always false
}
}
如果我们展开内循环,我们得到类似的东西:
for( ... )
{
bool controlRod = true;
// iteration 1 ----------------------------------------------------
if( ... && controlRod == true )
{
// ...
}
// ...
controlRod = false; // <-- set to false so condition is always false
// iteration 2 ------------------------------------------------------
if( ... && controlRod == true ) // controlRod was set to false,
// so this will never be true
{
// ...
}
// ...
controlRod = false; // <-- set to false so condition is always false
// iteration 3 --------------------------------------------------
if( ... && controlRod == true ) // controlRod was set to false,
// so this will never be true
{
// ...
}
// ...
controlRod = false; // <-- set to false so condition is always false
// more iterations...
}
答案 1 :(得分:0)
以下代码是您真正想要的吗?
在这个例子中,i-loop旋转环。请注意,通常您不会移动所有元素,只需通过(start+i)%size
移动环开始和地址。
注意,对于排序,您有std::sort
,这比冒泡排序更有效。
#include <cstdlib>
#include <vector>
#include <iostream>
int Randomiser() {
return rand() % 101;
}
int main() {
srand(time(NULL));
std::vector<int> placement(50,0);
for(int j = 0; j<100; j++)
{
int temp, temp2;
int rand = Randomiser();
bool controlRod=true;
if(rand<80)
{
temp2 = placement[49];
placement[49]=2;
}
temp = placement[49];
for(int i = 0; i<50; i++)
{
placement[49-i] = placement[49-(i+1)];
}
placement[0] = temp;
}
std::cout << "\nThis time in placement[0]: " << placement[0] << "\n";
}
/**
Local Variables:
compile-command: "g++ -g test.cc -o test.exe; ./test.exe"
End:
*/