473,473 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

about array

Hello!

Below is an example of an easy way to shuffle a Deck I must admit it's not
the most efficient way of doing it but the array is small so the time it's
take is hardy noticed at all.
But now to my question:
At the bottom of method Shuffle I have this statement:
"newDeck.CopyTo(cards, 0);"
which copy each Deck back into the cards array
I just wonder if I instead do this following
cards = newDeck; the result would be the same.

First cards is pointing to the array cards where each index in this array
cards is pointing to a Card object.
The same is true for the array newDeck as for cards.
So if I do cards = newDeck it would mean that cards is new pointing to the
newDeck array with all the random Card objects.
So as a summary it would be just the same as doing "newDeck.CopyTo(cards,
0);"
I just want to check with you if you agree with me.
So is it any point of doing this "newDeck.CopyTo(cards, 0);" insted of this
"cards = newDeck;"
It would be much faster to do this "cards = newDeck;"
insted of this
"newDeck.CopyTo(cards, 0);"

public void Shuffle()
{
Card[] newDeck = new Card[52];
bool[] assigned = new bool[52];
Random rand = new Random();

for (int i = 0; i < 52; i++)
{
int destCard = 0;
bool foundCard = false;

while (foundCard == false)
{
destCard = rand.Next(52);
if (assigned[destCard] == false)
foundCard = true;
}
assigned[destCard] = true;
newDeck[destCard] = cards[i];
}
newDeck.CopyTo(cards, 0);
}

//Tony
Jun 27 '08 #1
1 1112
Tony Johansson wrote:
Hello!

Below is an example of an easy way to shuffle a Deck I must admit it's not
the most efficient way of doing it but the array is small so the time it's
take is hardy noticed at all.
But now to my question:
At the bottom of method Shuffle I have this statement:
"newDeck.CopyTo(cards, 0);"
which copy each Deck back into the cards array
I just wonder if I instead do this following
cards = newDeck; the result would be the same.

First cards is pointing to the array cards where each index in this array
cards is pointing to a Card object.
The same is true for the array newDeck as for cards.
So if I do cards = newDeck it would mean that cards is new pointing to the
newDeck array with all the random Card objects.
So as a summary it would be just the same as doing "newDeck.CopyTo(cards,
0);"
I just want to check with you if you agree with me.
So is it any point of doing this "newDeck.CopyTo(cards, 0);" insted of this
"cards = newDeck;"
It would be much faster to do this "cards = newDeck;"
insted of this
"newDeck.CopyTo(cards, 0);"

public void Shuffle()
{
Card[] newDeck = new Card[52];
bool[] assigned = new bool[52];
Random rand = new Random();

for (int i = 0; i < 52; i++)
{
int destCard = 0;
bool foundCard = false;

while (foundCard == false)
{
destCard = rand.Next(52);
if (assigned[destCard] == false)
foundCard = true;
}
assigned[destCard] = true;
newDeck[destCard] = cards[i];
}
newDeck.CopyTo(cards, 0);
}

//Tony
Yes, you can safely replace the reference of the array instead of
copying the items to the array, provided of course that the reference is
the only reference to the array that you use.

public void Shuffle() {
Card[] newDeck = new Card[52];
bool[] assigned = new bool[52];
Random rand = new Random();

for (int i = 0; i < 52; i++) {
int destCard;
do {
destCard = rand.Next(52);
} while(assigned[destCard]);
assigned[destCard] = true;
newDeck[destCard] = cards[i];
}
cards = newDeck;
}
--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: J. Campbell | last post by:
I posted a question some time back about accessing a char array as an array of words. In order not to overrun the char array, I padded it with enough 0x00 bytes to ensure that when accessed as...
3
by: Joe C | last post by:
I have some code that performs bitwise operations on files. I'm trying to make the code portable on different endian systems. This is not work/school related...just trying to learn/understand. ...
5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
3
by: J Wang | last post by:
Dear, could you tell me about the usage of "##" in preprossor give me some simple examples. thanks. I just got the example from "dissection C" as follows:
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
13
by: agentxx04 | last post by:
Hi. Our assignment was to creat a program that can find the average, median & mode of a #of integers. Here's my program: #include<stdio.h> int main() { int item; int a, b, t, mode; int...
15
by: copx | last post by:
Q1: If an array is declared static in file A is it still valid to access it from file B? I mean if a function form file A which returns a pointer to a position inside of the array is called from...
14
by: ablock | last post by:
I have an array to which i have a added a method called contains. I would like to transverse this array using for...in...I understand fully that for...in is really meant for Objects and not Arrays,...
17
by: DiAvOl | last post by:
Hello everyone, merry christmas! I have some questions about the following program: arrtest.c ------------ #include <stdio.h> int main(int agc, char *argv) {
15
by: SM | last post by:
Hello, I have another simple question about an array in PHP and a variable in PHP. This is the array: $thumbs_cat_1 = array( 'wine', 'cheese', 'ice',
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.