Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Lesson 4



Multi-dimensional Arrays
Arrays can have more than 1 dimension, previously we only used a single [number] after the variable. but, this could only hold 1 word, it was ok for integers, but for 2 words it is impossible as the string is terminated by a space. A 2 dimensional array looks like this char My_array[5][10]; which states that ive got 5 words of a maximum length of 10 chars. You could also have a 3 dimensional array - char My_array[5][10][3]; but now its gets complicated and this is only used for 3D programming, and if you go to the 4th dimension, then your going to be playing with time, i think thats what physicists call the 4th dimension TIME, so im only going to talk about anything up to 2 dimensions for this tutorial. A 2 dimensional array can be thought of as a table, eg a 3 by 3 table would have 0,0 as the top left and 2,2 could be the bottom right shown below -

My_array[3][3];
0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2

Click table items above for an explanation on each.


To access a variable in the above example, you say My_array[number][number]=something. Its just the same as standard arrays, exept now weve got to think of it as a grid or table. Say you wanted to preset all the array variables to 1 and then store the integer 5 in the 2nd element of the 3rd line, you say this -

#include <conio.h>
#include <iostream.h>

void main()
{
	int cell,line,My_array[3][3];
	for(line=0;line<3;line++)			//1st loop, for each table line
	{
		for(cell=0;cell<3;cell++)		//Note i've used 2 loops
		{
			My_array[line][cell]=1;		//Preset all array variables
		}
	}
	My_array[1][2]=5;				//Set the value 5 into 1,2.
}
This in a table form would look like this -

My_array[1][2]=5;
1 1 1
1 1 5
1 1 1

Click each cell for an explanation.



Now if we want to input\output a string (name, address etc) we use a multidimensional char array, eg: char My_array[5][10]; To input\output a string, you could say cin>>My_array[0][0]; but that would only put in the first char, and we want the whole first word, and it doesnt work if we write cin>>My_array[0]; we could use a loop, but that doesnt work properly, so we must use a struct or a class.

Structures
Structures are records with fields, like name, age, birthday, Address.
It is written below the include files, which makes structures global. To access it, you need to define a variable of the type of the structure name, not int or char, but the struct name, then you use that variable to access the individual fields using a dot operator, eg: My_struct.Variable. The full example is like this:
#include <conio.h>
#include <iostream.h>

struct Struct_name
{
	int Age;
};

void main()
{
	Struct_name My_struct;		//Define an instance of "Struct_name"
	My_struct.Age=19;
	cout << "Age is: " << My_struct.Age;
	getch();
}
This is a simple use of a structure, where I made the variable Age a field within the struct. Then I defined My_struct as being an instance the structure, then i accesed the variable Age and made it 19, and output it to the screen.
You can use many variables within the structure to make them more complex, shown below -

#include <conio.h>
#include <iostream.h>
#include <string.h>

struct Struct_name
{
	int Age;
	char Name[20];
	char Address[50];
};				//Dont forget the semicolin after the bracket

void main() { Struct_name Profile[10];//Define Profile as an array of structures of "Struct_name" Profile[1].Age=24; strcpy(Profile[0].Name,"Bill"); strcpy(Profile[5].Address,"54 programming st, C++, Seattle"); cout << "Profile 0 name is: " << Profile[0].Name << endl; cout << "Profile 5 address is: " << Profile[5].Address; getch(); }
Now Profile[1] will have an age of 24, Profile 0 will have the name "Bill", and Profile 5 address is "54 programming st, C++, Seattle". Note how it is now possible to input\output strings with the use of structures. Also it is not neccesary to use an array of structures, you can just use single ones, but arrays of them seem to be more practical.

If your feeling confident, try this Quiz - Click here


Back Home Next