Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 17th, 2005, 3:46 AM   #1
Gunman
Programmer
 
Gunman's Avatar
 
Join Date: Oct 2005
Posts: 56
Rep Power: 3 Gunman is on a distinguished road
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 =)
Gunman is offline   Reply With Quote
Old Oct 17th, 2005, 4:12 AM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
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
Polyphemus_ is offline   Reply With Quote
Old Oct 17th, 2005, 4:34 AM   #3
Gunman
Programmer
 
Gunman's Avatar
 
Join Date: Oct 2005
Posts: 56
Rep Power: 3 Gunman is on a distinguished road
Okay so what are arrays used for ? Thanks for help
Gunman is offline   Reply With Quote
Old Oct 17th, 2005, 4:38 AM   #4
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
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 .
Polyphemus_ is offline   Reply With Quote
Old Oct 17th, 2005, 4:46 AM   #5
Gunman
Programmer
 
Gunman's Avatar
 
Join Date: Oct 2005
Posts: 56
Rep Power: 3 Gunman is on a distinguished road
Okay sure ..=)
Gunman is offline   Reply With Quote
Old Oct 17th, 2005, 5:28 AM   #6
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
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
Polyphemus_ is offline   Reply With Quote
Old Oct 17th, 2005, 6:37 AM   #7
Gunman
Programmer
 
Gunman's Avatar
 
Join Date: Oct 2005
Posts: 56
Rep Power: 3 Gunman is on a distinguished road
Thanks think i understood it ..=)
Gunman is offline   Reply With Quote
Old Oct 17th, 2005, 6:46 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
"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
DaWei is offline   Reply With Quote
Old Oct 18th, 2005, 12:00 AM   #9
Gunman
Programmer
 
Gunman's Avatar
 
Join Date: Oct 2005
Posts: 56
Rep Power: 3 Gunman is on a distinguished road
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();
}
Gunman is offline   Reply With Quote
Old Oct 18th, 2005, 3:33 AM   #10
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
you refuse to use CODE tags?
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:23 AM.

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