用c++写”随机生成一个二维数组,求每行最大值并存到一个一维数组中”

如题所述

void initarr(int * arr, unsigned int row, unsigned col)
{
  int rval;
  unsigned int rrow, rcol;
  int i, j;

  assert(arr && row>0 && col>0);
  memset(arr, row * col * sizeof(int), 0);

  randomize();
  for ( i = 0; i < row; i ++)
  {
    for (j = 0; j < col; j++)
    {
      if ( *(arr + i * col + j) == 0)
      {
        rval = random(6)+ 1;
        *(arr + i * col + j) = rval;
        findspace = 0;
        while (1)
        {
          rrow = random(row);
          rcol = random(col);
          if (*(arr + rrow  * col + rcol) == 0)
          {
            *(arr + rrow  * col + rcol) = rval;
            break;
          }
        } 
      }
    }
  }  
  return;
}

温馨提示:答案为网友推荐,仅供参考
相似回答