mercredi 16 août 2017

What does " rand()%3-1 " mean in c++?

The program creates a two-dimensional array (named "table" and fills the array with random numbers (-1, 0 or 1).

   int main() {
   srand(time(0));  
   const int ROWS=3;
   const int COLS=4;

   int table[ROWS][COLS];

for (int i = 0; i < ROWS; i ++) {
    for (int j = 0; j < COLS; j++)   {               
        table[i][j] = rand()%3-1;                
    }
}

for (int i = 0; i < ROWS; i ++)  {
    for (int j = 0; j < COLS; j++)
        cout << setw(3) << table[i][j];

    cout << endl;
 }

  return 0;
  }




Aucun commentaire:

Enregistrer un commentaire