Teen Programmers Unite  


Return to forum top


Posted by MrSmiley [send private reply] at May 08, 2003, 07:44:47 PM

ok im a well newb at programming in general i started this year with qbasic (first semester) && C++ (second semester) my teacher doesnt no jack crap about what shes teaching so im probally going to teach my self c++ over the summer vacation, but while im here could i get a hand from someone on arrays (i know i know beginners stuff, well hey ive had a bout a semester of c++, and half the time im playing games because im to lazy to do the work, dont take me wrong its that my teachers just not the brightest in the bundle, god i hope she doesnt come to these pages,) so if someone could just basicly explain in simple terms and show me a code or 2 with them in it (that work, so i can see the output) i would be very grateful plus ppl who dont know much could use this formum to learn.

Posted by ItinitI [send private reply] at May 08, 2003, 08:36:23 PM

Heres a link to an ebook...it's about C, but for things like arrays it might help with concepts: http://crasseux.com/books/

Posted by CodeRed [send private reply] at May 15, 2003, 01:33:04 AM

an array is basically a grid and each cell in the grid can contain a value (or an object, or a pointer to a memory location, or whatever). In order to access a value in the array you need to reference it, in a two dimensional array you would need two values, the row and the column.

in C++ you define an array like so:

int grid[5][5];

that will make a two dimensional array with 25 spaces, 5 accross by 5 down. An important thing to remember is that when you refererance these spaces they start at 0 and go to 1 - the size, in this case 4.

Here is how you assign a value to a location:

grid[0][0] = 2;

this will assign the integer value of 2 to the location (0,0).

Here is how you retrieve a value from the array:

int num = grid[0][0];

Now the integer "num" will have the value of 2, because that was the value at (0,0) of the array. Another useful technique you should learn is how to "zero out" the array, or fill it with 0's. This is handy because in Java at least (Im not sure about C++) the array is filled with random garbage from the beginning, which could be a problem. So, here is how to clear the array:

int i,j;
for (i=0; i<5; i++)
  for (j=0; j<5; j++)
     grid[i][j] = 0;

After this code is run the entire array will be filled with 0's

Hope that answers some of your questions, there is a lot more to it than that, but that should get you started.
Posted by unknown_lamer [send private reply] at May 15, 2003, 03:29:51 PM

In C++ one should be using nested vectors...std::vector<std::vector<type> (size)> (size). They have the same syntax as arrays but you can use stuff like std::vector::at for range checked access (making sure to catch out of range exceptions).

You must be logged in to post messages and see which you have already read.

Log on
Save for later automatic logon

Register as a new user
Copyright TPU 2002. See the Credits and About TPU for more information.