hi i have a program..i can't explain this code..can you explain this??...

int   card[ROWS][COLUMNS] = {{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31, rand() % 15 + 46, rand() % 15 + 61},
{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31, rand() % 15 + 46, rand() % 15 + 61},

{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31 ,rand() % 15 + 46, rand() % 15 + 61},

{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31, rand() % 15 + 46, rand() % 15 + 61},

{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31, rand() % 15 + 46, rand() % 15 + 61}};

Edited by Dani: Formatting fixed

3
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by dylank

It is initializing each array element with random numbers between the two values you see.

Edited by Ancient Dragon: n/a

hi i have a program..i can't explain this code..can you explain this??...

int card[ROWS][COLUMNS] = {{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31, rand() % 15 + 46, rand() % 15 + 61},
{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31, rand() % 15 + 46, rand() % 15 + 61},

{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31 ,rand() % 15 + 46, rand() % 15 + 61},

{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31, rand() % 15 + 46, rand() % 15 + 61},

{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31, rand() % 15 + 46, rand() % 15 + 61}};

First off, wrap your code in CODE tags. It seems like you just pressed the code button and then pasted your code outside of it. Also, a little formating would be nice, like so:

int card[ROWS][COLUMNS] =
{
{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31, rand() % 15 + 46, rand() % 15 + 61},
{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31, rand() % 15 + 46, rand() % 15 + 61},
{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31 ,rand() % 15 + 46, rand() % 15 + 61},
{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31, rand() % 15 + 46, rand() % 15 + 61},
{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31, rand() % 15 + 46, rand() % 15 + 61}
};

This is an array. It basicly allows you to hold multiple numbers of that type of data in one variable. so, int myArrar[10]={10,2,34,23,6,3,2,3,3434,23} would hold thoes values, accessed by myArray[variable_you_want_to_access] . This is a multidimentional array, so its like you can access them like points on a 2d graph (eg myArray[x][y]).

Read up on them here: http://www.cplusplus.com/doc/tutorial/arrays/
As for the rand(), it is filling up the array with random values, i guess its trying to shuffell a deck of cards? A question like that is like asking, "i have a problem... im not gonna say anything else... but can you help me?"

A little context would be great! And read up on some basic C++ santax!

ahhh okay...thanks...

Edited by demigodz24: n/a

int card[ROWS][COLUMNS] <--- are these an int array?

>>int card[ROWS][COLUMNS] <--- are these an int array?

The type of "card" is, indeed, "int", so yes.

int card[ROWS][COLUMNS] <--- are these an int array?

The type of "card" is, indeed, "int", so yes.

please help me to fixed the numbers...the numbers is repeating..how can i make it non repeating numbers?...
and generate a numbers for bingo?...

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int ROWS = 5;
const int COLUMNS = 5;

int main()
{

cout << "  B    I       N       G       O\n\n";
srand(time(0));
int   card[ROWS][COLUMNS] = {{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31, rand() % 15 + 46, rand() % 15 + 61},
{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31, rand() % 15 + 46, rand() % 15 + 61},
{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31 ,rand() % 15 + 46, rand() % 15 + 61},
{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31, rand() % 15 + 46, rand() % 15 + 61},
{rand() % 15 + 1, rand() % 15 + 16, rand() % 15 + 31, rand() % 15 + 46, rand() % 15 + 61}};

for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
cout << card[i][j];
cout << "\t|";

}
cout <<"\n";

}
system("pause");
}

Edited by mike_2000_17: Fixed formatting

the rand() is generating just what it should be doing. Random numbers. It will spit out repeating numbers if the domain is constricted enough.

Make a loop that makes a random number, and stores it in the array, and then make another random number. Loop thru the array, making sure that it is not repeated. If it is, go thru the loop again till you make a number that does not repeat.

Perhaps this is too complex a program to start out with? Try making a prog first, that prints out a bunch of random numbers in a restricted domain, and dosent repeat any number. Thats a good starting place.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.