|
-
June 26th, 2012, 09:34 PM
#1
mean, median and mode problem
Code:
import java.util.*;
class MMM {
//This method stores the sum of all the elements of array in variable sum and returns the average by dividing the sum with the length of array.
public static int findMean(int array[]){
int sum = 0;
int average=0;
int length=array.length;
for(int j = 0; j < length; j++){
sum += array[j];
average = sum / length;
}
return average;
}
//This method firstly sort the array and returns middle element if the length of arrray is odd.Otherwise it will return the average of two miidele elements.
public static int findMedian(int array[]) {
int length=array.length;
int[] sort = new int[length];
System.arraycopy(array, 0, sort, 0, sort.length);
Arrays.sort(sort);
if (length % 2 == 0) {
return (sort[(sort.length / 2) - 1] + sort[sort.length / 2]) / 2;
} else {
return sort[sort.length / 2];
}
}
//This method counts the occurrence of each element of array and return the lement which has the maximum count.
public static int findMode(int array[]) {
int max=0, maxCount=0;
int length=array.length;
for (int i = 0; i <length; ++i) {
int count = 0;
for (int j = 0; j <length; ++j) {
if (array[j] == array[i]) ++count;
}
if (count > maxCount)
{
maxCount = count;
max = array[i];
}
}
return max;
}
public static void main(String[] args)
{
Scanner nums = new Scanner(System.in);
//int min = 0;
//int max = 0;
int[] num = new int[30];
for ( int i = 0; i < num.length; i++) {
System.out.println("Enter a number: ");
num[i] = nums.nextInt();
}
int mean=MMM.findMean(num);
System.out.println("Mean= "+mean);
int median=MMM.findMedian(num);
System.out.println("Median= "+median);
int mode=MMM.findMode(num);
System.out.println("Mode= "+mode);
}
}
Good day to all..
I am already done with the coding and have visited many source to come up with this. But I got 2 problems that I haven't found any solution yet.
1. The input must only terminate once the user enters a negative number or the user has entered 30 integers already.
2. The integers must also be less than 1000; otherwise, the program will just ignore the input.
I've been tweaking this code for days now and still didn't find any solution for the problems. I hope someone can help me. And thank you for the time also and the chance to post here.
My degree of programming in java is below average as of now just started last 2 weeks ago due to requirements in my studies. Aslo forgive my coding if it is not that readable.
This should be the result of this code..
Enter number: 6
Enter number: 7
Enter number: 78
Enter number: 1004
Enter number: 78
Enter number: 54
Enter number: 3
Enter number: 0
Enter number: 66
Enter number: 689
Enter number: -5
Mean: 109
Median: 54
Mode: 78
-
June 28th, 2012, 03:26 AM
#2
Re: mean, median and mode problem
Hi,
You only have to check each value read. For example:
Code:
[...]
for (int i = 0; i < num.length; i++, size++) {
System.out.println("Enter a number: ");
num[i] = nums.nextInt();
if (num[i] < 0) {
System.out.println("num["+ i + "] is negative. Break");
num[i] = 0;
break;
}
if (num[i] >= 1000) {
System.out.println("num["+ i + "] is greater than 1000. Ignored");
i--;
size--;
}
}
[...]
Of course, you can avoid comments
A couple of considerations,
- You must count how many values are read. If you don't, your calculations will be wrong. For instance, if you read only 5 values, mean is their sum divided by 5, not by 30, as you do.
- The way you are taking mean value is wrong. MEAN = SUM(num)/size. MEAN should also be a double or float type, not int
- You should also check the way you are calculating mode and medina. Use size instead of length.
Hope this helps.
Albert.
Please, correct me. I'm just learning.... and sorry for my english :-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|