Teacher’s Notes:
=======================================
This is an assessment I designed in three parts. This supports my outcomes based assessment by creating tiers of skill that students should be able to demonstrate mastery over.
The assignment can be easier to mark because each student must write the answer in the box provided. If they can do this, then you won't need to review their code line by line since it is unlikely they found the answer by guessing.
Assignment:
=======================================
Name:____________________
Mean, Median, Mode
In this assignment, you will create a program that will read in a series of numbers from 1 to 100 and find either the mean, median or mode of those numbers. The first line of input will be n, the number of items in the series. The next n inputs will be integers that comprise the series of integers that you are to find the mean, median or mode of. To complete this assignment, you must use the numbers found at:
https://github.com/TaoOfChow/ClassroomSupportFiles/blob/main/MMMNumbers.txt
Print the mean, median and mode of these numbers in the table below. You should attempt as many of these three tasks as you can. They do not need to be done in order.
Mean (to 2 decimals):
|
|
Median (to 2 decimals):
|
|
Mode:
|
|
Basic:
The Mean:
The mean of the numbers is the average value. To find the mean, you add all of the values and divide the sum by the number of items you added together. After finding the mean, the program should print all of the numbers provided and also print out the mean value.
Advanced:
The Mode:
The mode is the number that appears most often. If there is a tie for appearing the most often, the program will display the lowest number as the mode.
Mastery:
The Median:
The median of the numbers is the middle number. This means that if you were to arrange the numbers in ascending order, the median number would be the number in the middle. If there are two numbers in the middle (an even number of items), then the median is the average of the two middle numbers. After finding the median,the program should print out the median value.
Note: For this assignment, you may NOT use any built in sort commands such as Arrays.sort(). You may build your own sorting algorithm if you want. There are ways to solve the problem without sorting the numbers.
Sample Input 1
20
40 41 22 89 6 9 78 3 25 75 79 78 14 63 95 71 78 44 77 11
Sample Output 1
40 41 22 89 6 9 78 3 25 75 79 78 14 63 95 71 78 44 77 11
Mean: 49.9
Median: 53.5
Mode: 78
Solution (JAVA):
=======================================
package code;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//list generator
/*
int[] list = new int[2000];
for(int x = 0; x < list.length; x++) {
list[x] = (int)(Math.random()*100)+1;
System.out.println(list[x]);
}*/
Scanner input = new Scanner(System.in);
int size = input.nextInt();
int[] list = new int[size];
for(int x = 0; x < list.length; x++)
list[x] = input.nextInt();
//Finding the mean and displaying the numbers back
double sum = 0;
for(int x = 0; x < list.length; x++)
{
System.out.print(list[x] + " ");
sum += list[x];
}
System.out.println("\nMean: " + sum/size);
//finding the median using bubble sort
for(int x = 0; x < list.length-1; x++)
for(int y = x; y < list.length; y++)
if(list[x] > list[y])
{
int temp = list[x];
list[x] = list[y];
list[y] = temp;
}
if(size % 2 == 0)
System.out.println("Median: " + (list[size/2] + list[size/2-1])/2.0);
else
System.out.println("Median: " + list[size/2]);
//finding the median by repeatedly removing the highest and lowest values until there is/are
//only the middle number(s) remaining.
/*
for(int y = 0; y < size/2-1; y++)
{
int highest = 0, lowest = 101;
int highestIndex = 0, lowestIndex = 0;
for(int x = 0; x < list.length; x++)
{
if(list[x] == -1)
continue;
if(list[x] > highest) {
highest = list[x];
highestIndex = x;
}
if(list[x] < lowest) {
lowest = list[x];
lowestIndex = x;
}
}
list[lowestIndex] = -1;
list[highestIndex] = -1;
}
sum = 0;
int count = 0;
for(int x = 0; x < list.length; x++)
if(list[x] != -1)
{
sum += list[x];
count++;
}
System.out.println("Median: " + sum/count);
*/
//finding the mode using a quick lookup table
int[] numbers = new int[101];
for(int x = 0; x < list.length; x++)
numbers[list[x]]++;
int max = 0;
int maxIndex = 0;
for(int x = 0; x < numbers.length; x++)
if(numbers[x] > max)
{
max = numbers[x];
maxIndex = x;
}
System.out.println("Mode: " + maxIndex);
}
}
//sample input 20 40 41 22 89 6 9 78 3 25 75 79 78 14 63 95 71 78 44 77 11