|
|
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.
|
|

09-09-2008, 03:56 AM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 22
|
|
|
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:
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
|
|

09-09-2008, 04:05 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 876
|
|
|
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.
|
|

09-09-2008, 04:20 AM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 22
|
|
|
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?
|
|

09-09-2008, 04:23 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 876
|
|
|
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.
|
|

09-09-2008, 05:53 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 20
|
|
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 
|
|

09-09-2008, 06:01 AM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 22
|
|
|
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!
|
|

09-09-2008, 06:15 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 876
|
|
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.
|
|

09-09-2008, 06:39 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 20
|
|
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. 
|
|

09-09-2008, 07:05 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
|
|
|
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.
|
|

09-09-2008, 06:17 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 31
|
|
|
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.
|
|

09-09-2008, 07:09 PM
|
|
Senior Member
|
|
Join Date: Aug 2008
Posts: 178
|
|
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.
|
|

09-10-2008, 01:21 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
|
|
Originally Posted by khajalid
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|