Question

JAVA The mode of a list of numbers is the number listed most often. Write a...

JAVA

The mode of a list of numbers is the number listed most often. Write a program that takes 10 numbers as input and displays the mode of these numbers. Your program should use parallel arrays and a method that takes an array of numbers as a parameter and returns the value that appears most often in the array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class ArrayMode {

   public static int mode(int arr[]) {
       int maxValue = 0, maxCount = 0;

       for (int i = 0; i < arr.length; ++i) {
           int count = 0;
           for (int j = 0; j < arr.length; ++j) {
               if (arr[j] == arr[i])
                   ++count;
           }
           if (count > maxCount) {
               maxCount = count;
               maxValue = arr[i];
           }
       }

       return maxValue;
   }

   public static void main(String args[]) {
       int arr[] = { 9, 5, 3, 8, 5, 12, 19, 5, 11 };

       System.out.println("The set of numbers are: ");
       for (int i = 0; i < arr.length; i++)
           System.out.print(arr[i] + " ");

       System.out.println("\nThe mode of the set is: " + mode(arr));
   }
}

Add a comment
Know the answer?
Add Answer to:
JAVA The mode of a list of numbers is the number listed most often. Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT