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

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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

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

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3987812
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:10:42+00:00 2026-05-20T06:10:42+00:00

Would be glad if someone can help me with the following problem. The following

  • 0

Would be glad if someone can help me with the following problem. The following method is suppose to returns an array whose ith entry is the number of times the int i appeared in array a. The method name gives the use of this method. Drawing a histogram of data found in array a.

    public static int histogram(int M, int[] a){

        int[] b = new int[M];

        for (int i = 0; i < M; i++){

           int w = 0; 

            for (int j = 0; j < a.length; j++){ 

            if (a[j] == i){ 
            w++;
            }
        }

           b[i] = w; 

     }

   return b; 
}

Accoring to me the code is correct – it may be wrong – I can’t test it because I’ve got an error thats really bugging me.

Histogram.java:22: incompatible types
found : int[]
required: int
return b;

1) What does above mean?? On example on the internet and in my handbook they also only use “return b;” to return an array to the main program.

2) How exactly does the return function work? Will I be able to use the following code to print the values of array b? Because at the moment I get an error as well for array b not being initialised in main… I think the error will go away if error 1 is sorted out. I’m new to JAVA so I won’t really know – new to programming too.

for (int x = 0; x < M; x++){

        System.out.printf("%d ", b[x]);

     }`

3) Should I maybe print the values in the histogram() method? The question to answer is though to “return” the array with the histogram values in it.

Would really be glad if you can help…

If you’ve come this far – thanks a lot… Even just reading till here means a lot 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:10:42+00:00Added an answer on May 20, 2026 at 6:10 am

    You should try returning array of ints instead of a single integer

    public static int[] histogram ...
    

    I’m not sure about 2&3, though.

    2) return is not a function, it’s a keyword causing you to leave the method.
    3) You can, if you want to.

    edit
    Whatever variables you declared in histogram method stay there, they’re local. If you want some variable in the main method, declare it. E.g.,

    int[] b = histogram(param1, param2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can not solve this problem for a while. I would be glad for
I am facing problem and would be glad if you could help . So,I
I would be very glad if someone can make clear for me example mentioned
Glad if someone can clarify. I always read that SQL and SPARQL are very
I'm using bash shell. Hi,ppl Would be glad if someone could provide some kind
I'm looking to learn some theory about data/audio/video compression, and would be glad to
Would the following SQL remove also the index - or does it have to
Would it suppose any difference regarding overhead to write an import loading all the
I'm trying to cope with some problem in finding good idea. I would like
Can someone explain to me why the form validation fails in chrome ? Pressing

Explore

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

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.