Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Arrays (http://www.programmingforums.org/showthread.php?t=6456)

Gunman Oct 17th, 2005 3:46 AM

Arrays
 
Hey can someone kinda of explain to me about arrays ? I have information about arrays but can't really understand them . I would like to know some basic uses of array and how and when they are used..and about one dimensional and two dimensional arrays . Thanks =)

Polyphemus_ Oct 17th, 2005 4:12 AM

Here is an example of a one-dimensional array:
:

int array[10]; // create an array, containing 10 integers

array[0] = 8; // assign the value 8 to the first element of the array (array indexes start always at 0!)
array[1] = 3; // assign the value 3 to the second element of the array
array[5] = 263; // assign the value 263 to the sixth element of the array


and an example of a two-dimensional array:
:

int array[10][5]; // create an array, containing 10 arrays, each containing 5 integers

array[0][3] = 173; // assign the value 173 to the fourth element of the first array
array[6][0] = 3245; // assign the value 3245 to the first element of the seventh array
array[8][3] = 632; // assign the value 632 to the fourth element of the 9th array


hope this helps :)

Gunman Oct 17th, 2005 4:34 AM

Okay so what are arrays used for ? Thanks for help

Polyphemus_ Oct 17th, 2005 4:38 AM

Arrays are basically used to store a series of values of the same category. They are also often used for an amount of data you don't know the size of when compiling - e.g. information read from a file. You can use dynamic memory allocation then - asking memory at runtime. If you want, i can also give you an example of that :).

Gunman Oct 17th, 2005 4:46 AM

Okay sure ..=)

Polyphemus_ Oct 17th, 2005 5:28 AM

:

int arraysize = 20; // this is the size of the array

int *array = new int[arraysize]; // create the pointer array, and let it point to memory for an array

array[0] = 8; // you know this part already


Gunman Oct 17th, 2005 6:37 AM

Thanks think i understood it ..=)

DaWei Oct 17th, 2005 6:46 AM

"This little thingy between the quotation marks is a sequential collection of characters. It could be stored in an array."

If that were stored in MyArray, MyArray [0] would contain "T" and MyArray [12] would contain "t". If you marked the end by adding a byte with the value, 0, it would actually constitute a C string. Simple, and useful.

Gunman Oct 18th, 2005 12:00 AM

Hey ..I found this code on the web but can't understand how it works..
Thanks for help.


#include <iostream>

using namespace std;

int main()
{
int x;
int y;
int array[8][8]; // Declares an array like a chessboard

for ( x = 0; x < 8; x++ ) {
for ( y = 0; y < 8; y++ )
array[x][y] = x * y; // Set each element to a value
}
cout<<"Array Indices:\n";
for ( x = 0; x < 8;x++ ) {
for ( y = 0; y < 8; y++ )
cout<<"["<<x<<"]["<<y<<"]="<< array[x][y] <<" ";
cout<<"\n";
}
cin.get();
}

stevengs Oct 18th, 2005 3:33 AM

you refuse to use CODE tags?


All times are GMT -5. The time now is 4:15 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC