Thursday, April 22, 2010

Arrays - that only moves between Rows and Columns

We already know that a variable cannot store more than one data at a time. Whenever we store a data in a variable it overwrite the old one. So if we want to store a same type of data and more than one we have no choice that we have to declare multiple variable. It is possible for small amount of data but not more, so here we need an array. Array is a variable with capability to store multiple data with an unique location can be compared with a cupboard with many racks.

What are Arrays?

To store similar types of data under a common variable name, instead of declaring N number of variables for the N number of data, we use an array. An array is a collection of similar type of variables under a common variable. Thus we can have a collection or Array of integers, floats, characters (which are known as strings), but remember that all elements of any given array must be of the same type, i.e. we cannot have an array of numbers some of which are int and some float etc. Either all should be int or all should be float or all should be char. The different components of an array are called the Array Elements. Arrays also known as subscripted variable

How to Declare an Array:

Like all other variables, an array needs to be declared first before it can be used. The declaration consists of stating the type of data-type used in the array, the name of the array and the number of elements in the array.

Data Type Array_Variable[ number of elements ] ;

Example :

(C or C++) : int age[20]; float salary[20]; char name[20];
In C/C++, char array indicate a String.
Java : int a[]=new int[20]; String name[]=new String[20];
Basic : dim n(10), a$(10)

Suppose we declare an array if integer with 5 elements, N[5] then

N[0]
N[1]
N[2]
N[3]
N[4]
Here N is variable and [0] to [4] is subscript or element location. When we input data in N[0] to N[4] location we can recall back in any point in that particular program. Above array also known as single dimensional array. We can initialize the array with a list of values.

Example :

(C or C++) : int age[4]={23,45,67,89};
char name[20]={"Anil","Sunil"};
Java : int a[]={1,5,6,7};
String name[]={1,5,6,7};

Initialization can be done when we use a fixed numbers of known data but there is no shortcut method to initialize large number of elements. Enter in data in an Array and printing back. Some example of Input and display the array elements.

C C++


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

void main()
{
int N[5],i;
printf("\nEnter 5 Nos "); // cout<<"Enter 5 Nos. ";
for(i=0;i<5;i++)
{
scanf("%d",&N[i]); // cin>>N[i];
}
printf("\nNos in Array"); // cout<<"Nos in Array ";
for(i=0;i<5;i++)
{
printf("%d ",N[i]); // cout<<N[i];
}
getch();
}

Java


import java.io.*;
class Arrays
{
public static void main(String args[])throws IOException
{
BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
int N[]=new int[5], i;
System.out.print("Enter 5 Numbers : ");
for(i=0;i<5;i++)
{
N[i]=Integer.parseInt(Br.readLine());
}
System.out.print("\nNumbers in Array : ");
for(i=0;i<5;i++)
{
System.out.print(N[i]+" ");
}
}
}

Multi dimensional Arrays:

Where Single Dimensional Arrays has one column and multiple rows, multi-dimensional arrays has multiple rows as well as multiple columns. Milti-dimensional data structure also known as matrices. A multi-dimensional array with 3 rows and 3 columns is shown. There are two array subscripts. One subscript denotes the row & the other the column.

N[0][0]N[0][1]N[0][2]
N[1][0]N[1][1]N[1][2]
N[2][0]N[2][1]N[2][2]
data_type array_variable[row_size][column_size];
The declaration of two dimension arrays is as follows:
(C/C++) : int N[4][5] ;
Java : int N[][]=new int[4][5];

Here m is declared as a matrix having 4 rows( subscript from 0 to 3) and 5 columns subscript 0 through 4). The first element of the matrix is N[0][0] and the last row last column is N[3]4]. When a matrix declared with same numbers of rows and column, is known as square matrix. The initialization is done row by row. Initializes the elements (C/C++)

int N[2][3]={5,1,2,3,4,5};
int N[][]={{2,3,4},{4,5,6}};

Some example of Input and display the multi-dimensional array elements.

C C++


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

void main()
{
int N[3][4],i,j;
printf("\nEnter 12 Nos "); // cout<<"Enter 12 Nos. ";
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
scanf("%d",&N[i][j]); // cin>>N[i][j];
}
}
printf("\nNos in Array"); // cout<<"Nos in Array ";
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%d ",N[i][j]); // cout<<N[i][j];
}
}
getch();
}

Java


import java.io.*;
class Arrays
{
public static void main(String args[])throws IOException
{
BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
int N[][]=new int[3][4], i,j;
System.out.print("Enter 5 Numbers : ");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
N[i][j]=Integer.parseInt(Br.readLine());
}
}
System.out.print("\nNumbers in Array : ");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
System.out.print(N[i][j]+" ");
}
}
}
}


!!!Its too hot around here, take you all the way to Canada and for time being
say you Matcaci!!!