Java Program to Find the Address of an Array

This is the Java Program to Find Address of an Array Element Given the Base Address.

Problem Description

Given the base address of an array, and the element size. Find out the address of the given element.

Example:
Base address = 2400
Element Size = 5
Element index = 6

Output:
Element address = 2430

Problem Solution

The address of an array element at index i, and the base address b, with a given element size s, is calculated using the formula

Element address = b + i*s.

advertisement
advertisement
Program/Source Code

Here is the source code of the Java Program to Find Address of an Array Element Given the Base Address. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. // Java Program to Find Address of an Array Element Given the Base Address.
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7.  
  8. public class ArrayElementAddress {
  9.     // Function to read input and display the output
  10.     public static void main(String args[])throws IOException
  11.     {
  12.         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  13.         long size_of_element,base_address,index;
  14.         System.out.println("Enter the base address of the array(>=0)");
  15.         try{
  16.             base_address=Long.parseLong(br.readLine());
  17.         }
  18.         catch(Exception e){
  19.             System.out.println("Invalid Base Address");
  20.             return;
  21.         }
  22.         System.out.println("Enter the size of the array element in bytes(>0)");
  23.         try{
  24.             size_of_element=Long.parseLong(br.readLine());
  25.         }
  26.         catch(Exception e){
  27.             System.out.println("Invalid Size");
  28.             return;
  29.         }
  30.         System.out.println("Enter the index of the element(>=0)");
  31.         try{
  32.             index=Long.parseLong(br.readLine());
  33.         }
  34.         catch(Exception e){
  35.             System.out.println("Invalid Index");
  36.             return;
  37.         }
  38.         if( base_address < 0 || size_of_element <=0 || index < 0 ){
  39.             System.out.println("Invalid Input");
  40.         }
  41.         long element_Address;
  42.         element_Address = base_address + (size_of_element * index);
  43.         System.out.println("The address of the element at index "+ index
  44.                                                  +" is "+element_Address);
  45.     }
  46. }
Program Explanation

1. In main() function the base address, size of the element and the index of an element are entered.
2. The condition if( base_address < 0 || size_of_element <=0 || index < 0 ) checks if any of the three values is invalid.
3. If any one of the input is invalid, the program terminates after displaying a suitable message.
4. Otherwise, the statement element_Address = base_address + (size_of_element * index); calculates the address of the element and it is displayed.

Note: Join free Sanfoundry classes at Telegram or Youtube

Time Complexity: O(1).

Runtime Test Cases
 
 
Case 1 (Simple Test Case):
 
Enter the base address of the array
2400
Enter the size of the array element in bytes
5
Enter the index of the element
6
The address of the element at index 6 is 2430
 
Case 2 (Simple Test Case - another example):
 
Enter the base address of the array
2800055680
Enter the size of the array element in bytes
10
Enter the index of the element
45
The address of the element at index 45 is 2800056130

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.