Arrays

I just finished a course in Python which I used lists for everything. So I couldn’t wait to learn arrays.

But C++ does not make it easy… where’s my .sort function?

An Array is a list of elements, each with it’s own index; kind of like a set of shelves.

Here’s a simple example:


#include <iostream>

 using namespace std;
 int main()
{
 //DECLARED A NEW ARRAY, TYPE INT, SIZE 8.
 int array[8];

 //ASSIGNING VALUES 0-7 TO MY EIGHT ELEMENTS
 for(int x=0; x<8; X++)
 cin>> ;

 //OUTPUT ARRAY TO SCREEN ONE ELEMENT AT A TIME
 for(int x = 0; x<8; x++)
 cout<<array[x];

 return 1;
}

Note: I have only used type int here, and I can use other numerical types easily. I will write another post on using characters to make words. For now, I don’t need them.

Note II: Next post, how to sort!

Note III: Just for fun, here’s a 2D array.


//DECLARING A 2D ARRAY

int twodimensionalarray[8][8];

//HOW TO ACCESS EACH ELEMENT

twodimensionalarray[arrayindexnumber1][arrayindexnumber2] = someInt;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s