472,993 Members | 3,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 software developers and data experts.

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 1101
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
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.