如何修复此代码

how do I fix this code

本文关键字:代码 何修复      更新时间:2023-10-16

我正在参加一门在线课程。

家庭作业问题是:

使用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";
}