This is the Java Program to Find Address of an Array Element Given the Base Address.
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
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.
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.
// Java Program to Find Address of an Array Element Given the Base Address.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ArrayElementAddress {
// Function to read input and display the output
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
long size_of_element,base_address,index;
System.out.println("Enter the base address of the array(>=0)");
try{
base_address=Long.parseLong(br.readLine());
}
catch(Exception e){
System.out.println("Invalid Base Address");
return;
}
System.out.println("Enter the size of the array element in bytes(>0)");
try{
size_of_element=Long.parseLong(br.readLine());
}
catch(Exception e){
System.out.println("Invalid Size");
return;
}
System.out.println("Enter the index of the element(>=0)");
try{
index=Long.parseLong(br.readLine());
}
catch(Exception e){
System.out.println("Invalid Index");
return;
}
if( base_address < 0 || size_of_element <=0 || index < 0 ){
System.out.println("Invalid Input");
}
long element_Address;
element_Address = base_address + (size_of_element * index);
System.out.println("The address of the element at index "+ index
+" is "+element_Address);
}
}
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.
Time Complexity: O(1).
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.
- Apply for Java Internship
- Apply for Computer Science Internship
- Practice Information Technology MCQs
- Practice BCA MCQs
- Practice Programming MCQs