Something About Arrays

Posted by Anoop Nair on September 3, 2017 Tags: Java C Data Structures Arrays

What are Arrays ?

An array is a list of finite number of n homogeneous elements. In other words we can say that it's a collection of data elements of similar kind.
The size of an array can be determined by the following formula.

Size of array "n" = Upper bound - Lower Bound + 1

Representation In Memory

A linear array is stored im successive memory cells. So the computer only needs to keep track of address of the first element i.e the base address of an array, which is denoted by base(arr) . Here arr is the name of the array. The computer calculates the address LOC of any element K of array arr by using the following formula.

LOC(arr(K)) = base(arr) + w(K - lower bound)
Here w is the number of words per memory cell.

Creating An Array

In the following section we will have a look on how we can create arrays and print it in different programming languages.

Creating An Array In C

  
    void main()
      {
          int n , i ;
          // Method One directly initializing array
          int dirArr[]={2,6,7,9,5};

          // printing initialized array
          printf("the list of values of initialized array \n");
          for(i = 0; i< sizeof(dirArr)/sizeof(dirArr[0]); i++){
              printf("%d \n", dirArr[i]);
          }


          //Method two - getting value from user
          int arr[20];  // here 20 is the maximum size of the defined array
          printf("Enter the Array size \n");
          scanf("%d", &n);

          printf("Enter the values of an integer array \n");
          for(i = 0; i< n; i++){
              scanf("%d", &arr[i]);
          }

          // printing user created array
          printf("\n the list of user created array elements \n");
          for(i=0; i< n; i++){
              printf("%d \n", arr[i]);
          }

      }

  
  

Output output

Creatind An Array In Java

    
    package basics;
    import java.io.* ;

    public class NewArray {
        public static void main(String args[]) throws IOException{

            // Method 1
            int dirArr[] ={ 2,6,7,9,5};
            int i;
            System.out.println("The values of direct initialized array are");

            for(i=0 ; i < dirArr.length; i++){
                System.out.println(dirArr[i]);
            }

           // Method 2 getting array values from user
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

            int arr[] = new int[20]; // defining a new integer array

             System.out.println("Enter the size of user defined array");
             int n = Integer.parseInt(br.readLine());
             System.out.println("Enter the values of user defined array");

             for( i = 0; i < n ; i++){
                arr[i] = Integer.parseInt(br.readLine());

              }
             System.out.println("The values of user defined array are");

             for( i=0 ; i < n; i++){
                System.out.println(arr[i]);
             }
        }

    }
    
  

Output output

Follow the following links to learn more


COMMENTS