Thread: Easy Array Question

  1. #1
    Registered User
    Join Date
    Jun 2010

    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?

    Array Tutorial Tutorial: Arrays


  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Tomball, TX
    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.

    EnvironmentPolygon Wall[20];
    I liked this alot becuase

    Wall[0].Collide(GLfloat x, GLfloat y, GLfloat z);
    does a point to point collision based on the wall's width, height, and length.
    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.

    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 code checks for 100 collisions in 11 lines.

    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[10]. 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.

    int map[10][10];
    this would give you 100 int positions 10 rows and 10 columns.
    MAKE sure to remember map[0] is the FIRST data position not map[1]. I messed that up alot when I first started using them. Hope this helps
    Last edited by Lesshardtofind; 09-10-2010 at 10:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array question HELP!!
    By evangela in forum C Programming
    Replies: 7
    Last Post: 11-28-2009, 12:38 PM
  2. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  3. easy array of pointers question
    By The Gweech in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2003, 03:05 PM
  4. array question
    By KAchE in forum C Programming
    Replies: 4
    Last Post: 02-18-2002, 06:33 PM