Easy Array Question
I'm on the array section of the the tutorial and was playing around with them and was wondering a couple of things.
In what situations are arrays used in programming?
Are there any simple (and I mean simple) programs to make to experiment with arrays like to a multiplication table?
Cprogramming.com Tutorial: Arrays
I honestly can't think of programming without arrays. In a game map I made recently I made a class that represented any rectangular object in my 3d world. Then when I declared that class I made it an array.
I liked this alot becuase
does a point to point collision based on the wall's width, height, and length.
Wall.Collide(GLfloat x, GLfloat y, GLfloat z);
So later when I need to check for collision of bullets to walls I was able to run two loops for all the collision detection... while it isn't the most "accurate" collision detection mathmaticly it saved me ALOT of time in programming.
This code checks for 100 collisions in 11 lines.
for(int a = 0; a < 5; a++)
for(int b= 0; b < 20; b++)
if(Wall[b].Collide(Bullet[a].CenterX(), Bullet[a].CenterY(), Bullet[a].CenterZ)
This works for bullets and every object I declare an EnvironmentPolygon. So just for fun I had made a monster class that had 15 environment Polygons. And since the polygon had built in collision you could detect headshots. obviously 15 polygons makes a very basic looking person, but it was fun.
Declaring arrays cuts down on the number of variables you need to name. The bigger the program gets the harder it gets for me to track variables if I just keep naming them new things.
As for example progams you could try:
Making a program that takes 10 strings a person types.
Display back all 10 strings in the opposite order they were given.
Only use one string declared as an array.
If you want to try more of a game type experiment you could make a "Map" using an array. Maybe try an int map. Make 0 be empty and 1 be occupied. Randomly generate a position for 1 and make all the rest empty. Then let the user input a guess of where the 1 is and tell them weather they are right or wrong.
If you grasp that basic game maps can be made with a 3d array.
this would give you 100 int positions 10 rows and 10 columns.
MAKE sure to remember map is the FIRST data position not map. I messed that up alot when I first started using them. Hope this helps