Java Program to Find the Address of an Array
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.
// Function to read input and display the output
{
}
}
}
}
}
}
}
}
}
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.