How to remove a value from an array

Write your question here.

I am making a simulation of Go Fish! game. In this program, I use only the 2's through 9's (using only a 32-card deck instead of all 52) for a faster game.

I am currently working on my stealCards function. This function's job is to transfer a card/cards from one hand to another.

My program is incomplete (I'm still working on it), but for the general context, here it is.

Edit: // how do you remove a value from "From"


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  #include <iostream>
using namespace std;

void shuffleCards (int[], int& position);
void drawCards (int ocean[], int& oceanPos, int hand[], int& handSize, int &drawn );
void stealCards(int, int& [], int&, int& [], int&);

int main()

{
	char yesNo = 'y';
	while (yesNo == 'y') {
		int ocean [32];
		for (int i = 0; i < 32; i++)     // initialize the deck
		{
			ocean [i] = 2 + rand() % 9;
		}
		// Declare variables
		int oceanPos;
		int playerHand [32];
		int computerHand[32];
		int playerSize = 0;
		int computerSize = 0;
		int drawn;

		shuffleCards (ocean, oceanPos);     //shuffles the card
		
		for (int i = 0; i < 5; i++)
		{
			drawCards(ocean, oceanPos, playerHand, playerSize, drawn);
		}
		for (int i = 0; i < 5; i++)
		{
			drawCards(ocean, oceanPos, computerHand, computerSize, drawn);
		}
		
		cout << "Would you like to play again?";
		cin >> yesNo;
	}
	return 0;
}

void shuffleCards (int ocean[], int& position)
{
	int i;
	int j;
	int temp; // temporary storage
	for (i = 0; i < 32; i++) {
		j = 1 + rand() % 32;
		temp = ocean[i]; // store in temp
		ocean [i] = ocean[j]; // j fills the empty i
		ocean [j] = temp; // temp(or i) goes to empty j.
	}
	position = 0;
}


void drawCards ( int ocean[], int& oceanPos, int hand[], int& handSize, int &drawn )
{
	drawn = ocean[oceanPos]; 
	handSize++;   // increase the player's cards count
	hand[handSize] = drawn; // make a room for the new card
	oceanPos++; 
}

int countRank ( int rank, int hand[], int handSize ) 
{
	int i;
	int count = 0;
	for ( i = 0; i < handSize; i++ )
	{
		if ( hand[i] == rank )
		{
			count++;
		}
	}

	return count;
}

void stealCards (int rank, int& from[], int& fromSize, int& to[], int& toSize )
{
	for (int i = 0; i < fromSize; i++ )
	{
		if (from[i] == rank)
		{
			
			toSize++;
			to[toSize] = rank;
			fromSize--;
			// how do you remove an array from fromSize?
		}
	}
}
Last edited on
// how do you remove an array from fromSize?

What do you mean? fromSize isn't a container. It doesn't have any arrays in it, so how can you remove an array from it?
I meant a value from an array. As you can see, "From" is discarding a card and giving it to "To" How can I remove that card from "From"?
Last edited on
It depends on whether or not you want to preserve the existing order of the elements in the array. If maintaining the order is important, you need to move each element after the one to be removed down by one position. Or if order is unimportant, simply move the last element into the position of the removed element. In either case, you then decrement the value of the array size by 1.
I don't think the order is important because you are just giving it to the other player. Also, I don't think you are removing the last element of my From array because it is asking if you have this card, then you must give it to the other player.
Well, I couldn't tell whether order would be important or not, as it isn't shown where in the code stealCards() will be called.
It will be called in the main function (every function will be called in the main function). The order does not matter in stealCards because it is only transferring a card from one hand to another. For example, if I have 5, 2, 7, 4, 8, and you have 2, 2, 2, 3, 1, you will ask me if I have 2 (because that's what you need to complete a set). I have to give 2 to you so my array size will decrease and I have to remove 2 from my card because I just gave it to you.

Therefore, my array Size will decrease by 1. My hand will be now 5, 7, 4, 8.

What I am asking is how can I remove that 2 card.
Last edited on
I already described how to remove the card:

Simply move the last element into the position of the removed element. Then decrement the value of the array size by 1.

Thus
size =5
5, 2, 7, 4, 8
becomes
size = 4
5, 8, 7, 4

(though if you want the result to be 5, 7, 4, 8, then you have to move each element after the removed element into the next lower slot - requires a loop. Not a big deal for a small array, but for longer arrays it can be a considerable overhead because removing a single element requires many other elements to be moved too).
Last edited on
Topic archived. No new replies allowed.