C++ C++;在乘法图中放置随机值(嵌套循环)

C++ C++;在乘法图中放置随机值(嵌套循环),c++,C++,我为整数1…10创建了一个乘法表。代码如下: for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { std::cout << "\t" << i * j; } std::cout << std::endl; } 我正在努力解决的问题是,我需要在乘法表中插入一个表示错误值的随机数。例如: 1 2 3 4

我为整数1…10创建了一个乘法表。代码如下:

for (int i = 1; i <= 10; i++) {
    for (int j = 1; j <= 10; j++) {
      std::cout << "\t" << i * j;
    }
    std::cout << std::endl;
}
我正在努力解决的问题是,我需要在乘法表中插入一个表示错误值的随机数。例如:

1   2   3   4   5   6   7   8   9   10
2   4   6   8   10  12  14  16  18  20
3   6   9   13  15  18  21  24  27  30
正如您在本例中所看到的,错误值为13,其中3x4=12,但数字为13

我应该使用另一个循环来插入随机数吗?如果是这样,怎么做?

您可以使用
头文件来完成此操作

std::random_device rd;
std::mt19937 gen(rd());
// it produces uniformly distributed random integers in the range [a, b] (both inclusive)
std::uniform_int_distribution<> dis(0, 1); // either add 0 or 1 (1 will be the error -- can change a,b according to your need)

for (int i = 1; i <= 10; i++)
{
  for (int j = 1; j <= 10; j++)
  {
    std::cout << "\t" << i * j + dis(gen); // just add the error value to your multiplication
  }
  std::cout << std::endl;
}
std::随机设备rd;
标准:mt19937 gen(rd());
//它产生[a,b]范围内均匀分布的随机整数(包括两者)
标准:均匀分布图(0,1);//添加0或1(错误为1--可以根据需要更改a、b)
对于(int i=1;i
#包括//输入/输出
#包括
结构字段数据{
使用行=无符号整数;
使用Col=无符号整数;
常量行行{10};
const cols{10};
行错误行;
错误的颜色;
无符号int err_值;
FieldData(){}//对行/列使用默认值
FieldData(Row max_Row,Col max_Col):行(std::move(max_Row)),列(std::move(max_Col)){}//为行/列使用给定的值
};
无效打印字段(常量字段数据和数据)
{
对于(FieldData::Row Row=1;Row
我应该使用另一个循环来插入随机数吗?如果是,如何进行

您只插入了一个错误,因此对此进行循环没有多大意义

那么,为什么不先决定随机数的去向呢?一旦知道了,就可以修改内部循环体,如下所示:

int value = i * j;
if ((i == error_location.row) and (j = error_location.column)) {
    value = // code for introducing an error, e.g. sample a random answer,
            // or a random offset added to the correct answer etc.
}
std::cout << "\t" << i * j;
int值=i*j;
if((i==error\u location.row)和(j=error\u location.column)){
value=//引入错误的代码,例如,随机抽取一个答案,
//或者在正确答案上添加随机偏移量等。
}

std::cout所以感谢大家的投入——特别是@Wolfgang。 我确实读过C++11的随机头文件及其函数。 我确实创建了一个带有随机函数的源代码,还有一个没有。 任何关于如何使我的源代码更高效的反馈都将不胜感激。 除此之外,它实现了我需要它做的事情

#include <iostream>
#include <ctime>
#include <iomanip>

const unsigned int ROWS = 10;
const unsigned int COLUMNS = 10;

void randomValue(unsigned int &, unsigned int &, unsigned int &);

int main() 
{   
  unsigned int rValue;
  unsigned int cValue;
  unsigned int pValue;

  unsigned int rowError; 
  unsigned int columnError;
  unsigned int productError;

  unsigned int userRowGuess;
  unsigned int userColumnGuess;

  char choice;  

  do 
  { 
    randomValue(rValue, cValue, pValue); 

    std::cout << "\033[2J\033[0;0H"; // This code clears the screen 
    std::cout << std::endl << std::endl;       
    std::cout << std::setw(35) << "++++++++++++++++++++++++" << std::endl;
    std::cout << std::setw(35) << "+ Multiplication Table +" << std::endl;
    std::cout << std::setw(35) << "++++++++++++++++++++++++" << std::endl;
    std::cout << std::endl << std::endl;   

    while (true)
    { 
      rowError = rValue;
      columnError = cValue;
      productError = pValue;

      if (productError == rowError * columnError)
      {
        productError--;
      }

      for (unsigned int row = 1; row <= ROWS; row++)
      {
        for (unsigned int column = 1; column <= COLUMNS; column++)
        {
          if (row == rowError && column == columnError)
          { 
            std::cout << "\t" << "" << productError;       
          }
          else
          {
            std::cout << "\t" <<  row * column;
          }
        }
      std::cout << std::endl;
      }  
      break;
    }


    std::cout << std::endl << std::endl;
    std::cout << "\t\t" << "Type in the column number " << std::endl;
    std::cout << "\t\t" << "of the location of the    " << std::endl;
    std::cout << "\t\t" << "error & then press        " << std::endl;
    std::cout << "\t\t" << "[Enter]: ";
    std::cin >> userColumnGuess;
    std::cout << std::endl;

    std::cout << "\t\t" << "Type in the row number " << std::endl;
    std::cout << "\t\t" << "of the location of the    " << std::endl;
    std::cout << "\t\t" << "error & then press        " << std::endl;
    std::cout << "\t\t" << "[Enter]: ";
    std::cin >> userRowGuess;
    std::cout << std::endl;

    if (userRowGuess != rowError && userColumnGuess != columnError)
    {
      std::cout << "\t\t" << "Your answer was incorrect!" << std::endl << std::endl;
      std::cout << "\t\t" << "Error value '" << productError << "' is located" << std::endl;
      std::cout << "\t\t" << "on row " << rowError << ", column " << columnError << "." << std::endl;
    }
    else
    {
      std::cout << "\t\t" << "You are correct! You win!" << std::endl;
    }


    std::cout << std::endl;
    std::cout << "\t\t" << "Would you like to play again?" << std::endl << std::endl;
    std::cout << "\t\t" << "Type in 'Y' for yes or 'N'" << std::endl;
    std::cout << "\t\t" << "for no & then press [Enter]: ";
    std::cin >> choice;

    while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N')
    {
      std::cout << std::endl;
      std::cout << "\t\t" << "Invalid entry. Only 'Y' or 'N'" << std::endl;
      std::cout << "\t\t" << "are accepted answers." << std::endl << std::endl;
      std::cout << "\t\t" << "Would you like to play again?" << std::endl << std::endl;
      std::cout << "\t\t" << "Type in 'Y' for yes or 'N' for" << std::endl;
      std::cout << "\t\t" << "no & then press [Enter]: ";
      std::cin >> choice;
    }   
    std::cout << std::endl;

  } while (choice == 'y' || choice == 'Y'); 

  std::cout << "\t\t" << "Press [Enter] to continue....." << std::endl;
  std::cin.get();
  std::cin.get();

  return 0;
}

void randomValue(unsigned int &rValue, unsigned int &cValue, unsigned int &pValue)
{ 
  srand((unsigned)time(NULL));

  unsigned int r = rValue = (rand() % ROWS) + 1;
  unsigned int c = cValue = (rand() % COLUMNS) + 1;
  unsigned int p = pValue = (rand() % (ROWS * COLUMNS)) + 1;
}
#包括
#包括
#包括
常量无符号整数行=10;
const unsigned int COLUMNS=10;
无效随机值(无符号整数&,无符号整数&,无符号整数&);
int main()
{   
无符号整数右值;
无符号int值;
无符号整数值;
无符号整数行错误;
无符号整数列错误;
无符号整数错误;
无符号整数userRowGuess;
unsigned int userColumnGuess;
字符选择;
做
{ 
随机值(右值、cValue、pValue);

std::cout根据什么情况应该插入错误?您的解释/动机非常模糊,我无法理解您正在尝试做什么。最好提供您已经做了什么,以及您到底需要实现什么,以便我们能够提供帮助。很抱歉,我的程序目标缺乏详细信息。此乘法表rt已被要求具有错误值,因为程序将通过询问行号和列号提示用户指定值错误的位置。如果用户输入的信息错误,程序将提示行和列的正确位置。如果正确,将显示祝贺。这意味着增强我对使用嵌套循环的理解。
(行==4&&col==3)
让程序指定一些随机值,总是在3x4位置,而不是其他位置,这与OP的要求相矛盾。我问过位置应该基于什么条件……也许我理解错了?!这个乘法表从一开始就必须有一个错误值,因为程序会提示用户使用sp通过询问行号和列号来指定值错误的位置。如果用户输入的信息错误,程序将提示行和列的正确位置。如果正确,将显示祝贺。此程序将处于do while循环中-询问用户是否希望再次播放。因此,它是对于随机数的位置,每次都要在不同的位置上进行定位。我必须声明我是新的,不知道随机的头文件和它的函数是什么。我用Tony Gladis开始W/C++第九。我还没有遇到STD::RealthyDead,Autoto,SimultIn ItIdDuffuutuug等等。@沃尔夫冈的代码提供了ACTTL。我要找的是什么!说得对!唯一的一件事是我不知道你写了什么。我还没有进入结构,我才刚刚开始学习类。我还没有听说或读过使用、自动、随机设备rd、mt199937生成器等术语。有没有一种方法可以产生与代码产生的结果相同的结果,但在某种程度上只包含循环,如果它,否则,也许只是课堂。我是C++的新手,我的大脑正在试图了解你写的东西。你能给我一个能最好地教我这些信息的链接吗?
#include <iostream> // input/output
#include <random>

int main()
{
   std::random_device rd;
   std::mt19937 generator(rd());

   unsigned int rows = 10;
   unsigned int cols = 10;
   std::uniform_int_distribution<unsigned int> dist_rows(1, rows); // random for row (1 to value of field_rows)
   std::uniform_int_distribution<unsigned int> dist_cols(1, cols); // random for col (1 to value of field_cols)
   std::uniform_int_distribution<unsigned int> dist_result(1, rows * cols); // random for result

   while (true) // infinite loop - must be interrupted explicitly with break or similar
   {    
      unsigned int err_row = dist_rows(generator);     // Generate the random number that defines the row
      unsigned int err_col = dist_cols(generator);     // Generate the random number that defines the col
      unsigned int err_value = dist_result(generator); // Generate the random value

      if (err_value != err_row * err_col)
         err_value--; // avoid correct value on error position - simple decrement the value

      // print_field
      for (unsigned int row = 1; row <= rows; row++)
      {
         for (unsigned int col = 1; col <= cols; col++)
         {
            if (row == err_row && col == err_col) // condition to insert the error value
               std::cout << "\t" << err_value;
            else
               std::cout << "\t" << row * col;
         }
         std::cout << std::endl;
      }

      //
      // some code to ask the user and so on.... (std::cin ...)
      //

      std::cout << "Error value '" << err_value << "' is on row " << err_row << ", col " << err_col << std::endl;

      // if (... some code to check if user wants to break...)
      break;

      // code to go on
      // go on
      std::cout << std::endl;
   }
}
int value = i * j;
if ((i == error_location.row) and (j = error_location.column)) {
    value = // code for introducing an error, e.g. sample a random answer,
            // or a random offset added to the correct answer etc.
}
std::cout << "\t" << i * j;
constexpr const int table_leg { 10 };
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> coordinate_distribution(0, table_leg - 1);
struct { int row, col; } error_location = 
    { coordinate_distribution(gen), coordinate_distribution(gen) };
#include <iostream>
#include <ctime>
#include <iomanip>

const unsigned int ROWS = 10;
const unsigned int COLUMNS = 10;

void randomValue(unsigned int &, unsigned int &, unsigned int &);

int main() 
{   
  unsigned int rValue;
  unsigned int cValue;
  unsigned int pValue;

  unsigned int rowError; 
  unsigned int columnError;
  unsigned int productError;

  unsigned int userRowGuess;
  unsigned int userColumnGuess;

  char choice;  

  do 
  { 
    randomValue(rValue, cValue, pValue); 

    std::cout << "\033[2J\033[0;0H"; // This code clears the screen 
    std::cout << std::endl << std::endl;       
    std::cout << std::setw(35) << "++++++++++++++++++++++++" << std::endl;
    std::cout << std::setw(35) << "+ Multiplication Table +" << std::endl;
    std::cout << std::setw(35) << "++++++++++++++++++++++++" << std::endl;
    std::cout << std::endl << std::endl;   

    while (true)
    { 
      rowError = rValue;
      columnError = cValue;
      productError = pValue;

      if (productError == rowError * columnError)
      {
        productError--;
      }

      for (unsigned int row = 1; row <= ROWS; row++)
      {
        for (unsigned int column = 1; column <= COLUMNS; column++)
        {
          if (row == rowError && column == columnError)
          { 
            std::cout << "\t" << "" << productError;       
          }
          else
          {
            std::cout << "\t" <<  row * column;
          }
        }
      std::cout << std::endl;
      }  
      break;
    }


    std::cout << std::endl << std::endl;
    std::cout << "\t\t" << "Type in the column number " << std::endl;
    std::cout << "\t\t" << "of the location of the    " << std::endl;
    std::cout << "\t\t" << "error & then press        " << std::endl;
    std::cout << "\t\t" << "[Enter]: ";
    std::cin >> userColumnGuess;
    std::cout << std::endl;

    std::cout << "\t\t" << "Type in the row number " << std::endl;
    std::cout << "\t\t" << "of the location of the    " << std::endl;
    std::cout << "\t\t" << "error & then press        " << std::endl;
    std::cout << "\t\t" << "[Enter]: ";
    std::cin >> userRowGuess;
    std::cout << std::endl;

    if (userRowGuess != rowError && userColumnGuess != columnError)
    {
      std::cout << "\t\t" << "Your answer was incorrect!" << std::endl << std::endl;
      std::cout << "\t\t" << "Error value '" << productError << "' is located" << std::endl;
      std::cout << "\t\t" << "on row " << rowError << ", column " << columnError << "." << std::endl;
    }
    else
    {
      std::cout << "\t\t" << "You are correct! You win!" << std::endl;
    }


    std::cout << std::endl;
    std::cout << "\t\t" << "Would you like to play again?" << std::endl << std::endl;
    std::cout << "\t\t" << "Type in 'Y' for yes or 'N'" << std::endl;
    std::cout << "\t\t" << "for no & then press [Enter]: ";
    std::cin >> choice;

    while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N')
    {
      std::cout << std::endl;
      std::cout << "\t\t" << "Invalid entry. Only 'Y' or 'N'" << std::endl;
      std::cout << "\t\t" << "are accepted answers." << std::endl << std::endl;
      std::cout << "\t\t" << "Would you like to play again?" << std::endl << std::endl;
      std::cout << "\t\t" << "Type in 'Y' for yes or 'N' for" << std::endl;
      std::cout << "\t\t" << "no & then press [Enter]: ";
      std::cin >> choice;
    }   
    std::cout << std::endl;

  } while (choice == 'y' || choice == 'Y'); 

  std::cout << "\t\t" << "Press [Enter] to continue....." << std::endl;
  std::cin.get();
  std::cin.get();

  return 0;
}

void randomValue(unsigned int &rValue, unsigned int &cValue, unsigned int &pValue)
{ 
  srand((unsigned)time(NULL));

  unsigned int r = rValue = (rand() % ROWS) + 1;
  unsigned int c = cValue = (rand() % COLUMNS) + 1;
  unsigned int p = pValue = (rand() % (ROWS * COLUMNS)) + 1;
}