0

Before you read ahead or try to help, this question is regarding my homework so the requirements to this question will be very specific.

I am writing a code that has 2 functions. The first function creates or initializes a 5*5 matrix for an array with numbers from 1 - 25 in random positions.

The second function prints it. However, I am required to use a seed value of 233 in the srand() function which I am unsure of how to use, despite constantly searching for it online. Anyway, the printout should look something like this:

--------------------------
|  4 |  5 | 10 | 21 | 22 |
--------------------------
|  1|  11 | 3  | 19 | 20 |
--------------------------
| 24 |  18 | 16 | 14 |  9|
--------------------------
| 17 |  7  | 23 | 15 |  6|
--------------------------
|  2 | 12 | 13 | 25 | 8  |
--------------------------

The first and most easily explainable issue that I have is that all my display function is doing is printing all the values in a straight line and not in the format that I want it to be.

The other part is that when I use into srand(time(233)), it gives me an error and I'm not sure why even though it is required for my assignment.

The second issue is that some of the numbers start reoccurring in the matrix and they are not supposed to, is there a way to make sure there are no duplicates in the matrix?

Although this is in the C++ language, what I have learned is the C style syntax (no std:: kinds of code or stuff like that). So far I have learned basic arrays, loops, and functions.

#include <iostream>
#include <ctime>

using namespace std;

const int ROW_SIZE = 5;
const int COLUMN_SIZE = 5;

void createBoard(int matrix[][5]);
void display(int matrix[][5]);
int main()
{

    srand(time(233)); //the seed value error

    int matrix[5][5];

    createBoard(matrix);

    display(matrix);
}
void createBoard(int matrix[][5])
{
    for (int i = 0; i < ROW_SIZE; i++)
    {
        for (int j = 0; j < COLUMN_SIZE; j++)
        {
            matrix[i][j] = 1 + rand() % 25;
        }
    }
}
void display(int matrix[][5])
{

    cout << "--------------------------" << endl;
    for (int i = 0; i < ROW_SIZE; i++)
    {
        for (int j = 0; j < COLUMN_SIZE; j++)
        {
            cout << "| " << matrix[i][j];
        }
    }
    cout << "--------------------------" << endl;
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 2
    The seed for `srand` is the argument - you want `srand(233)`. `time` is used for creating a seed that is "pseudo-unique". – molbdnilo Apr 08 '21 at 08:15
  • If you don't want the output on a single line, think about where to add line breaks in the output. – molbdnilo Apr 08 '21 at 08:21
  • For the uniqueness of the values, you could create an array of size 25 that contains your 1-25 values. And for each cell of your matrix, you pick a random value in this array (random index between 0-24). Then your remove the picked value and you continue for the next cell (this time you have to pick a random index between 0-23, and so on...) – Fareanor Apr 08 '21 at 09:10

3 Answers3

1

Assuming the function time is a requirement, it receives the address of a time_t variable so you need something like:

time_t t = 233;
srand(time(&t)); 

Though the function will just replace the value of t, so, there is that.

If not, as suggested by molbdnilo, you can use srand(233)(which is probably what is being requested), but know that this will generate the same repeated sequence.


As for the repeated values in the array, a possible strategy is to go back in the array from the generated index and as soon as you find a repetition, stop, and generate a new one, repeat until no equal number is found, though you have better methods and algorithms.

Since you are not to use std:: kinds of code or stuff , as you so eloquently put it, here is a C post that may help:

Unique random number generation in an integer array


The array print formatting issue is just a matter of adjusting and printing the lines in the correct position, to keep a consistent spacing you should use <iomanip> library, setw():

#include <iomanip>
void display(int matrix[][5])
{
    cout << " +------------------------+" << endl;
    for (int i = 0; i < ROW_SIZE; i++)
    {
        for (int j = 0; j < COLUMN_SIZE; j++)
        {
            cout << " | " << setw(2) << matrix[i][j]; // set spacing
        }
        puts(" |\n +------------------------+");
    }
}

Output:

 +------------------------+
 | 16 | 25 | 23 |  1 | 24 |
 +------------------------+
 | 11 |  4 | 23 |  7 | 22 |
 +------------------------+
 | 21 | 23 | 12 |  6 | 15 |
 +------------------------+
 | 18 | 10 |  8 | 22 | 11 |
 +------------------------+
 | 23 | 18 | 22 | 18 | 16 |
 +------------------------+

Footnote:

There are much better ways to do this, not using rand, if not for your homework, you should take a look for future memory:

https://en.cppreference.com/w/cpp/numeric/random

anastaciu
  • 23,467
  • 7
  • 28
  • 53
0

For your display function, you just have to add line endings (std::endl) at the right place:

void display(int matrix[][5]){    
    cout<<"--------------------------"<< endl;
    for(int i = 0; i < ROW_SIZE; i++){
        for(int j = 0; j < COLUMN_SIZE; j++){
            cout<<"| "<< matrix[i][j] << " ";
        }
        cout <<"|" << endl;
    }
    cout<<"--------------------------"<< endl;
}

For the creation, if you use C++, you can use shuffle: http://www.cplusplus.com/reference/algorithm/shuffle/

void createBoard(int matrix[][5]){
    // Create an array { 1, 2 ... 25}
    std::array<int,ROW_SIZE * COLUMN_SIZE> tmp;
    for (int i = 0; i < ROW_SIZE * COLUMN_SIZE; i++)
    {
         tmp[i] = i + 1;
    }

    // define your seed
    unsigned seed = 233;
    
    // shuffle your array using that seed
    shuffle (tmp.begin(), tmp.end(), std::default_random_engine(seed));

    // store the elements in your matrix
    for (int i = 0; i < ROW_SIZE; i++){
        for(int j = 0; j < COLUMN_SIZE; j++){
            matrix[i][j] =  tmp[i * COLUMN_SIZE + j];
        }
    }
}

Note that if you're using C++, you can use STL containers to store your 5x5 board (like array, vector etc...). They come with very handy features (like shuffle).

Note also that the seed is just a number to initialize your random generator. Setting it to 233, makes sure that two different executions of your program will always generate the same sequence of number (that's how you understand that in computer world, it is not really random, but pseudo-random).

Dharman
  • 30,962
  • 25
  • 85
  • 135
Pierre Baret
  • 1,773
  • 2
  • 17
  • 35
  • 1
    `std::endl` adds a line break **AND** flushes the buffer. What the OP really need is just to add a line break `'\n'`. – Fareanor Apr 08 '21 at 09:05
  • 1
    Moreover, `std::shuffle` does not need a STL container, it only needs random access iterators, which means it should work fine with C-style arrays as well. – Fareanor Apr 08 '21 at 09:06
0

You can use a Int Array with 26 element(cause its final index is 25) then set all of the element to 0 use a while loop to try to generate a X number,if it hasnt been used(Check[X] =0), let matrix[i][j] = X and let Check[X] = 1, if it has been used (Check[X]=1) then break the while loop)

And with the seed 233, I dont know why its not run but when i replace it with 'NULL', its run pretty good :D

#include <iostream>
#include <ctime>

using namespace std;
const int ROW_SIZE = 5;
const int COLUMN_SIZE = 5;
int check[26]={0};
void createBoard(int matrix[][5]);
void display(int matrix[][5]);

int main(){

    srand(time(NULL)); //the seed value error

    int matrix [5][5];

    createBoard(matrix);
    
    display(matrix);

}

void createBoard(int matrix[][5])
{
    for (int i = 0; i < ROW_SIZE; i++)
    {
        for(int j = 0; j < COLUMN_SIZE; j++)      
        {
            while (true)
            {
                //random number X;
                int x =  1 + rand() % 25;
                if(!check[x]) // If X not used;
                {
                    matrix[i][j] =  x;//add to table;
                    check[x]=1; //Mark that X used;
                    break; 
                }
            }
        }     
    }
}

void display(int matrix[][5]){

    cout<<"--------------------------"<< endl;
    for(int i = 0; i < ROW_SIZE; i++){
        for(int j = 0; j < COLUMN_SIZE; j++){
            cout<<"| "<< matrix[i][j];
        } 
    }
    cout<<"--------------------------"<< endl;
}
  • Why a size of 26 ? OP needs values between 1-25 (not 0-25) so there are only 25 values required, not 26. Anyway, how would 26 values fit in a 5x5 matrix ? – Fareanor Apr 08 '21 at 09:16
  • As you can see we need to generate a number between 1-25, when we have a X number ,we need to check if it has been used,so we need check the array with X as Index ,suppose X was 25,a array with 25 elements only have index from 0 to 24, so if you want check the 25th index you need a array with 26 elements,and not use the 0 index. (of cousre you can change Check[x] to Chack[x-1] if you want, we just need a 25 elements array). – Nguyen Ngoc Apr 08 '21 at 09:37