### Passing arrays to and from functions

Okay, so I'm in my first semester of programming. I am creating a "Go Fish!" card game program. Everything that I want to do makes perfect sense in my head, but I don't understand how to put it into code.

Essentially, I want one function, which I named "randomize," to deal a hand of 5 cards to the player and computer, as well as randomizing a large series of values in the "ocean" for use in the card game. I'm almost positive I successfully did this within the scope of this function, but how do I get my work out of the function?

Say that I created three arrays within randomize, named pHand[], cHand[], and ocean[]. I want to compare values of these arrays in a separate function.

I was gonna pass by reference but that doesn't seem possible? Also, I know you guys probably hate big walls of code with people asking you to fix their errors, so I've omitted that unless it's requested. Last thing to mention, we haven't touched upon vectors, or anything very advanced at all in this course, and I must stay inside the confines of what we learned in class.

Thank you very much!

 ``12345678910111213141516171819202122`` ``````#include void compare(int a[], int b[], int c[]) { if(a[0] == b[0] && b[0] == c[0]) std::cout << "!!!" << std::endl; } void change(int a[], int b[], int c[]) { a[0] += b[0] -= c[0] += 9; //Do not do this again ever } int main() { int x[3] = {1, 2, 3}; int y[3] = {1, 3, 5}; int z[2] = {1, 9001}; compare(x, y, z); change(z, y, x); std::cout << x[0] << ' ' << y[0] << ' ' << z[0] << std::endl; }``````

Arrays are threated as pointers when you pass them into functions, so if you pass an array into fuction you will deal with actual array, not a copy.
Last edited on
Create the arrays in `main()' and pass them to the `randomize()' function.

> I must stay inside the confines of what we learned in class.
That's stupid.
Thank you. One last thing... the user inputs a value and it checks to see if the computer has any. I've successfully done this part, but I'm unsure how I should remove the cards from the computer's hand array to the player's hand array? I've added the function code for some visibility as to what's going through my head. Also, it's a 32 card deck with only values 2-9.

 ``1234567891011121314151617181920`` ``````void compare(int pAsk, int cHand[], int pHand[]) { //pAsk is the user's inputted card value, cHand and pHand are the players' arrays //samecount is to count how many times he has the same value int samecount = 0; for (int i = 0; i < 32; i++) { if (cHand[i] == pAsk) { //Add something here to change values of the hands? samecount++; } } if (samecount > 0) cout << "Yes, I have " << samecount << " " << pAsk << "s." << endl; else cout << "Nope, I have none. Go fish!\n"; }``````
Last edited on
Zero it out? `cHand[i] = 0;`
You also should make sure that there is no random number when you deal hand in the beginning. Best way is to initialize all values in array with 0 (or other number if zero is used for card enumeration)

 I must stay inside the confines of what we learned in class.
Vector is asking to be used here.
Really to force students to not use standard library when it would be the right place to use it, is like breaking them legs and then force them to dance. And I thought where people who likes to reinvent the wheel comes from.
 I must stay inside the confines of what we learned in class.

^^ this is not a bad thing. it simply saying that they need to utilize what they learned so far. only then they can really learn to appreciate the codes or hard work it takes to make things "convenient" for their use.
Topic archived. No new replies allowed.