Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

Quearn

Quearn Logo Quearn Logo

Quearn Navigation

  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Quearn Academy
  • About Us
  • Blog
  • Contact Us
Home/Questions/Q 803885
Next
In Process
codemania
  • 0
codemania
Asked: May 27, 20232023-05-27T09:30:08+00:00 2023-05-27T09:30:08+00:00In: Education

Java Program to Find the Address of an Array

  • 0
Java Program to Find the Address of an Array
codejava
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    1. codemania
      2023-05-27T09:30:28+00:00Added an answer on May 27, 2023 at 9:30 am

      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.

       

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

      • 0
      • Share
        Share
        • Share onFacebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    Related Questions

    • Codility Passing Car Problem in Java
    • Dutch National Flag Problem in Java
    • Java Program to Check if an Array is Decreasing
    • Java Program to Check if an Array is Increasing
    • Smallest Sum Contiguous Subarray in Java

    Sidebar

    Ask A Question
    • Popular
    • Answers
    • imran

      Point out the wrong statement. A. The standard instances are ...

      • 272 Answers
    • imran

      Point out the correct statement. A. A volume is mounted ...

      • 250 Answers
    • VARISHA PARVEZ

      Which type of e‐commerce focuses on consumers dealing with ...

      • 181 Answers
    • Shivangi Panthi
      Shivangi Panthi added an answer B.clause June 6, 2023 at 8:47 pm
    • Shivangi Panthi
      Shivangi Panthi added an answer B.phrase June 6, 2023 at 8:46 pm
    • Shivangi Panthi
      Shivangi Panthi added an answer C.Intonation June 6, 2023 at 8:46 pm

    Related Questions

    • 30. A noun that dandies neither a male or a ...

      • 1 Answer
    • What does grade 33 cement indicate?

      • 1 Answer
    • . In Unix, which system call creates the new process? ...

      • 1 Answer
    • What are the types of distributed operating systems? a) Zone ...

      • 1 Answer
    • Network operating system runs on ___________ a) every system in ...

      • 7 Answers

    Top Members

    jafri

    jafri

    • 0 Questions
    • 0 Answers
    Namita

    Namita

    • 0 Questions
    • 0 Answers
    Sohail hossain

    Sohail hossain

    • 0 Questions
    • 0 Answers

    Trending Tags

    account algorithm analytics answer c computer computer science python cs e commerce electric electronics it language mcq mechanical physics poll programming python question

    Explore

    • Home
    • Quearn Academy
    • Add group
    • Groups page
    • Communities
    • Questions
      • New Questions
      • Trending Questions
      • Must read Questions
      • Hot Questions
    • Polls
    • Tags
    • Badges
    • Users
    • Help

    Footer

    Quearn

    Quearn is a social questions & Answers Engine which will help you establish your community and connect with other people, Also you Can Earn Money.

    About Us

    • Meet The Team
    • Blog
    • About Us
    • Contact Us

    Legal Stuff

    • Terms of Use
    • Privacy Policy
    • Cookie Policy

    Help

    • Knowledge Base
    • Support

    Follow

    © 2018-2023 Quearn®. All Rights Reserved | Developed by ASHAS Industries Proudly 🇮🇳