Question

In statistics the mode of a set of values is the value that occurs most often....

In statistics the mode of a set of values is the value that occurs most often. Write a program that determines how many pieces of pie most people eat in a year. Set up an integer array that can hold responses from 30 people. For each person, enter the number of pieces they say they eat in a year. Then write a function that finds the mode of these 30 values. This will be the number of pie slices eaten by the most people. The function that finds and returns the mode should accept two arguments, an array of integers, and a value indicating how many elements are in the array.

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

//create the below class and execute it

import java.util.Random;

/**

* @author Haaric

*

*/

public class PieStats {

public static void main(String[] args) {

//below 5 lines are for generating randon int values to fill out our input array

Random ran = new Random();

int peopleCount = 30;

int[] input = new int[peopleCount];

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

input[i] = Math.abs(ran.nextInt(100));

System.out.println("Array of numbers are: ");

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

System.out.println(input[i] + " ");

System.out.println("mode of the provided array is: " + findMode(input, peopleCount));

}

/**

* @param pieCountArr

* @param size

* @return mode

*/

public static int findMode(int[] pieCountArr, int size) {

int maxVal = 0, maxCount = 0;

//outer for loop is for iterating over the array of the counts

// inner for loop is for finding the occuraces of the element in the array

//the if condition is to check the max count of the currrent element is more than the previous element occurances

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

int count = 0;

for (int j = 0; j < size; ++j) {

if (pieCountArr[j] == pieCountArr[i])

++count;

}

if (count > maxCount) {

maxCount = count;

maxVal = pieCountArr[i];

}

}

return maxVal;

}

}

Add a comment
Know the answer?
Add Answer to:
In statistics the mode of a set of values is the value that occurs most often....
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
  • Create a function l_mode that finds the index of the value that occurs most often in...

    Create a function l_mode that finds the index of the value that occurs most often in an array. The input to your function is an array of integers A and the number of elements in the array 0 < n <= 10^3. Assuming v is the most frequent element in the array, your function will return the index i of the first (leftmost) occurrence of v in A. You can assume that -500 <= A[i] <= 500, for 0 <=...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • Problem overview. There are a number of ways to normalize, or scale, a set of values....

    Problem overview. There are a number of ways to normalize, or scale, a set of values. One common normalization technique scales the values so that the minimum value goes to 0, the maximum value goes to 1, and the other values are scaled accordingly. Using this normalization technique, the values of the array below are normalized. Original Array Values -1 -2 2 Normalized Array Values 0.25 0.01.0 0.5 The equation that computes the normalized value from a value xe in...

  • C++

    INFO1112 A11Lab 6March 18, 2021 Note:  It is important that you do this lab exercise properly.  Please read and follow the instructions very carefully.  You need to understand what you are required to do, and complete the program.  It uses a one-dimensional array.  You can refer to the PowerPoint slides, many of the tasks can be found there, but you need to apply them for this program. Note that the array will be filled with the integers when the user enters them. Consider...

  • Use basic C++ 3. A text file, superstars.txt, contains statistics on cricket players. For each player,...

    Use basic C++ 3. A text file, superstars.txt, contains statistics on cricket players. For each player, the file contains the player's first name, last name, number of matches played, total number of runs scored, number of times the player scored 100 runs in a match, and the number of wickets taken. The last row of data in the file contains the word "END" only. Some rows are shown below 30 11867 164 Chanderpaul Shivnarine 34 11912 130 Lara Brian 73...

  • C++ Programming 1. Write a function (header and body) that accepts an integer as an argument...

    C++ Programming 1. Write a function (header and body) that accepts an integer as an argument and returns that number squared. 2. Write the code that will fill an integer array named multiples with 10 integers from 5 to 50 that are multiples of five. (This should NOT be in the form of an initialization statement.) 3. Write the code that will display the contents of the integer array named multiples. Recall: the size of the array is 10. 4....

  • Goals: Write your own class. Instantiate an object of your class. Interact with the object's public...

    Goals: Write your own class. Instantiate an object of your class. Interact with the object's public interface. Requirements: Design a class that has an array of floating-point numbers. The constructor should accept an integer argument and dynamically allocate the array to hold that many numbers. The destructor should free the memory held by the array. In addition, there should be member functions to perform the following operations: Store a number in any element of the array - accepts an integer...

  • Design and implement a Java class (name it SummerStats.java) that tracks statistics for summer job salaries...

    Design and implement a Java class (name it SummerStats.java) that tracks statistics for summer job salaries for a group of people over several years. The only data field you need is a 2-Dimenssional array of values representing salaries. The rows the represents the people and the columns represent the years. The constructor method takes two integers representing the number of people and the number of years, then randomly generates the annual salaries and fills the array. Other class methods include...

  • Design and implement a Java class (name it Summer Stats. java) that tracks statistics for summer...

    Design and implement a Java class (name it Summer Stats. java) that tracks statistics for summer job salaries for a group of people over several years. The only data field you need is a 2-Dimenssional array of values representing salaries. The rows the represents the people and the columns represent the years. The constructor method takes two integers representing the number of people and the number of years, then randomly generates the annual salaries and fills the array. Other class...

  • test cases cannibal([27, 9, 3, 8, 11], 11) == 2 cannibal([27, 9, 3, 8, 11], 50)...

    test cases cannibal([27, 9, 3, 8, 11], 11) == 2 cannibal([27, 9, 3, 8, 11], 50) == 0 cannibal([3, 3, 3, 2, 2, 2, 1, 1, 1], 4) == 9 cannibal("1,2,3,4,5", 5) == 4 Because 7 8 9! Actually, this is mathematically impossible. "Cannibal numbers" as they're called can only eat numbers that are smaller than them, or the same size. After eating another number, the number that gets eaten disappears, and the number that ate them increases by 1...

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
ADVERTISEMENT