what is an array in programming?

 What is an array?

Array is an collection of similar type of data referenced under the same name .It is a non primitive (size is not fixed)data type . In an array it collects only same type of data .Array is an linear type of data structure. Elements are arranged in a sequence. If the array is integer then all element will be of integer type .If an array is char data type then all element will be char type.  Each element can be accessed directly  by only using its index. 

What data type should all the elements in an array be?
The data type should be same for all the elements in an array. example : int ,float ,string ,char etc. for example the data type of an Array is float then it can only store the float values.

There are two types of an Array

  • 1-D Array
  • 2-D Array

  • 1-D Array

    Declaration of an 1-D Array

    Syntax  : 

    Data_type array_name [size]; //C++

    example :int a[7];

    Data_type array_name[]=new data_type[size]; //JAVA

    For example, int a[]=new int[5]; or int []a=new int[5]; in this example int is a data type of an array means only integer type of element can be stored in an array having name as a. the size of array is 5 means it contains 5 element . and indexing of element starts from 0 and the last index of an array is 4 . we represent each index as a[0],a[1],a[2],a[3],a[4],a[5]. if the size of an array is n then the last index will be array_name[n-1].

    How to store the data. 

    example :

     int []marks=int[3];  // we can also declare as : int []marks={98,89,88};

    marks[0]=98;

    marks[1]=89;

    marks[2]=88; 

    System.out.println(marks[2]); // the output will be 88. 

    If we not store the value in any of one index it will give a null or 0 value.

    WAP (write a program) to input 5 element in 1-D array.

    By using C++ programming language

    #include<iostream.h>

    #include<conio.h>

    void main(){

    clrscr(); // for clearing the screen .

    int a[5] ,i;

    cout<<"enter 5 elements";  // 23 34 45 56 67 

    for(i=0;i<5;i++){

    cin>>a[i]; }

    cout<<"array elements are"<<endl ;  // endl for next line.

    for(i=0;i<5;i++){

    cout<<"element"<<i+1<<"="<<a[i] <<endl;

    getch();

    }

    OUTPUT:

    enter 5 elements

    23 34 45 56 67

    array elements are

    element 1=23

    element 2=34

    element 3=45

    element 4=56

    element 5= 67

    2-D Array 

    How to declare an 2-D Array

    Syntax : 

    Data type array_name[size][size]; // C++

    Data_type [ ] [ ] array_name = new Data_type[no of row ] [no of column]; //JAVA

    How to store the data

    for example :
     int  [ ] [ ] marks=new int [3][3];  // or int [ ] [ ] marks={88,76,78,88,98,79}
    marks [0][0]=88;
    marks[0][1]=76;
    marks[0][2]=78;
    marks[1][0]=88; 
    marks[1][1]=98;
    marks[1][2]=79;
    marks[2][0]=75;
    marks[2][1]=85;
    marks[2][2]=95;
     
    System.out.println("marks="+marks[1][1]); // marks=98 

     WAP  to input 9 elements in 2-D array.

    By using C++ programming  language

    #include<iostream.h>
    #include<conio.h>
    void main(){
    clrscr();
    int a[3][3],i,j;
    cout<<"enter 9 elements "; // 1 2 3 4 5 6 7 8 9
    for(i=0;i<3;i++){
    for(j=0;j<3;j++){
    cin>>a[i][j];
    }
    }
    cout<<"the array elements are"<<endl;
    for(i=0;i<3;i++){
    for(j=0;j<3;j++){
    cout<<"element ["<<i<<"]["<<j<<"]="<<a[i][j];
    }
    }
    getch();
    }
    OUTPUT :
    enter 9 elements 
     1 2 3 
     4 5 6
     7 8 9
    the array elements are

    element[0][0]=1
    element[0][1]=2
    element[0][2]=3
    element[1][0]=4
    element[1][1]=5
    element[1][2]=6
    element[2][0]=7
    element[2][1]=8
    element[2][2]=9

    Functions of an Array:

    There are two function of an array which we perform for determining the size of an array and  find the sorted array .
    • Sorting ( sort() )
    • length ( length() )
    Sorting  : For sorting an array in ascending order we use sort function. 
    example :  Arrays.sort(marks); //76 78 79 88 88 98

    length : to find the length of an array we use length() .
    example : System.out.println(marks.length()); //3


    Post a Comment

    have you any doubt then ask.

    Previous Post Next Post