Hello everyone,

I am writing this post again because there were ambiguities in the previous one. I need help with this task tomorrow. I will present what I have, what I got from you and ask which code is best to use to perform this task.

I will start with the program I wrote to fill the 6x6 array with random numbers and display it on the console. The program displays random numbers stored in an array in the range of -5 to 64 ... I believe deeply that it contains no error.

int x,y;  
    int rand_tab[6][6]; 
int main()
{

    srand(time(0)); 
    for(y=0;y<6y++)
    {

        for(x=0;x<6;x++) 
        {
            rand_tab[y][x]=(( rand() % 68 ) - 25 ); 
            cout << rand_tab[y][x] << " "; 
        }
    cout << endl;
    }

I know more or less what's going on, but I'm wondering how to modify the program to select numbers divisible by 3. How to add this code. I came up with such an idea:

    srand(time(0)); 

    for(y=0;y<6;y++) 
    {

        for(x=0;x<6;x++) 
        {
            if( (rand_tab[ y ]&& rand_tab[x]) % 3 == 0 )
             {

               rand_tab[y][x]=(( rand() % 68 ) - 25 );
            cout << rand_tab[y][x] << " ";

             }

Unfortunately, the program does not show me any values ​​in this case. Zong :-(

The next task that I need is to get started adding to the program a function that would display from the drawn numbers only those values that are not repeated from the rand () function.

I found a program on the net that does the rand () function does just that, in the range of 0-100:

#pragma hdrstop
#pragma argsused

#ifdef _WIN32
#include <tchar.h>
#else
typedef char _TCHAR;
#define _tmain main
#endif

#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <ctime>

void swap( int array[], int pos1, int pos2 ) {
    int tmp = array[ pos1 ];
    array[ pos1 ] = array[ pos2 ];
    array[ pos2 ] = tmp;
}

int _tmain( int argc, _TCHAR * argv[] ) {

    srand( time( NULL ) );

    int numbers[ 100 ];
    int matrix[ 6 ][ 6 ];

    // fill in the numbers
    for( int i = 0; i < 100; ++i ) {
        numbers[ i ] = i;
    }

    // a random number of swaps
    int repeat = rand() % 100 + 100;

    // perform the swaps...
    for( int i = 0; i < repeat; ++i ) {
        // ... with random positions
        swap( numbers, rand() % 100, rand() % 100 );
    }

    // fill in the matrix
    for( int i = 0; i < 6; ++i ) {
        for( int j = 0; j < 6; ++j ) {
            matrix[ i ][ j ] = numbers[ i * 10 + j ];
        }
    }

    std::cout << "This is the matrix:" << std::endl << std::endl;

    // print the matrix
    for( int i = 0; i < 6; ++i )

    for( i = 0; i < 10; i++ )

    {
        for( int j = 0; j < 6; ++j ) {
            std::cout << matrix[ i ][ j ] << "  ";
        }
        std::cout << std::endl;
    }
    getchar();
    system( "PAUSE" );
    return 0;
}

Now the question is, is it a better wau to modify the program I have already written, or modyfing the code mentioned above?

Thank you all for any, even the slightest help.

Thanks guys!

You're going to need an extra loop to keep trying new random numbers until you get one that is divisible by 3 and not present in the numbers you have already got...
in pseudocode...

int nextGoodRandom() {
     do
          get a random nunber
     until number %3==0 AND number is not in the existing numbers
     return number
}

not in the existing numbers can be determined by keeping a list of the numbers you have already used

commented: Thanks for your help. But since I'm a beginner I'm not sure where should I put this loop inside the code of my program... Thanks in advance. +0

is it a better wau to modify the program I have already written, or modyfing the code mentioned above?

The second code doesn't do what you need, and you will have to try to understand it before you can mod it, and it's not your code (which is what your teacher expects). So in my opinion you should ignore it and carry on with your own code.

I'm not sure where should I put this loop inside the code of my program.

It effectively replaces line 12... instead of just getting one random value you keep getting new values until you get one that meets the criteria. I showed the pseudo-code as a function, but you can just put the loop in-line.
To keep things simple, just do the mod 3 bit for now, and worry about the no repeats later.

Just had a thought...
if you get a random number and multiply it by 3 then you have a new random number that's divisible by 3.
Can it be that simple?

Or you could subtract the random number mod 3. That makes it easier to keep it within the given range.

x = new random number in range (-25,43)
x = x - x mod 3
commented: Cunning! +15
commented: That looks great! I have no idea how to use it in my code, but I will be trying everything to work it out. I'd be so grateful if you could help me:) +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.