Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-09-2008, 03:56 AM
Member
 
Join Date: Aug 2008
Posts: 22
carlodelmundo is on a distinguished road
Finding the Mode in An Array
Hi. My instructor gave us an assignment to calculate the modes in an array. Basically, the array values (not the index) contains the frequencies of the index number (like a histogram). For example, since the value of "10" (the highest number for values) occurs in index numbers 2, 4, and 11, the program must output that the "mode occurs at index numbers 2, 4, and 11." Here's what I have so far:

Code:
public class Stats { public static void main(String[]args) { int[] array = new int [16]; array[0] = 0; array[1] = 0; array[2] = 10; array[3] = 5; array[4] = 10; array[5] = 0; array[6] = 7; array[7] = 1; array[8] = 0; array[9] = 6; array[10] = 0; array[11] = 10; array[12] = 3; array[13] = 0; array[14] = 0; array[15] = 1; System.out.println("The maximum is " + CalculateMaximum(array) + "."); } public static int CalculateMaximum(int[] array) { int maximum = array[0]; for (int i = 1; i < array.length; i++) { if ( array[i] > maximum) { maximum = array[i]; } } return maximum; } }
So far, I've managed to calculate the maximum (the program outputs 10). My problem is, I can't think of an algorithm that will return the index numbers if the array value is its maximum. That is... what can I do so that whenever the program sees that the maximum number (the mode) is 10, that it returns the index value of the array?

That was confusing. Let me know if you need clarification. Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-09-2008, 04:05 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 876
Fubarable is on a distinguished road
Hint: you could use a second array whose indices are the range of numbers possible in your array, initialize each value to 0,... I'll leave the rest to you.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-09-2008, 04:20 AM
Member
 
Join Date: Aug 2008
Posts: 22
carlodelmundo is on a distinguished road
Fubarable,

Thanks for that response. Someone told me to use a second array, and you're recommending it too! I'm still a novice... the problem still serves ambiguity. What can we achieve by creating a second array whose indices are the ranges 0-10, and initializing each array value to 0?

Are we trying to create a search algorithm, and it returns the modal indices for the original array?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-09-2008, 04:23 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 876
Fubarable is on a distinguished road
To find the algorithm, walk away from the computer, pull out a few sheets of paper, write down 16 numbers ranging from 0-10 on this paper, and solve the problem on paper only. Then do it again. Once you are familiar with the steps necessary to solve this on paper, it's usually fairly easy to translate this into Java code.

Good luck.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-09-2008, 05:53 AM
Member
 
Join Date: Apr 2008
Posts: 20
pheonix is on a distinguished road
Usually when I want to solve a maximum, minimum programing thing I use Math.max(the first number, the second number);
all I have to do is just figure out a way to use it with arrays.
In your case I would put it inside a loop . using the same array (no need for another). I would need 2 loops instead. so that the first number would stay still while the second number keeps increasing. one way or another it would get the maximum. Now I still didn't try it myself and I'm just giving you an idea. If by any chance you wrote this thread because you were looking for someone to actually write the code and show it to u. I would be happy to show you my way, but if you want to do it on your own, thats fine too
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 09-09-2008, 06:01 AM
Member
 
Join Date: Aug 2008
Posts: 22
carlodelmundo is on a distinguished road
Unfortunately, my perseverance and pride forces me to solve the problem on my own with only the suggestions of others as helping tools. Thank you for both of your suggestions--but I'm afraid I need to take it a step backwards in order to be on the same page as you two.

AFAIK, by giving me code, you'll only give me answers to my problem (and I probably will not learn it as well). In addition, that quality of self-accomplishment is gone. Thank you though!
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 09-09-2008, 06:15 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 876
Fubarable is on a distinguished road
Quote:
by giving me code, you'll only give me answers to my problem (and I probably will not learn it as well). In addition, that quality of self-accomplishment is gone.
Well spoken. If only more felt like you, I'd have more faith in the future of programming.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 09-09-2008, 06:39 AM
Member
 
Join Date: Apr 2008
Posts: 20
pheonix is on a distinguished road
Good answer bro. I thought u might be just a guy who is taking a required course even though its not your major. Good thing I just gave an idea.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 09-09-2008, 07:05 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
What index you want to have if if multiple max values are found? According to your example value 10 is the maximum and it found three times in the array. So what is the required index? First occurrence or all of them?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 09-09-2008, 06:17 PM
Member
 
Join Date: Sep 2008
Posts: 31
khajalid is on a distinguished road
what can you do is that just search for number with highest frequency.Then,search again and compare with that no and print the index.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 09-09-2008, 07:09 PM
Senior Member
 
Join Date: Aug 2008
Posts: 178
Supamagier is on a distinguished road
Code:
public int[] getMaximum(int[] array) { int[] toReturn = new int[array.length+1]; for (int i = 0, a = 0; i < array.length; i++) { if ( array[i] > toReturn[0]) { toReturn[0] = array[i]; toReturn[a++] = i; } } return toReturn; } }
This returns an array with the maximum in the first index, and in the other indexes the place of the it in the array checked.
__________________
check out
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
, 100% made by me.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 09-10-2008, 01:21 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by khajalid View Post
what can you do is that just search for number with highest frequency.Then,search again and compare with that no and print the index.
Yes, this is the easiest way you have to follow. That's why I ask about the maximum occurrence.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding the largest number in an array starchildren3317 New To Java 5 08-11-2008 05:42 AM
Disable Drag mode of Jinternal Frame smartsubroto New To Java 4 06-23-2008 09:21 AM
Finding elements in a vector Java Tip java.lang 0 04-14-2008 10:37 PM
Finding largest no bugger New To Java 11 11-29-2007 02:49 PM
Finding GCF in java lenny Advanced Java 1 07-31-2007 07:41 AM


All times are GMT +3. The time now is 02:09 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org