Love the idea of the program. Im quite interested in seeing how to get something like this to work. Is there any chance someone could add a working version to the code snippets section or possibly send me a working snippet in PM? Im trying to educate my self and self-learn c++... I dont at the moment have any books.

Thanks in advance.....
BrettW02

The best way to learn actually is to try to do it yourself.

A good reference is always a very useful help. I refer to these two often:
C/C++ Reference (short and sweet)
cplusplus.com (all the gory details)

Start small, add on details as you go, and you'll be able to do much more complicated things in short order.

Hope this helps.

Great sites... been to them more then a few time - LOL. I love doing the prgramming myself. I have an easier time learning some chunks by taking a built program and manipulating it to see what changes and so on - this way I am able to figure out what is doing what.

Thank you for the fast reply.

BrettW02

OK... at attempting this program I cant even get a two dimensional array to print a 4 x 4 grid of random numbers that are all original (I know I know, I only need 8 different numbers - but I am trying to get no duplicates first b4 I attempt to get a duplicate of only 2 of each... ) Please help... thank you

@Duoas
Maybe - U could throw me a test chunk of a snippet I could play with which could help me develop this game idea....

bw02

here is what I was playing with
<---

#include <iostream>
#include <time.h>
#include <windows.h>

#define MAXRAND 25
#define SIZE 5

using namespace std;

int main(){
    
    int row=0, rowCheck=0;
    int col=0, colCheck=0;
    
    
    int cardHidden [5][5];
    int cardShow [5][5];
    srand(time(NULL));

    //cardHidden   [5][5]= (rand() % 10) + 1;
    
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
    
    for(row=1; row<SIZE; row++)
    {          
        for(col=1; col<SIZE; col++)
        {       
            cardShow  [row][col]= (rand() % MAXRAND) + 1;
        }
         
    }
    
    
for(rowCheck=1; row<SIZE; row++)
    {          
        for(colCheck=1; col<SIZE; col++)
        {

            for(row=1; row<SIZE; row++)
            {          
                for(col=2; col<SIZE; col++)
                {   
                    if(cardShow[rowCheck][colCheck] == cardShow[row][col])
                    {
                        cardShow[row][col] = (rand() % MAXRAND) + 1;
                        rowCheck=1;
                        colCheck=2;
                        row=1;
                        col=2;
                        

                        
                    }
                }
            }
        }
    }
    
    

    
    for(row=1; row<SIZE; row++)
    {          
        for(col=1; col<SIZE; col++)
            {       
                cout << cardShow[row][col] << "\t";
                
            }
     
              
    cout << "\n\n";
                
    }
        
    
	
    system("PAUSE");

  return 0;
}

--->Thanks

Im having one heck of a time here....

Problem I am attempting to solve. I have created a 2 dimensional array - each position in the array is assigned a random number. I am trying to create a 4 x 4 grid (given the 2d array). I have set the array[5][5] for an extra row and column to print a location...

IE:

----1---2---3---4
A | X | X | X | X

B | X | X | X | X

C | X | X | X | X

D | X | X | X | X

(Where X is the random number)

I need 8 pairs of random values as well as the 8 values to be in different locations every time the program is run....

to get the random numbers in the array here is what I did...

#include <iostream>
#include <time.h>
#include <windows.h>


#define MAXRAND 25
#define SIZE 5

using namespace std;

int main(){
    
    int row=0;
    int col=0;
    

    int cardShow [5][5]; // Set of cards (4x4 grid to store unique random values)
    srand(time(NULL));

    
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); // Font color


//Set each position in the card array a random value
    for(row=1; row<SIZE; row++)
    {          
        for(col=1; col<SIZE; col++)
        {       
            cardShow  [row][col]= (rand() % MAXRAND) + 1;
        }
         
    }


//Display each value of card to screen
    for(row=1; row<SIZE; row++)
    {          
        for(col=1; col<SIZE; col++)
        {       
            cout << cardShow[row][col] << "\t";
                
        }     
              
        cout << "\n\n";                
    }

        
    cout << "\n\nProgram complete.. press any key to exit.";
    cin.get();

    return 0;
}

I have tried so many things to get the random numbers to be unique and to show in 2 random positions in the array but cant figure it out.... Please help!!!!

I know I have to take baby steps but maybe someone could help me with a "step-by-step"
mini guide to help me achieve my goal.

Thank you in advance - -

If I understand correctly, you want to check your random numbers as you are creating them to make sure that the same number was not assigned to an earlier card? If that is the case, one solution could involve setting up a boolean array of MAXRAND elements and initializing them to false. As a random number is generated, that array is checked to see whether that number has been selected before. If so, generate another random number. If not, assign it to cardShow[row][col]. After assigning it, flag that number as taken by assigning the array for that number index to be true now. If at a later time that number is generated again, when the array is checked it will show up as already having been assigned and thus discarded. Taking your code and adding code/pseudo-code,

boolean numberTakenAlready[MAXRAND];
// code to initiaize this entire array to false
    for(row=1; row<SIZE; row++)
    {          
        for(col=1; col<SIZE; col++)
        {
            bool numberPicked = false;
            int number;
            while (!numberPicked)
            {                      
                 number = rand() % MAXRAND + 1;
                 // check numberTakenAlready[number - 1]
                 // if it's false, set numberPicked to true to exit loop
            }
            cardShow[row][col] = number;
            numberTakenAlready[number - 1] = true;
        }
    }

Just commented on the other thread.
http://www.daniweb.com/forums/thread96644.html

boolean numberTakenAlready[MAXRAND];
// code to initiaize this entire array to false
    for(row=1; row<SIZE; row++)
    {          
        for(col=1; col<SIZE; col++)
        {
            bool numberPicked = false;
            int number;
            while (!numberPicked)
            {                      
                 number = rand() % MAXRAND + 1;
                 // check numberTakenAlready[number - 1]
                 // if it's false, set numberPicked to true to exit loop
            }
            cardShow[row][col] = number;
            numberTakenAlready[number - 1] = true;
        }
    }

Just commented on the other thread.
http://www.daniweb.com/forums/thread96644.html

Ok this is great... I am only unsure of one thing here --» I am a little unsure of the reason for [number - 1] -- Im sure im just overlooking something rather simple...

Member Avatar for iamthwee

Another way to look at this is to create an array of 16 consecutive numbers:

{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}

Then use a shuffle algo to mix them up. Which can be achieved by picking two numbers then swapping them. Repeat until happy.

Ok this is great... I am only unsure of one thing here --» I am a little unsure of the reason for [number - 1] -- Im sure im just overlooking something rather simple...

The 1 is subtracted because in C++ array indexes start at 0 and your loops:

for(row=1; row<SIZE; row++)
    {          
        for(col=1; col<SIZE; col++)

start at 1. Your cardShow array is not utilizing the 0 indexes of the array. The numberTakenAlready array is. You're generating random numbers from 1 to MAXRAND and the array indexes are from 0 to MAXRAND - 1. Everything's one off since array indexes always start at 0.

You could define the array to be this if you don't want to subtract the 1:

boolean numberTakenAlready[MAXRAND + 1];

rather than

boolean numberTakenAlready[MAXRAND];

That way numberTakenAlready[MAXRAND] would not be a segmentation fault and you'd no longer have to subtract one. The important thing is to simply be consistent so everything matches up. Either subtract 1 everywhere when accessing or changing it or do it nowhere.

Hello I am trying to generate two dimensional array with non-repeating random numbers but can not able to do that.
Please let me know step-by-step. IN c programming hmm not in any other languages.

commented: Just a tip. Avoid putting your new request under such an old discussion. Folk won't easily reply to you. +15
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.