C++ 我的变量black1和black2在c++;?
我的教授强迫我使用一个叫做RAPTOR的程序,preety会创建并执行流程图和伪代码。我一直在转换我的流程图,而不是用实际语言书写。我终于让我的程序编译了,但是现在它不会给我和RAPTOR程序一样的结果,我的black1变量一直返回0,而black2一直在吐出像1000这样的数字。无论如何,任何帮助都将不胜感激,谢谢 代码如下:C++ 我的变量black1和black2在c++;?,c++,C++,我的教授强迫我使用一个叫做RAPTOR的程序,preety会创建并执行流程图和伪代码。我一直在转换我的流程图,而不是用实际语言书写。我终于让我的程序编译了,但是现在它不会给我和RAPTOR程序一样的结果,我的black1变量一直返回0,而black2一直在吐出像1000这样的数字。无论如何,任何帮助都将不胜感激,谢谢 代码如下: #include <iostream> #include <string> #include <cstdlib> #include
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int trials;
int first;
string bag;
int j;
float percent;
string temp;
int black2;
int runs;
int l;
int black1;
int firstdraw;
int c;
string possible;
int seconddraw;
srand(time(NULL));
l = bag.length();
int r = rand() % 4;
possible ="bw";
runs =0;
while (!(runs>9))
{
black1 =0;
black2 =0;
runs =runs+1;
trials =0;
while (!(trials==1000))
{
trials =trials+1;
bag ="bbwww";
l = bag.length();
c =1;
temp =" ";
while (!(c==5))
{
j = r ;
temp[1] = bag[c];
bag[c] = bag[j];
bag[j] = temp[1];
c =c+1;
}
c =1;
j = r;
firstdraw =bag[j];
if (firstdraw==possible[1])
{
black1 = black1+1;
first = 1;
}
else
{
first = 0;
}
while (!(j>l-1))
{
bag[j] = bag[j + 1];
j =j+1;
}
l =l-1;
j = r;
seconddraw = bag[j];
if (seconddraw==possible[1] && first==1)
{
black2 =black2+1;
}
else
{
}
}
percent = 100.0 * black2/black1;
cout << black2 << " " << black1 << endl;
cout << "Percentage for run #" << runs << " and " << trials << " trials: %" << percent << endl; }
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
国际试验;
int优先;
绳袋;
int j;
浮动百分比;
字符串温度;
int black2;
整数运行;
int l;
int black1;
int firstdraw;
INTC;
字符串可能;
int二次抽签;
srand(时间(空));
l=袋长();
int r=rand()%4;
可能=“bw”;
运行次数=0;
而(!(运行>9))
{
黑1=0;
black2=0;
运行次数=运行次数+1;
试验=0;
而(!(试验=1000))
{
试验=试验+1;
bag=“bbwww”;
l=袋长();
c=1;
温度=”;
而(!(c==5))
{
j=r;
温度[1]=袋[c];
袋[c]=袋[j];
袋[j]=温度[1];
c=c+1;
}
c=1;
j=r;
firstdraw=袋[j];
if(firstdraw==可能[1])
{
黑色1=黑色1+1;
第一个=1;
}
其他的
{
第一个=0;
}
而(!(j>l-1))
{
袋[j]=袋[j+1];
j=j+1;
}
l=l-1;
j=r;
第二次抽取=袋[j];
if(seconddraw==可能[1]&&first==1)
{
black2=black2+1;
}
其他的
{
}
}
百分比=100.0*black2/black1;
cout很抱歉,我不会修复你的全部代码,我让它更具可读性,使用名称空间std;
删除了那难看的,并给你留下了一些评论。检查这个,修复你的代码,如果需要的话,看看一些教程。我用这段代码反复得到一个调试断言。我猜你从未运行过任何调试器和just开启了释放模式
也不要忘记初始化变量。您可能会遇到未定义行为的风险。
这两根绳子走进一家酒吧坐下。酒保说:“那是什么?”
第一个字符串是:“我想我要一杯啤酒,quag fulk boorg jdk^CjfdLk jk3s d#f67howe%^U r89nvy~~owmc63^Dz x.xvcu”
“请原谅我的朋友,”第二个字符串说,“他不是空终止的。”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
int试验=0;
int first=0;
int c=0;
int j=0;
int l=0;
int black2=0;
整数=0;
int black1=0;
int firstdraw=0;
int=0;
浮动百分比=0.0f;
std::string temp=“\0”;
std::string bag=“\0”;
std::string可能=“\0”;
srand(时间(空));
l=袋长();
fflush(stdin);//您可能希望这样做以从rand()中获取随机数,否则它可能会反复使用相同的数
int r=0;
r=rand()%4;
可能=“bw”;
运行次数=0;
而(!(运行>9))
{
黑1=0;
black2=0;
runs=runs+1;//为什么没有for循环?
试验=0;
而(!(试验=1000))
{
trials=trials+1;//为什么循环没有?
bag=“bbwww”;
l=袋长();
c=1;
温度=”;
而(!(c==5))
{
j=r;
温度[1]=袋[c];//温度[1]l-1)
{
bag[j]=bag[j+1];//很可能也会导致调试断言。。。
j=j+1;
}
l=l-1;
j=r;
第二次抽取=袋[j];
if(seconddraw==可能[1]&&first==1)
{
black2=black2+1;
}
其他的
{
//不需要!?!
}
}
百分比=100.0*black2/black1;
std::cout很抱歉,我不会修复你的整个代码,我让它更具可读性,使用名称空间std;
删除了那难看的,并给你留下了一些注释。检查这个,修复你的代码,如果需要的话,看看一些教程。我用这段代码一次又一次地得到了调试断言。我猜你从来没有运行过任何调试程序nd刚刚开启了释放模式
也不要忘记初始化变量。您可能会遇到未定义行为的风险。
这两根绳子走进一家酒吧坐下。酒保说:“那是什么?”
第一个字符串是:“我想我要一杯啤酒,quag fulk boorg jdk^CjfdLk jk3s d#f67howe%^U r89nvy~~owmc63^Dz x.xvcu”
“请原谅我的朋友,”第二个字符串说,“他不是空终止的。”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
int试验=0;
int first=0;
int c=0;
int j=0;
int l=0;
int black2=0;
整数=0;
int black1=0;
int firstdraw=0;
int=0;
浮动百分比=0.0f;
std::string temp=“\0”;
std::string bag=“\0”;
std::string可能=“\0”;
srand(时间(空));
l=袋长();
fflush(stdin);//您可能希望这样做以从rand()中获取随机数,否则它可能会反复使用相同的数
int r=0;
r=rand()%4;
可能=“bw”;
运行次数=0;
而(!(运行>9))
{
黑1=0;
black2=0;
runs=runs+1;//为什么没有for循环?
试验=0;
而(!(试验=1000))
{
trials=trials+1;//为什么循环没有?
bag=“bbwww”;
l=袋长();
c=1;
温度=”;
而(!(c==5))
{
j=r;
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <time.h>
#include <stdlib.h>
int main()
{
int trials = 0;
int first = 0;
int c = 0;
int j = 0;
int l = 0;
int black2 = 0;
int runs = 0;
int black1 = 0;
int firstdraw = 0;
int seconddraw = 0;
float percent = 0.0f;
std::string temp = "\0";
std::string bag = "\0";
std::string possible = "\0";
srand (time(NULL));
l = bag.length();
fflush(stdin); // You may want to do this to get random numbers from rand() otherwise it may use the same number over and over again
int r = 0;
r = rand() % 4;
possible = "bw";
runs = 0;
while (!(runs > 9))
{
black1 = 0;
black2 = 0;
runs = runs + 1; //Why no for loop ?
trials = 0;
while (!(trials == 1000))
{
trials = trials + 1; //Why no for loop ?
bag ="bbwww";
l = bag.length();
c = 1;
temp =" ";
while (!(c == 5))
{
j = r;
temp[1] = bag[c]; // temp[1] <- debug assertion, can't work
bag[c] = bag[j];
bag[j] = temp[1]; // temp[1] <- debug assertion, can't work either
c = c + 1;
}
c = 1;
j = r;
firstdraw = bag[j];
if (firstdraw == possible[1])
{
black1 = black1 + 1;
first = 1;
}
else
{
first = 0;
}
while (!(j > l - 1))
{
bag[j] = bag[j + 1]; //Will most likely cause a debug assertion too...
j = j + 1;
}
l = l - 1;
j = r;
seconddraw = bag[j];
if (seconddraw == possible[1] && first==1)
{
black2 = black2 + 1;
}
else
{
//Not needed !?!
}
}
percent = 100.0 * black2 / black1;
std::cout << black2 << " " << black1 << std::endl;
std::cout << "Percentage for run #" << runs << " and " << trials << " trials: %" << percent << std::endl;
}
return 0;
}