View Single Post
Old Nov 27th, 2005, 2:17 PM   #1
Java|Tera
Newbie
 
Join Date: Nov 2005
Posts: 14
Rep Power: 0 Java|Tera is on a distinguished road
Arrow Median/Mode in arrays? {Need help}

Hi, I am fairly new to Java Programming, as well as these forums..but I am having a problem in which I need help.

Our class assignment is to make a program which random numbers are generated into an array. The array length is determined by the user, as is the range of the random numbers. We have to do this using different methods, and the rules are, we can't use static, or class variables.

After calculating the random numbers, a menu appears in which the user can choose to:

- Show all of the numbers in the array

- Find the largest number in the array

- Find the smallest number in the array

- Find the sum of all the numbers in the array

- Find the average of all the numbers in the array

- Find the median of all the numbers in the array

- Find the mode of all the numbers in the array

The ones in red are the things I am having problems with.

Now, for the showing the numbers in the array bit.

It works when I print the array in the same method it is calculated in, but when I pass it to the menu method, it prints all of the same number, ex: 4 4 4 4 4 4 4 4

Here is the code for the main method:

import java.io.*;
import java.text.DecimalFormat;

public class Random_Amount {
	
    public static void main(String args[ ])throws IOException {
	Random_Amount rAm= new Random_Amount();
	int intNumber, intNumber1= 999999,intRangeHigh, intRangeLow, intRangeSum, intTotal= 0, intRan, intHighNum= -99999, intLowNum= 99999;
	
	intNumber= rAm.Amount();
	
	intRangeLow= rAm.Low();
	
	intRangeHigh= rAm.High();
	
	intRangeSum= intRangeHigh - intRangeLow;
	
	int intList[]= new int[intNumber1];
	for (int i= 0; i < intNumber; i++) {
	    intList[intNumber]=intRan= (int)((Math.random()*(intRangeSum)+1)+intRangeLow);
	    intTotal+=intRan;
	    if (intRan > intHighNum)
	    intHighNum= intRan;
	
	    if (intRan < intLowNum)
	    intLowNum= intRan;
	}
	
	System.out.println();
	
	
	rAm.Menu(intLowNum, intHighNum, intTotal, intNumber, intList);
    
    }

And here is the code for the menu method:

private void Menu(int intLowNum, int intHighNum, int intTotalNum, int intAmount, int intArray[])throws IOException {
    Random_Amount rAm= new Random_Amount();
    String strChoice;
    int intChoice;
    double dblAverage;
    DataInputStream stdin= new DataInputStream(System.in);
    DecimalFormat oneDecimal= new DecimalFormat("0.0");
    System.out.println("Main Menu");
    System.out.println();
    
    System.out.println("Enter 1 to view the random numbers");
    System.out.println();
    
    System.out.println("Enter 2 to find the largest number of the random numbers");
    System.out.println();
    
    System.out.println("Enter 3 to find the smallest number of the random numbers");
    System.out.println();
    
    System.out.println("Enter 4 to find the sum of the random numbers");
    System.out.println();
    
    System.out.println("Enter 5 to find the average of the random numbers");
    System.out.println();
    
    System.out.println("Enter 6 to find the median of the random numbers");
    System.out.println();
    
    System.out.println("Enter 7 to find the mode of the random numbers");
    System.out.println();
    
    System.out.println("Enter 8 to end the program");
    System.out.println();
    
    strChoice= stdin.readLine();
    intChoice= Integer.parseInt(strChoice);
    System.out.println();
    
    while (intChoice!=8) {
    switch (intChoice) {
		case 1: for (int i= 0; i < intAmount; i++)
			    System.out.println(intArray[intAmount]);
			    break;
		
		case 2: System.out.println("The largest number is "+intHighNum); break;
		
		case 3: System.out.println("The smallest number is "+intLowNum); break;
		
		case 4: System.out.println("The sum of all the numbers is "+intTotalNum); break;
		
		case 5: 
		    dblAverage= (( double ) intTotalNum) / (( double ) intAmount);
		    System.out.println("The average of all the numbers is "+oneDecimal.format(dblAverage));
		    break;
		
		case 6:System.out.println();
		break;
		
		case 7: System.out.println(); break;
	 
		default: System.out.println("Invalid Input"); break;
	}
	strChoice= stdin.readLine();
	intChoice= Integer.parseInt(strChoice);
	}
	System.out.println("Sequence ended by User's request");
      }

Please don't criticise my code, I am new to it, as I mentioned earlier

So yeah...can someone help me with that bit?

As for the median part, I sorted the array, and tried to find the median, but it just prints out a 0, can someone help me with this?

void findMedian(int intArray[], int intArraySize)
    {
	int i, j, k;
	int intMinimum, intTemporary, intDivide, intMedian;

	for (i = 0; i < intArraySize-1; i++) {
	    intMinimum = i;
	    for (j = i+1; j < intArraySize; j++) {
		if (intArray[j] < intArray[intMinimum])
		    intMinimum = j;
	    }
	intTemporary = intArray[i];
	intArray[i] = intArray[intMinimum];
	intArray[intMinimum] = intTemporary;
  
	}
	if (intArray.length%2 == 0) {							    //If the length of the array is even
			intDivide = intArray.length/2;
			intMedian = (intArray[intDivide] + intArray[intDivide-1])/2;	    //The median is defined as the average of the middle numbers
		}
		else 	{								    //If the length of the array is odd
			intDivide = intArray.length/2;
			intMedian = intArray[intDivide];				    //Defines the median as the middle number
		}
		System.out.println ();
		System.out.println("The median of all random numbers is " + intMedian);

    	}

And finally, for the mode, I have no idea how to find that :\

If someone could help me with these problems, that would be great

Here is the full code, {Just for Reference}

import java.io.*;
import java.text.DecimalFormat;

public class Random_Amount {
	
    public static void main(String args[ ])throws IOException {
	Random_Amount rAm= new Random_Amount();
	int intNumber, intNumber1= 999999,intRangeHigh, intRangeLow, intRangeSum, intTotal= 0, intRan, intHighNum= -99999, intLowNum= 99999;
	
	intNumber= rAm.Amount();
	
	intRangeLow= rAm.Low();
	
	intRangeHigh= rAm.High();
	
	intRangeSum= intRangeHigh - intRangeLow;
	
	int intList[]= new int[intNumber1];
	for (int i= 0; i < intNumber; i++) {
	    intList[intNumber]=intRan= (int)((Math.random()*(intRangeSum)+1)+intRangeLow);
	    intTotal+=intRan;
	    if (intRan > intHighNum)
	    intHighNum= intRan;
	
	    if (intRan < intLowNum)
	    intLowNum= intRan;
	}
	
	System.out.println();
	
	
	rAm.Menu(intLowNum, intHighNum, intTotal, intNumber, intList);
    
    }
    
    private int Amount()throws IOException {
	DataInputStream stdin= new DataInputStream(System.in);
	String strNum;
	int intNum;
    
	System.out.println("Please enter the amount of numbers to calculate.");	
	strNum= stdin.readLine();
	intNum= Integer.parseInt(strNum);
	System.out.println();
	return intNum;
    }
	
    private int Low()throws IOException {
	DataInputStream stdin= new DataInputStream(System.in);
	String strRangeLow;
	int intRangeLow;
	
	System.out.println("Please enter the lowest number you would like to calculate.");
	strRangeLow= stdin.readLine();
	intRangeLow= Integer.parseInt(strRangeLow);
	System.out.println();
	return intRangeLow;
    }
    	
    private int High()throws IOException {
	DataInputStream stdin= new DataInputStream(System.in);
	String strRangeHigh;
	int intRangeHigh;

	System.out.println("Please enter the highest number you would like to calculate.");
	strRangeHigh= stdin.readLine();
	intRangeHigh= Integer.parseInt(strRangeHigh);
	System.out.println();
	System.out.println();
	System.out.println();
	return intRangeHigh;
    }
    
    
    private void Menu(int intLowNum, int intHighNum, int intTotalNum, int intAmount, int intArray[])throws IOException {
    Random_Amount rAm= new Random_Amount();
    String strChoice;
    int intChoice;
    double dblAverage;
    DataInputStream stdin= new DataInputStream(System.in);
    DecimalFormat oneDecimal= new DecimalFormat("0.0");
    System.out.println("Main Menu");
    System.out.println();
    
    System.out.println("Enter 1 to view the random numbers");
    System.out.println();
    
    System.out.println("Enter 2 to find the largest number of the random numbers");
    System.out.println();
    
    System.out.println("Enter 3 to find the smallest number of the random numbers");
    System.out.println();
    
    System.out.println("Enter 4 to find the sum of the random numbers");
    System.out.println();
    
    System.out.println("Enter 5 to find the average of the random numbers");
    System.out.println();
    
    System.out.println("Enter 6 to find the median of the random numbers");
    System.out.println();
    
    System.out.println("Enter 7 to find the mode of the random numbers");
    System.out.println();
    
    System.out.println("Enter 8 to end the program");
    System.out.println();
    
    strChoice= stdin.readLine();
    intChoice= Integer.parseInt(strChoice);
    System.out.println();
    
    while (intChoice!=8) {
    switch (intChoice) {
		case 1: for (int i= 0; i < intAmount; i++)
			    System.out.println(intArray[intAmount]);
			    break;
		
		case 2: System.out.println("The largest number is "+intHighNum); break;
		
		case 3: System.out.println("The smallest number is "+intLowNum); break;
		
		case 4: System.out.println("The sum of all the numbers is "+intTotalNum); break;
		
		case 5: 
		    dblAverage= (( double ) intTotalNum) / (( double ) intAmount);
		    System.out.println("The average of all the numbers is "+oneDecimal.format(dblAverage));
		    break;
		
		case 6:System.out.println(); break;
		
		case 7: System.out.println(); break;
	 
		default: System.out.println("Invalid Input"); break;
	}
	strChoice= stdin.readLine();
	intChoice= Integer.parseInt(strChoice);
	}
	System.out.println("Sequence ended by User's request");
      }
    
    void findMedian(int intArray[], int intArraySize)
    {
	int i, j, k;
	int intMinimum, intTemporary, intDivide, intMedian;

	for (i = 0; i < intArraySize-1; i++) {
	    intMinimum = i;
	    for (j = i+1; j < intArraySize; j++) {
		if (intArray[j] < intArray[intMinimum])
		    intMinimum = j;
	    }
	intTemporary = intArray[i];
	intArray[i] = intArray[intMinimum];
	intArray[intMinimum] = intTemporary;
  
	}
	if (intArray.length%2 == 0) {							    //If the length of the array is even
			intDivide = intArray.length/2;
			intMedian = (intArray[intDivide] + intArray[intDivide-1])/2;	    //The median is defined as the average of the middle numbers
		}
		else 	{								    //If the length of the array is odd
			intDivide = intArray.length/2;
			intMedian = intArray[intDivide];				    //Defines the median as the middle number
		}
		System.out.println ();
		System.out.println("The median of all random numbers is " + intMedian);

    	}								
    }

Oh, and if it matters..we are using Java Workshop 2.0 (Have to, stupid school :mad: )

Last edited by Java|Tera; Nov 27th, 2005 at 2:28 PM. Reason: Changed Title
Java|Tera is offline   Reply With Quote