-4

Basic its this program takes input 6 numbers from user, store them in an Array, calculate their Mean and Mode. Also count how many numbers are greater than mean. The code of my program is where i'm wrong

My mean is correct but i have problem in mode.

package p18;

import java.util.Arrays; import java.util.Scanner;

public class P18 {

public static void main(String[] args) {

Scanner S=new Scanner(System.in);
  int[] arr1=new int [6];
     for (int i = 0; i < 6; ++i) {
         int g = S.nextInt();
           arr1[i] = g;
  }

     int input=6;

    double total=0d;
    double mean;

    for(int i=0;i<input;i++)
  {
    total=total+arr1[i];
   }
    mean= total/input;

    System.out.println("the mean is:" + mean); 

PROBLEM STARTS FROM HERE MODE PORTION


     int max=0;//problem starts from here
     int maxFreq=0;

     Arrays.sort(arr1);

     max = arr1[arr1.length-1];

     int[] count = new int[max + 1];

     for (int i = 0; i < arr1.length; i++) {
    count[arr1[i]]++;
}

 for (int i = 0; i < count.length; i++) {
    if (count[i] > maxFreq) {
        maxFreq = count[i];
    }
}

for (int i = 0; i < count.length; i++) {
    if (count[i] == maxFreq) {
        return i;
    }}
return -1;
  }}

4 Answers4

1

You are not storing the input into the array at all. You need to add something like the following to store the user input:

Scanner S=new Scanner(System.in);
int[] arr1=new int [6];
for (int i = 0; i < 6; ++i) {
    int g = S.nextInt();
    arr1[i] = g;
}

int input=6;

double total=0d;
double mean;

for(int i=0;i<input;i++)
{
    total=total+arr1[i];
}
mean= total/input;

System.out.println("the mean is:" + mean);

I also changed the mean and total to doubles so that you can get a decimal value for the mean, otherwise it would round down.

dcod
  • 70
  • 1
  • 9
0

You have to ask for every number you want to ask to the user, and if you want the mean you need to use a float or a double.

Scanner S = new Scanner(System.in);
double mean, total;
double input = 6;
double arr1 = new double[input];


for (int i = 0;i<input;++i){
    arr[i] = S.nextDouble();
}

for(int i=0;i<input;i++)
{
 total=total+arr1[i];
}
mean= total/input;

System.out.println("the mean is:" + mean);
Benoît Verhaeghe
  • 612
  • 1
  • 6
  • 21
0

Where do you think you are reading 6 integers? You call S.nextInt() only once and don't use its return value. This should be done in your loop to read multiple inputs and do something with them.

0

Finding the mean is easy; the other answers have covered that.

You also want to make sure you use a loop to gather 6 values for your array, and call nextInt() for all of those values.

The harder part may be the mode. The way I see to solve for the mode is:

1) Sort the array.

2) Create an int to hold the value of your current mode, and another int to hold the number of occurrences of that mode. You'll also need a counter int.

3) Using a loop, run through the sorted array. For every numeric value in the array, count how many times it occurs. Since it's in sorted order, every time the value changes, you know you've counted all of that value. Check to see if the occurrence is larger than the current largest occurrence, and if so, change the occurrence value and the mode value.

ragingasiancoder
  • 616
  • 6
  • 17