Arrays:
Array is used to store collection of variables of same data type. It may be single dimensional type or multidimensional type.Arrays help a lot ,instead of defining variables again and again, use a single array with multiple variables you want to define.
It consists of contiguous memory locations, lowest address corresponds to first element in the array.
Single dimensional arrays
Syntax:
type arrayname[ array size] = {};Type: type can be any c++ data type
Array size: array size must be integer constant greater than zero
Array name: valid c++ identifier
Example: int x[1]={10}; ==> Note: x is an array of one integer with array size one.
Example: int x[10]; ==> Note: here x is an array of 10 integers
you can assign integers values to this example through For loop
for(int i =0 ; i< 10 ; i++)
{
x[i]= i + 2;
}
arrays integers value will b 2,3,4,5,6,7,8,9,10,11
Initialization: Arrays can be initialize in two ways weather one by one as in above example or using a single statement.
Example using single statement:
int x[5] = {1,2,3,4,5};
Example without Array size:
int x[ ] = {1,2,3,4,5}; ==> Note: it is also correct but the difference is array size is undefined
Array index:
Every element in the array has its number called index number. Lowest number 0 is called base index. It means first element in the array has its index number 0. Starts from 0 and then goes on to 1,2,3 ....
Now the question is what is advantage of index number and how can we use it. Lets take an example,
Example:
int x[5] = {10,20,30,40,50};
now let say i want to assign a value 40 in the array to a variable z of type integer , i define like this using its index number. The index numbers for above array are these
Array elements
|
Index number
|
10
|
0
|
20
|
1
|
30
|
2
|
40
|
3
|
50
|
4
|
int z = x[3];
Program code:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{ Comments
int x[5]={10,20,30,40,50}; // initialization of array
int z = x[3]; // assigning integer variable z a value of 40 from array named x[]
cout<<"value of 2nd array element = "<<x[1]<<endl; // it will display 20
cout<<"value of variable z = " <<z; // it will display 40
getch();
}
Comments is just for understanding the program.
Output for the program is
Multi Dimensional Arrays
Syntax:
type array_name [size 1] , [size 2] , ... , [size n];
Example: for three dimensional array
int threedim[5][6][2];
Two Dimensional Arrays:
Two dimensional arrays are the simplest form of multi-dimensional arrays
Syntax:
type array_name [size 1] [size 2];
Array name : valid c++ identifier
Type : valid c++ data type
[size 1][size 2] : two dimensional array size
Two dimensional arrays can be easily understand if you consider it as a table which has size 1 number of rows and size 2 number of columns.Look at this example
Example:
int x [ 3 ][ 4 ]; ==> two dimensional array with three rows and four columns
Consider this two dimensional array as table which has three rows and four columns.
Column 0
|
Column 1
|
Column 2
|
Column 3
| |
Row 0
|
x [0][0]
|
x[0][1]
|
x [0][2]
|
x [0][3]
|
Row 1
|
x [1][0]
|
x [1][1]
|
x [1][2]
|
x [1][3]
|
Row 2
|
x [2][0]
|
x [2][1]
|
x [2][2]
|
x [2][3]
|
Thus every element in the array x can be identified by form x[ i ][ j ] , where i and j are can be considered as subscripts.
Initialization:
Multidimensional arrays can be initialized by specifying each row with braces separated by comma's.
Example:
int x[3][4] = { {10,20,30,40} , {50,60,70,80} , {90,100,110,120} };
It can also be initialized like this
int x[3][4]= { 10,20,30,40,50,60,70,80,90,100,110,120 };
Accessing Elements Using indexing:
From the table as shown above you can see that how element can b accessed using subscripts i.e row index and column index.
Program code:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x[3][4]={ {10,20,30,40} , {50,60,70,80} , {90,100,110,120} }; // initializtion of 2 dim. array
cout<<"x[2][1] elementof the array is = " << x[2][1] <<endl; // it wil dislpay 100
getch();
}
Output:
Click to enlarge it.
Exercises:
1.What is the output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x[5]={10,20,30,40,50};
int z = x[1];
cout<<"value of 1st array element = "<<x[1]<<endl;
cout<<"value of variable z = " <<z;
getch();
}
2.What is the output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x[5]={10,20,30,40,50};
cout<<"value x[6]"<<x[6]<<endl;
getch();
}
3.What is the output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x[5]={10,20,30,40,50};
cout<<"value of x[0] = "<<x[0]<<endl;
getch();
}
4.What is the output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x[3][4]={ {10,20,30,40} , {50,60,70,80} , {90,100,110,120} }; // initializtion of 2 dim. array
cout<<"x[2][3] elementof the array is = "<<x[2][3]<<endl;
getch();
}
5.What is the output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x[3][4]={ {10,20,30,40} , {50,60,70,80} , {90,100,110,120} }; // initializtion of 2 dim. array
cout<<"x[0][3] elementof the array is = "<<x[0][3]<<endl;
getch();
}
6.What is the output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x[3][4]={ {10,20,30,40} , {50,60,70,80} , {90,100,110,120} }; // initializtion of 2 dim. array
cout<<"x[0][20] elementof the array is = "<<x[0][20]<<endl;
getch();
}
7.What is the output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x[3][4]={ {10,20,30,40} , {50,60,70,80} , {90,100,110,120} }; // initializtion of 2 dim. array
cout<<"x[0][30] elementof the array is = "<<x[0][30]<<endl;
getch();
}
8.What is the output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x[3][4]={ {10,20,30,40} , {50,60,70,80} , {90,100,110,120} }; // initializtion of 2 dim. array
cout<<"x[20][20] elementof the array is = "<<x[20][20]<<endl;
getch();
}
9.What is the output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x[3][4]={ {10,20,30,40} , {50,60,70,80} , {90,100,110,120} }; // initializtion of 2 dim. array
cout<<"x[50][1] element of the array is = "<<x[50][1]<<endl;
getch();
}
10.What is the output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x[3][4]={ {10,20,30,40} , {50,60,70,80} , {90,100,110,120} }; // initializtion of 2 dim. array
cout<<"x[0][0] elementof the array is = "<<x[0][0]<<endl;
getch();
}
Keep practicing on it using for loop.
ReplyDeleteVery informative article.Thank you author for posting this kind of article .
http://www.wikitechy.com/view-article/two-dimensional-array-program-in-cpp-with-example
Both are really good,
Cheers,
Venkat
Very helpful
ReplyDelete