hi! i have written a program that takes a random number from 0-999, 10000 times. I have the program calculating how many times each number comes up randomly, and finds the mode. But my program doesn't include how to find multiple largest numbers, ex. 2 and 4 each come up 18 times, but it only prints 2, because it comes first. Can anyone help me on how I can print multiple largest numbers? Thanks!

import java.util.Random;
public class modeofnumbers {
    public static void main(String[] args) {
        Random randGen = new Random(1);
        int[] nums = new int[10000];
        for (int i=0; i<nums.length; i++) {
            nums[i] = randGen.nextInt(1000);
        }
        int[] count = new int[1000];
        for (int i=0; i<nums.length; i++){
            int digit = Integer.valueOf(nums[i]);
            count[digit]++;
        }
        System.out.println("Digit:  Frequency:");
        for (int i=0; i<1000; i++) {
            System.out.println(i + "\t" + count[i]);
        }
        int highIndex = 0;
        for (int i=0; i<10; i++) {
            if (count[i] > count[highIndex]) {
                highIndex = i;
            }
        }
        System.out.println("Mode = " + highIndex);
    }

}
for (int i=0; i<nums.length; i++){
            int digit = Integer.valueOf(nums[i]);
            count[digit]++;
        }

digit is not a good name for this variable in my opinion since it can range from 0 to 999, not 0 to 9.

int highIndex = 0;
        for (int i=0; i<10; i++) {
            if (count[i] > count[highIndex]) {
                highIndex = i;
            }
        }

If you want to keep track of more than one number, highIndex cannot be an int, since int can only store one value. Make highIndex a Vector or something else that can store more than one value.

I'd suggest adding an int variable called highCount, representing the number of times the mode shows up. It would be the maximum of count for all i.

Then go through count[] in a for loop and for any index i where count == highCount, add that index i to your highIndex Vector.

I'd suggest adding an int variable called highCount, representing the number of times the mode shows up. It would be the maximum of count for all i.

im lost upon how to do this. it would be highIndex, but how do i see which other numbers have the same as highIndex?

I am not familiar with Vectors. Is there a way that once the highest is founded it prints out every like number with the same highest? If not, mind explaining and/or guiding me to where i can learn about Vectors or an alternative way to do this?

hmm i made the changes you suggested, but i am not familiar with Vectors. Is there a way that once the highest is founded it prints out every like number with the same highest? If not, mind explaining and/or guiding me to where i can learn about Vectors?

http://java.sun.com/javase/6/docs/api/java/util/Vector.html
http://www.java2s.com/Code/Java/Collections-Data-Structure/UseVectorinjavautil.htm

You don't need to store the values. As you said, you can just display them:

if (count[i] == highCount)
     // display i

what is highCount initialized as or what do i set it equal to

for (int i=0; i<nums.length; i++){
            int digit = Integer.valueOf(nums[i]);
            count[digit]++;
        }

digit is not a good name for this variable in my opinion since it can range from 0 to 999, not 0 to 9.

int highIndex = 0;
        for (int i=0; i<10; i++) {
            if (count[i] > count[highIndex]) {
                highIndex = i;
            }
        }

If you want to keep track of more than one number, highIndex cannot be an int, since int can only store one value. Make highIndex a Vector or something else that can store more than one value.

I'd suggest adding an int variable called highCount, representing the number of times the mode shows up. It would be the maximum of count for all i.

Then go through count[] in a for loop and for any index i where count == highCount, add that index i to your highIndex Vector.

what is highCount initialized as or what do i set it equal to

For the set {5,5,5,6,6,7,7,7}, highCount = 3 since 5 and 7 are both modes and show up 3 times.

dont think i am not listening to what you said, but why wouldn't a simpler method work, this doesn't work, but something based off it might. Once it has found the final highIndex it rechecks count for every instance that the final highIndex is and prints that out, that would work, right? i am just not sure how to write that...

public static void main(String[] args) {
        Random randGen = new Random(1);
        int[] nums = new int[10000];
        for (int i=0; i<nums.length; i++) {
            nums[i] = randGen.nextInt(1000);
        }
        int[] count = new int[1000];
        for (int i=0; i<nums.length; i++){
            int digit = Integer.valueOf(nums[i]);
            count[digit]++;
        }
        System.out.println("Digit:  Frequency:");
        for (int i=0; i<1000; i++) {
            System.out.println(i + "\t" + count[i]);
        }
        int highIndex = 0;
        for (int i=0; i<10; i++) {
            if (count[i] > count[highIndex]) {
                highIndex = i;
            }
        if (count[i] == highIndex) {
            System.out.println("Mode(s) = " + highIndex);
        }
        }
    }

}
int highIndex = 0;
for (int i=0; i<10; i++) 
{
    if (count[i] > count[highIndex]) 
    {
        highIndex = i;
    }
    if (count[i] == highIndex) 
    {
        System.out.println("Mode(s) = " + highIndex);
    }
}

You can't display until you know what highIndex is, which you won't know till the for-loop is over. This line doesn't make sense:

if (count[i] == highIndex)

count[] and index have nothing to do with each other, so they should not be compared like this. If you are comparing anything to highIndex, it should be i, not count, since i loops through all indexes.

What does 10 represent here?


for (int i=0; i<10; i++)

after highIndex is found (the final highIndex) which in this case is 9
then that is compared to every number (count) and if the number is equal to the same highIndex which is 9 it would print out what number it is. But this would not happen from the start of the program, only after the final highIndex is found. i am not sure about 10, i put that there and didn't think about it...

after highIndex is found (the final highIndex) which in this case is 9

highIndex might be 9, might not be 9. You have no idea what highIndex will be until the for-loop is concluded. Thus you shouldn't display anything until the for-loop is concluded since you are only displaying or not displaying based on highIndex.

then that is compared to every number (count) and if the number is equal to the same highIndex which is 9 it would print out what number it is.

Again, highIndex may not ever equal 9 and why are you comparing count to highIndex? highIndex is never assigned to anything involving count[]. It's assigned to equal i here:


highIndex = i;

You are comparing an index to something that isn't an index. It's comparing oranges to apples. You can compare count to count[highIndex] and you can compare i to highIndex, but comparing count to highIndex is meaningless.

But this would not happen from the start of the program, only after the final highIndex is found.

int highIndex = 0;
for (int i=0; i<10; i++) 
{
    if (count[i] > count[highIndex]) 
    {
        highIndex = i;
    }
    if (count[i] == highIndex) 
    {
        System.out.println("Mode(s) = " + highIndex);
    }
}

The comparison is made inside the for-loop and highIndex is changing all the time inside that for-loop. You are not just comparing something to the FINAL highIndex.

I have change your code snippet as below check it out. It will print all the modes having maximum number. Check for the arrays sizes if i think it need some changes.

import java.util.Random;

public class ModeOfNumbers {
	public static void main(String[] args) {
		Random randGen = new Random(1);
		int[] nums = new int[10000];
		for (int i=0; i<nums.length; i++) {
			nums[i] = randGen.nextInt(1000);
		}
		int[] count = new int[1000];
		for (int i=0; i<nums.length; i++){
			int digit = Integer.valueOf(nums[i]);
			count[digit]++;
		}
		System.out.println("Digit: Frequency:");
		for (int i=0; i<1000; i++) {
			System.out.println(i + "\t" + count[i]);
		}
		int highIndex = 0;
		int[] modes = new int[10];
		modes[0] = 0;
		int n = 0;
		
		for (int i=0; i<1000; i++) {
			if (count[i] > count[highIndex]) {
				highIndex = i;
			}
			if (count[i] == count[highIndex]) {
				if(count[i] > count[modes[0]]){
					modes[0] = i;
					n = 1;
					for(int j=1; j<10; j++)
						modes[j] = 0;
				}
				else{
					modes[n++] = i;
				}
			}
		}
		for(int i = 0; i<10 ; i++){
			if(modes[i] ==0) {
				break;
			}
			else
				System.out.println("Mode = " + modes[i]);
		}

	}

}

thanks to both of you, i now know about Vectors and how to solve this problem. hardik.rajani, that is what i was trying to say, and figured some of that out on my own, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.