![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 56
Rep Power: 3
![]() |
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 =)
|
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
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 ![]() |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Oct 2005
Posts: 56
Rep Power: 3
![]() |
Okay so what are arrays used for ? Thanks for help
|
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
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
. |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Oct 2005
Posts: 56
Rep Power: 3
![]() |
Okay sure ..=)
|
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Oct 2005
Posts: 56
Rep Power: 3
![]() |
Thanks think i understood it ..=)
|
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
"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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Oct 2005
Posts: 56
Rep Power: 3
![]() |
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(); } |
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
you refuse to use CODE tags?
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|