I am approaching to create a code that generate magic square. This is what i got, i code a program that can generate 3 x 3 number from 1 to 9 consecutively and not repeating any of the number. How can i modify the program to make it possible to just randomly generates 9 numbers without repeating it?
#include <iostream>
using namespace std;
int main() {
// Declare a 10x10 array
const int nNumRows = 4;
const int nNumCols = 4;
int nProduct[nNumRows ][nNumCols ] = { 0 };
// Calculate a multiplication table
for (int nRow = 0; nRow < nNumRows; nRow++)
for (int nCol = 0; nCol < nNumCols; nCol++)
nProduct[nRow][nCol] = nRow * nCol;
// Print the table
for (int nRow = 1; nRow < nNumRows; nRow++)
{
for (int nCol = 1; nCol < nNumCols; nCol++)
cout << nProduct[nRow][nCol] << "\t";
1. Call srand once at the top of the program to seed the random number generator.
2. Call rand() to get a number in the range [0 .. RAND_MAX].
3. Check if you've not seen it before. If you have get another one; if not, store it. Do this for each slot.
4. You check if you've seen if before by comparing it with the numbers you've saved so far.