Explain Arrays Please

Pages: 123... 9
Can anyone explain to me how to construct an array and what they are. I've read my text book and I've read the forum and I still don't understand them fully. I've got a project and I'm supposed to use them to construct a simple board game, I've got all my functions laid out except the array parts, here I have hit a brick wall because I don't understand them. If you can explain please do if you know where there are some examples for derpheads like me I would be very appreciative.
Hello, I'd be glad to help.
To initialize an array you would do something like so:
int array[7] = {1,2,3,4,5,6,7}

The number in square brackets is the number of objects in the array. The number in curly brackets sets each corresponding object in the array equal to that integer.

If I do this, I have an array of 7 integers. The first object in the array would be represented by array[0]. If i were to do cout << array[0]; it would return 1.
Last edited on
arrays are actually quite simple. each language has their own way of making them, but they are all used the same way. an array is the most basic container. a container is something that holds sets of data. in c++, which is strongly typed (meaning that if you make an int variable, it cant be used as a float variable). as such, if you make an array of type char then it will only hold char values. lets look at some actual code now. first, you have to declare your array (for this mini tutorial we will use int, but you could really use it with any datatype (ie bool, float, double, char, string):
int myarray[10]
lets take a look at this: int myarray[10] this declares an array called myarray of type int. what the [10] means is that it can hold 10 values. you can now fill it with values like this:
1
2
3
4
5
myarray[0] = 1;
myarray[1] = 4;
myarray[2] = 745;
//...
myarray[9] = 74;

notice how i started at zero? that is because the first "cell" (ie myarray[0]) starts at zero. so the second cell is myarray[1]. we can also set it to the value of other variables:
1
2
int i = 10;
myarray[4] = i;


now lets say you wanted to give it a predetermined set of values:
 
int myarray[] = {10, 4, 5, 7};

this might seem strange at first. lets take a look:
each element is comma seperated. this puts each in its own cell. ie:
myarray[0] = 10
myarray[1] = 4
myarray[2] = 5
myarray[3] = 7
also notice how there is no size in the [] of myarray[] = {...}. that is because if you give it default values (ie {10, 4, 5, 7}) then its size is the number of elements in the list. in this case its 4 (because 10 is the first element, 4 is the second, 5 is the third, and 7 is the fourth). if you want you can include a size and a default value list. if the size is greater than the number of elements in the list, then the remaining free spaces are filled with zereos. ie int myarray[10] = {9, 6, 5};. the remaining 7 cells will be filled with 0's. if the size is smaller than the number of the elements in the list, then it makes the size the same as the number of elements in the default value list.
Okay so in my program i have to have an array that keeps track of how much money each player receives upon landing on a place on the board. How should an array be used in this context. If i wasn't made to do it this was i would just make a lot of if statements for each spot on the board and randomly give out money.
So i could use an array to set the values of each space on my program (boardgame) equal to some random number which in turn could be used for the amount of money gained or loss. Then link that information to another array that is associated with the players (assuming i can figure out how to keep a running total on that amount of money they have earned) and also have an array that keeps track of where they are on said board.
question is how do i relate these arrays to each other so they are aware of the information as it is updated in each?
1
2
3
4
5
6
7
8
9
10
int boardSpace [25];

boardSpace [0] = 10;
boardSpace [1] = 10;
boardSpace [2] = 20;
boardSpace [3] = -15;
boardSpace [4] = 5;
boardSpace [5] = -30;
....
boardSpace [25] = 100;


something like this?

If you had a board of say 3 x 3 like this:

1
2
3
4
5
6
7
8
9
10

	int myBoard[3][3] = {

		// board of 3x3 squares
		0, 0, 0,
		0, 0, 0,
		0, 0, 0,

	};


and a array of the same size for rewards such as:

1
2
3
4
5
6
7
8
9

	int myReward[3][3] = {

		22, 5, 11,
		14, -3, 6,
		5, -7, -12
	};




Assuming your players position is held in down and across variables you could use those variables to access the myReward array, grab its value and add it to the players funds (or deduct if its a minus)

such as

player1Funds += myReward[down][across];

So your basically using the players position that would position the player in the board to grab the reward from the other array.






closed account (iAk3T05o)
It took me more than a week to understand arrays.
Could you explain the board game. I/someone else could help you better with comments in actual code and maybe you will understand better.
Its got to be a game like candy land so its not just a chess board shape, it needs to have a flow to it, because i will be including a random dice roll that will allow the player to advance a certain number of squares. Or could i use a simple array that is 5x5 (for the 25 needed squares) and just assign values to each.
1
2
3
4
5
6
int boardSpace [] = { 
10, 5, 20, -5, 30,
-15, 15, 25, -40, 40,
100, -100, 30, 25, 10,
10, 15, -15, 30, -40,
50, 100, -90, 200, 1000 };

like this
I can link the actual project so you can see what I'm trying to do. I however don't want to tempt someone doing it for me just explaining the parts i don't understand. https://www.dropbox.com/s/jmmlg5n3z6yf5eh/PROGRAM%20TWO.docx.pdf

and right now I'm asking about the arrays and essentially how they are integrated in this program.
Last edited on

Had i did the array in my example as:

myBoard[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0};

It still allocates the same amount of memory as my:

1
2
3
4
5
6
7

myBoard[3][3] = {

   ....

};


The difference being is that you access it differently. For example,
to get to the value one in the following array you use myBoard[2][0];

1
2
3
4
5
6
7
8
9
10

int myBoard[3][3] = {

	// board of 3x3 squares
	0, 0, 0,
	0, 0, 0,
	1, 0, 0,

};


but if you do it the way you suggested...

1
2
3
4
5
6
7

int myBoard[9] = {

	0, 0, 0, 0, 0, 0, 1, 0, 0,

};


to get to the 1 value you would use myBoard[6];
Ok so i can set up multiple arrays that would keep track of that info then just use the dice roller to call the necessary space as the turns progress? but how would i go about using the dice roll result to choose "myBoard[6]" assuming i roll a 6?

You would have a variable which is currently storing your position, use rand to generate a random number and add that random number to your current player position (and ofc checking you haven't exceeded the end of the maze/line).

That new generated position you could then use to access your rewards array which stores the money gained for that position adding it to the players funds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16


	int playerPosition = 0;
	float playerFunds = 0;

	int myReward[20] = { 0, 20, 5, -3, -10, 6, 20, 5, -3, -10, 6, 20, 5, -3, -10, 6, 20, 5, -3, -10 }


	// do a rand operation, add result to playerPosition.
	
	// update player position on screen
	
	// use playerPosition variable to access the myReward array to obtain the reward and update the
	// players funds.

So this would the array thats keeping track of money
int myReward[20] = { 0, 20, 5, -3, -10, 6, 20, 5, -3, -10, 6, 20, 5, -3, -10, 6, 20, 5, -3, -10 }

And if there are multiple players would there need to be a function that generates arrays for each?

edit: Im guessing thats what "void intializeArrays(string[], int[], float[]);" this functions purpose is. "This function will use a for loop to iterate through all elements of each array setting each element of names array to an empty string, each element of boardSpace array to zero, and each element of money array to 0.0." -- Quoting the assignment.

And with these variables how are they linked to the array "myReward"
1
2
int playerPosition = 0;
	float playerFunds = 0;
Last edited on

I decided to write you a little program to hopefully help you understand a little better:

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

#include <iostream>
#include <time.h>
using namespace std;

int main()
{

	int playerPosition = 0;
	const int maxPos = 20;		// last cell of our maze
	float playerFunds = 0;


	int p1Move;

	int myReward[20] = { 0, 20, 5, 9, 4, 6, 20, 5, -3, 1, 6, 20, 5, -13, 2, 6, 20, -5, -3, -10 };

	// lets do some testing.
	srand(time(NULL));			// set the random seed.

	for (int i = 0; i < 3; i++)		// lets loop 3 times and get some moves.
	{
		// get next move
		p1Move = rand() % 6 + 1;		// number between 1 and 6
		// p1Move now has generated number
		playerPosition += p1Move;
		if (playerPosition > maxPos)
			playerPosition = maxPos;	// we are at last cell, dont go over..  so are we winner?
		else
		{
			// we have moved
			playerFunds += myReward[playerPosition];
			cout << "Player moved " << p1Move << " spaces and has " << playerFunds << " funds." << endl;
		}

	}
	return 0;
}


You would need to have two variables for player1 and player2 for their position and funds, the rewards array would stay the same for both as this is merely a lookup list of values to be added to the players money if he lands on that position.

Hopefully the above example will help you understand better.

Im guessing thats what "void intializeArrays(string[], int[], float[]);" this functions purpose is. "This function will use a for loop to iterate through all elements of each array setting each element of names array to an empty string, each element of boardSpace array to zero, and each element of money array to 0.0." -- Quoting the assignment.


I didn't read much into the pdf you sent other than looking at the snake like maze the player works his way up. However, the function you describe is merely a clean-up function that would reset any variables/arrays at the start of every game - you wouldn't want your players positions or money staying the same when its game over - on that note you wouldn't be resetting the myReward to zeros as this is a lookup for each game.


Last edited on

Just briefly looked at the pdf following my post and noticed it must support 20 players. Creating 20 separate variables for each player would be messy so the other option is use an array for that.

You could have a array for players funds and position.

1
2
3
4
5

string  playerNames[20];
int playerPos[20] = { 0, 0, 0, 0, 0, ..and so on };
float PlayerFunds[20] = { 0, 0, 0, 0, ..and so on };


player1 would be playerNames[0], playerPos[0] and playerFunds[0].
player2 would be playerNames[1], playerPos[1] and playerFunds[1].

..and so on.
Last edited on
I commented into the code what i understand is going on, am i right so far?


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
#include <iostream>
#include <time.h>
using namespace std;

int main()
{

	int playerPosition = 0;
	const int maxPos = 20;		// last cell of our maze
	float playerFunds = 0;


	int p1Move;

	int myReward[20] = { 0, 20, 5, 9, 4, 6, 20, 5, -3, 1, 6, 20, 5, -13, 2, 6, 20, -5, -3, -10 };
	// Alright so all this is ^^ is boardSpace array with listed rewards as it moves along the 25 spaces avaliable.
	// lets do some testing.

	for (int i = 0; i < 3; i++)		// lets loop 3 times and get some moves.
	{
		// get next move
		srand(time(NULL));				// set the random seed.
		p1Move = rand() % 6 + 1;		// number between 1 and 6
		// p1Move now has generated number
		playerPosition += p1Move; 
/* Player has now rolled and the "playerPosition" is updated to keep track of how far they are, 
like playerPosition = (myReward[3]) <-- or would this just be the number in the array?
 In addition the value at myReward[3] is also added to the playerFunds = makeing it 5 now. Next turn
 it would do the same to each depending on the roll and add the new totals to each of the variables.*/ 
		if (playerPosition > maxPos)
			playerPosition = maxPos;	// we are at last cell, dont go over..  so are we winner?
		else
		{
			// we have moved
			playerFunds += myReward[playerPosition];
			cout << "Player moved " << p1Move << " spaces and has " << playerFunds << " funds." << endl;
		}	// this is just output to keep users aware of their current status.

	}
	return 0;
}


playerPosition = (myReward[3]) would update the players position with the value at index 3 of myReward which is 9 - remember this is a lookup for money if you jump on that position of the maze.

so in my example i do the following:

1) create a loop of 3
2) generate a random number (like a dice)
3) add that number to the players position
5) use the new player position number to get the value from the myReward array
6) the value from the myReward array is added to players funds.


Just looking at the pdf you provided following my first posts, the function actionOnSpace(); randomly picks a number 1 or 0, if 1 then as per the instructions a further number is generated in the main() function between 1 and 100000 and added to the players funds therefore you wouldn't need a lookup table called myReward.

It mentions three arrays which are names, boardSpace, money.

names would obviously be the players names.
boardSpace would be the players position on the board.
money would be the players funds.










The function actionOnSpace() is one of the functions i don't understand at all and was going to go see a tutor about it tomorrow.

I guess i can try and break it down-
1) "randomly picks a number 1 or 0" <-- Dice roll? either positive result or not (also explains the out put of emoticons)
2) "further number is generated in the main()" <-- accounts for the addition of money if any. removes need for an array associated with boardSpace.

I guess the larger issue is the whole name array and how to switch between players and give them alternating rolls.(updating their information aswell).
Bare with me, writing you an example as we speak :)
Pages: 123... 9