Pass by Reference

Pages: 12
I find it helpful to try to write the functions needed and program flow out on paper first and figure out what info needs to be collected, where it needs to be used, etc. before starting in to actually code.
Let me ask am I passing in suitIndex correctly into SuitsInHand. That has been my biggest question today is how to pass the suitIndex correctly into suitsInHand
void suitsInHand(int suitsInMyHand[], void dealACard(int suitIndex))
{
//function definition
}

Where you're calling it though, you're not sending in suitIndex. I'd call the function from the function where you get the value for suitIndex.
Where you're calling it though, you're not sending in suitIndex. I'd call the function from the function where you get the value for suitIndex.

Can you explain this more
1
2
3
4
5
6
7
8
9
void dealAHand(char *suits[], char *faces[], int deck[][FACES], int suitsInMyHand[])    {
	int i;

	for (i = 0; i < 5; i++)	{
		dealACard(suits, faces, deck, suitsInMyHand);
		suitsInHand(suitsInMyHand, suits); //<-you are sending in suits
	}
	printf("\n");
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void dealACard(char *suits[], char *faces[], int deck[][FACES])    {
	int suitIndex, faceIndex;

	suitIndex = rand() % 4;// <- suitIndex gets its value here
	faceIndex = rand() % 13;
	while (deck[suitIndex][faceIndex] == TAKEN) {
		suitIndex = rand() % 4; 
		faceIndex = rand() % 13;
	}
	deck[suitIndex][faceIndex] = TAKEN;

	
	printf("%s of %s \n", faces[faceIndex], suits[suitIndex]);

}



If you want to call it from dealAHand, you'll need to get the value of suitIndex (and faceIndex) back to the dealAHand function from the dealACard function.
Ok I know i'm close now but the suitsInMyHand is still not counting so what am i missing

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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SUITS 4
#define FACES 13
#define AVAILABLE 0
#define TAKEN 1


void dealACard(char *suits[], char *faces[], int deck[][FACES]);
void dealAHand(char *suits[], char *faces[], int deck[][FACES]);
void suitsInHand(int suitsInMyHand[], int suitIndex);

main()  {
	char *suits[4] = { "Hearts", "Diamonds", "Spades", "Clubs" };
	char *faces[13] = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" };
	int deck[4][13] = { AVAILABLE };
	int suitsInMyHand[4] = { 0 };
	int i;

	srand(time(NULL));

	for (i = 0; i < 4; i++)
		dealAHand(suits, faces, deck);

	system("pause");
}

void dealAHand(char *suits[], char *faces[], int deck[][FACES])    {
	int i;

	for (i = 0; i < 5; i++)	{
		dealACard(suits, faces, deck);
	}
	printf("\n");
}

void dealACard(char *suits[], char *faces[], int deck[][FACES])    {
	int suitIndex, faceIndex;

	suitIndex = rand() % 4;
	faceIndex = rand() % 13;
	while (deck[suitIndex][faceIndex] == TAKEN) {
		suitIndex = rand() % 4;
		faceIndex = rand() % 13;
	}
	deck[suitIndex][faceIndex] = TAKEN;

	
	printf("%s of %s \n", faces[faceIndex], suits[suitIndex]);

}

void suitsInHand(int suitsInMyHand[], int suitIndex)	{

	suitsInMyHand[suitIndex]++;
}
I don't see where you're calling the suitsInHand function?
I just added new stuff but now i'm getting an syntax error after the debugger passes the

suitsInMyHand[suitIndex]++

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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SUITS 4
#define FACES 13
#define AVAILABLE 0
#define TAKEN 1


void dealACard(char *suits[], char *faces[], int deck[][FACES], int suitsInMyHand[]);
void dealAHand(char *suits[], char *faces[], int deck[][FACES]);
void suitsInHand(int suitsInMyHand[], int suitIndex);

main()  {
	char *suits[4] = { "Hearts", "Diamonds", "Spades", "Clubs" };
	char *faces[13] = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" };
	int deck[4][13] = { AVAILABLE };
	int suitsInMyHand[4];
	int i;

	srand(time(NULL));

	for (i = 0; i < 4; i++)
		dealAHand(suits, faces, deck, suitsInMyHand);

	system("pause");
}

void dealAHand(char *suits[], char *faces[], int deck[][FACES], int suitsInMyHand[])    {
	int i;

	for (i = 0; i < 5; i++)	{
		dealACard(suits, faces, deck, suitsInMyHand);
		suitsInHand(suitsInMyHand, suits);
	}
	printf("\n");
}

void dealACard(char *suits[], char *faces[], int deck[][FACES])    {
	int suitIndex, faceIndex;

	suitIndex = rand() % 4;
	faceIndex = rand() % 13;
	while (deck[suitIndex][faceIndex] == TAKEN) {
		suitIndex = rand() % 4;
		faceIndex = rand() % 13;
	}
	deck[suitIndex][faceIndex] = TAKEN;

	
	printf("%s of %s \n", faces[faceIndex], suits[suitIndex]);

}

void suitsInHand(int suitsInMyHand[], int suitIndex)	{

	suitsInMyHand[suitIndex]++;
}
and the debugger is only showing me one suitsInMyHand and not an array
also once the dealACard function is complete I completely lose the value of suitIndex
line 34 - you're sending in suits - that's not a integer, that's an array. The suitsInHand function is expecting an integer. If you're going to call the suitsInHand function from dealAHand, dealAHand is going to have to know the suitIndex value that gets set in dealACard.

Check your function prototypes, calls and definitions to make sure they're in agreement with the number and type of parameters.

Edit: I'm not sure what compiler you're using? It might be an older one if it lets you compile just main() and not int main().
Last edited on
Since suitsInMyHand is a counter, you'll want to initialize it to 0.
Ok so my issue now is that the suitIndex is good until it hits the suitsInMyHand function and then it just returns garbage. Also when i use the function suitsInMyHand[suitIndex}++; i get a syntax error that stops the whole program.

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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SUITS 4
#define FACES 13
#define AVAILABLE 0
#define TAKEN 1


void dealACard(char *suits[], char *faces[], int deck[][FACES], int suitsInMyHand[]);
void dealAHand(char *suits[], char *faces[], int deck[][FACES], int suitsInMyHand[]);
void suitsInHand(int suitsInMyHand[], int suitIndex);

main()  {
	char *suits[4] = { "Hearts", "Diamonds", "Spades", "Clubs" };
	char *faces[13] = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" };
	int deck[4][13] = { AVAILABLE };
	int suitsInMyHand[4] = { 0, 0, 0, 0 };
	int i;

	srand(time(NULL));

	for (i = 0; i < 4; i++)
		dealAHand(suits, faces, deck, suitsInMyHand);

	system("pause");
}

void dealAHand(char *suits[], char *faces[], int deck[][FACES], int suitsInMyHand[])    {
	int i;

	for (i = 0; i < 5; i++)	
		dealACard(suits, faces, deck, suitsInMyHand);
		
	printf("\n");
}

void dealACard(char *suits[], char *faces[], int deck[][FACES], int suitsInMyHand[])    {
	int suitIndex, faceIndex;
	int i;

	suitIndex = rand() % 4;
	faceIndex = rand() % 13;
	while (deck[suitIndex][faceIndex] == TAKEN) {
		suitIndex = rand() % 4;
		faceIndex = rand() % 13;
	}
	deck[suitIndex][faceIndex] = TAKEN;

	printf("%s of %s \n", faces[faceIndex], suits[suitIndex]);

	for (i = 0; i < 5; i++)
		suitsInHand(suitsInMyHand, suits);
}

void suitsInHand(int suitsInMyHand[], int suitIndex)	{

	suitsInMyHand[suitIndex]++;
}
Line 53 - you're sending suits still - that's an array, not the suitIndex variable. Why are you using a for loop at line 52? You've only dealt one card there?
Yeah I just took out the for loop
alright so I took out the for loop and yes finally removed the suits and put in suitIndex which makes much more sense. Last issue I'm down to is the suitsInHand function. It is not giving me an array of 4 suits and the suitIndex number is going into it but the suitsInMyHand does not change at all.
This is just an example, but it takes the suitIndex and increments the counter array. I threw in extra cout statements so I could track the incrementing of the array elements.
Then back in the main function, it ties the counter to the actual string literal for the face type.

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

void suitsInHand(int suitsInMyHand[], int suitIndex)	
{
	std::cout << "suitIndex value sent into function is " << suitIndex << std::endl;
	suitsInMyHand[suitIndex]++;
	std::cout << "suitsInMyHand[" << suitIndex << "] is now " << suitsInMyHand[suitIndex] << std::endl;
}

int main() 
{
	int suitsInMyHand[4] = {};
	std::string suits[4] = {"Hearts", "Diamonds", "Spades", "Clubs"};
	int suitIndex;
	srand(time(0));

	for (int i = 1; i <= 5; i++)
	{
		suitIndex = rand() % 4;
		suitsInHand(suitsInMyHand,suitIndex);
	}
	std::cout << std::endl;
	for (int i = 0; i < 4; i++)
	{
		std::cout << suitsInMyHand[i] << " " << suits[i] << std::endl;
	}
	return 0;
suitIndex value sent into function is 1
suitsInMyHand[1] is now 1
suitIndex value sent into function is 1
suitsInMyHand[1] is now 2
suitIndex value sent into function is 1
suitsInMyHand[1] is now 3
suitIndex value sent into function is 2
suitsInMyHand[2] is now 1
suitIndex value sent into function is 0
suitsInMyHand[0] is now 1

1 Hearts
3 Diamonds
1 Spades
0 Clubs
Well I'm not following that on correctly. The one thing I'm worried about is when I look at the array in the debugger I only get 1 array point when I should be getting 4. So I think that is half the problem
Well I'm gonna keep in touch with you on this post tomorrow cause you have been a ton of help. I got to get some sleep for my day job since I don't know enough yet for a developer job. Thank you and I will still ask questions on here till I figure this out. And then I have the whole next problem to this assignment. But till tomorrow
I just added a few lines to your function here. It looks like it's incrementing the right array element.

1
2
3
4
5
void suitsInHand(int suitsInMyHand[], int suitIndex)	{
	suitsInMyHand[suitIndex]++;
	std::cout << "incrementing array" << std::endl;
	std::cout << "suitsInMyHand[" << suitIndex << "]=" << suitsInMyHand[suitIndex] << std::endl;
}

Two of Clubs 
incrementing array
suitsInMyHand[3]=1
Eight of Clubs 
incrementing array
suitsInMyHand[3]=2
Ten of Clubs 
incrementing array
suitsInMyHand[3]=3
Eight of Hearts 
incrementing array
suitsInMyHand[0]=1
Ten of Spades 
incrementing array
suitsInMyHand[2]=1
Topic archived. No new replies allowed.
Pages: 12