如何修复此代码
how do I fix this code
我正在参加一门在线课程。
家庭作业问题是:
使用rand()函数打印1到100之间的前50个数字。试着把每5个随机数只放在一行。使用setw()函数进行对齐。
我目前有以下内容。我知道这是一个无限循环,我认为问题出在我的if
语句中。
#include <iostream>
#include <time.h>
#include <iomanip>
using namespace std;
int main() {
int random_number;
srand(time(NULL));
random_number = rand() % 100 + 1;
cout << "50 random numbers are:" << endl;
while (random_number < 100, rand() % 100 + 1)
{
cout << random_number << setw(5) << endl;
if (random_number == 100)
break;
}
system("pause");
}
您的while
有故障,请参阅前面的注释。这应该行得通。
#include <iostream>
#include <time.h>
#include <iomanip>
using namespace std;
int main() {
int random_number;
srand(time(NULL));
cout << "50 random numbers are:" << endl;
for(int i=0; i<50; i++)
{
random_number = rand() % 100 + 1;
cout << setw(5) << random_number << endl;
}
system("pause");
}
或者,如果您想使用while
,
#include <iostream>
#include <time.h>
#include <iomanip>
using namespace std;
int main() {
int random_number;
srand(time(NULL));
cout << "50 random numbers are:" << endl;
int counter=0;
int random_number;
while(counter<50)
{
counter++;
random_number = rand() % 100 + 1;
cout << setw(5) << random_number << endl;
}
system("pause");
}
但只使用CCD_ 4,因为它更紧凑。
我知道上面的许多代码对op都很好。因为正如我们所看到的,op对编码来说是新的,这是我的简化尝试。
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main() {
int random_number, count=0; //Using count as a counter.
srand(time(NULL));
cout << "50 random numbers are:" << endl;
//checking two conditions. count and random number generated is less than 100
while (count <50)
{
if((random_number = rand() % 100 + 1) > 100 ) continue;
count++;
cout<< setw(5) << random_number ;
//Try to put every 5 random number in one line only.
if (count %5==0) cout<<endl;
}
}
代码问题:
- 你只生成了一次随机数,因为它在while循环
- while循环没有正确的停止条件。给出无限循环
- 您的解决方案格式与问题中的要求不符
谢谢。
我认为你发布的内容是,你想打印50个介于1到100之间的随机数字,不是吗?如果是这样的话,我注意到错误出现在while循环的leaf条件中。你需要一个变量来计算显示的数字,你应该检查这个变量是否达到了显示的50个数字。
srand(time(NULL));
random_number = rand() % 100 + 1;
cout << "50 random numbers are:" << endl;
for (int i = 0;i<50;++i)
{
random_number = rand() % 100 + 1
cout << random_number << setw(5) << endl;
// if (random_number == 100)
// break;
}
system("pause");
按照你编码离开条件的方式,可能永远不会离开。退出循环时必须获得100分
我希望这篇文章对你有帮助谢谢。
由于这是家庭作业,我不能直接给出答案。然而,这应该算是现代c++的一个很好的演示:
查看Coliru直播
#include <boost/spirit/include/karma.hpp>
#include <boost/iterator/function_input_iterator.hpp>
#include <boost/random.hpp>
boost::random::variate_generator<boost::mt19937, boost::uniform_int<int> > rdngen(
boost::mt19937(), boost::uniform_int<int>(1, 1000));
int main() {
auto f = boost::make_function_input_iterator(rdngen, 0),
l = boost::make_function_input_iterator(rdngen, 100);
auto range = boost::make_iterator_range(f, l);
using namespace boost::spirit::karma;
std::cout << format_delimited(columns(5) [ right_align(10) [ *auto_ ] ], "t", range) << "n";
}
相关文章:
- 如何修复sfml c++代码编译错误
- 如何修复我的最大公约数代码?它适用于除零和零以外的所有数字
- 我的代码中的意外价值以及我如何修复它
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- C++指针问题:如何修复这些代码中的错误?
- 如何修复 Arduino LED 串控制器的代码?
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 如何修复初学者C++代码中发生的未定义行为
- 终端进程终止,退出代码为:1有什么方法可以修复它吗
- 我在哪里修复有关模板的C++代码?
- 无论代码长度如何,以下代码的内存要求有何不同?
- 如何修复以下代码以进行二叉树级别订单打印
- 如何在以下代码中修复此逻辑错误
- 如何修复此代码对几个测试用例的错误答案?
- 修复第三方代码:"error: ‘enable_if’ in namespace ‘std’ does not name a template type"
- 如何修复将传输到 java 的 2d 数组代码使用 JNI
- 如何修复代码中的"Invalid memory reference"错误?
- 如何修复使用代码 2 退出"CL.exe"
- 如何在我的代码中修复无穷循环?我认为它跳过了第二个CIN,因此它一直在循环
- 如何在导致应用程序崩溃的代码中修复指令