![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 | |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Quote:
|
|
|
|
|
|
|
#22 |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 5
![]() |
I don't know about hideously inefficient... it is O(N).
I would agree that it is not the most elegant way to do it though, and my method does nothing for ties. ![]() EDIT: Actually, now that I think about it, if I populate both arrays at the same time, then it becomes O(1). And I think that you are describing essentially the same thing that I am, only it appears that with your method, each time you check the count, you have to traverse the array where you stored your count... Unless I am totally misunderstanding your angle of attack, in which case I am always open to a fresh perspective.
__________________
HijackThis Team-SFDC Last edited by groovicus; Nov 28th, 2005 at 1:21 PM. |
|
|
|
|
|
#23 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Lemme write some code for ya - check back in a bit.
|
|
|
|
|
|
#24 |
|
Programmer
|
I would sort the array making the numbers in increasing order. You already have a method to find the smallest number in the array, just use that and once you find the smallest number add that to a new array. When that is all done you will have the array in increasing order. You can easily find the median that way, plus it will make it easier to find the mode.
JD |
|
|
|
|
|
#25 |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 5
![]() |
I'll throw in mine just for comparison...
public void Mode() {
for(int i = 0; i < rand.length; i++){
rand[i] = (int)(Math.random()*TOP_BOUND);
count[rand[i]]++;
if(count[rand[i]] > mode) {
mode = count[rand[i]];
index = rand[i];
}
}
}Its not quite complete, but the missing parts should be pretty obvious. ![]()
__________________
HijackThis Team-SFDC Last edited by groovicus; Nov 28th, 2005 at 2:41 PM. |
|
|
|
|
|
#26 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
But what if there's more than one mode?
|
|
|
|
|
|
#27 |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 5
![]() |
I mentioned that my method does not handle multiples... That is just another tiny loop, but it does increase it to O(n). (lol..
unless I add another array )...But since it is an assignment, it would hardly be fair to give away the farm.
__________________
HijackThis Team-SFDC |
|
|
|
|
|
#28 |
|
Newbie
Join Date: Nov 2005
Posts: 14
Rep Power: 0
![]() |
The deadline was extended, I now have 24 hours. I have the median, and now I just need the mode. I fixed all the other problems. Here is my code
One problem I am having at the moment, is I am not able to bulletproof the ShowMenu bit of code, it's commented out. Any suggestions? Oh, and also, I have to find the mode, and I need to figure out a way to let the user choose new random numbers if they want to ![]() ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Name: Random_Amount program //
// Author: jkyyiuyiyui //
// Date: November 29th, 2005 //
//********************************************************************************************************************************************************************//
// Problem Definition: //
// The program generates random numbers according to the data input by the user. It will then determine the largest and smallest numbers, //
// the sum, average, median, and mode of all the numbers displayed in a menu all by the user's request. //
//********************************************************************************************************************************************************************//
// List of Indentifiers: //
// - intCount is the amount of numbers created in the array. //
// - intLow is the lowest value of the random numbers that can be created. //
// - intHigh is the highest value of the random numbers that can be created. //
// - intChoice is the variable that is used to choose which action to perform in the menu method. //
// - getSum is the total sum of all the random numbers. //
// - intList is the array //
// - intInput is used to detect run-time errors in the duration of the program. //
// //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.text.DecimalFormat;
public class RandomNumberProject {
int[] intList = null; //Instantiates array intList giving it a value of 0 (null)
public static void main(String args[ ])throws IOException { //Creates main method
RandomNumberProject rAm = new RandomNumberProject();
int intCount, intLow, intHigh, intSelection= 0;
intCount = rAm.Amount(); //Assigns intNumber the return value from method Amount
intLow = rAm.LowNumber(); //Assigns intRangeLow the return value from method Low
intHigh = rAm.HighNumber(intLow); //Assigns intRangeHigh the return value from method High
rAm.Random(intCount, intLow, intHigh);
do {
intSelection = rAm.ShowMenu(); //Calls the method ShowMenu assigning variables as parameters to pass data
System.out.println("---------------- Answer ----------------\n");
rAm.PerformMenu(intSelection); //Calls the PerformanceMenu method while sending the data of intSelection to it's parameters
System.out.println("---------------- Answer ----------------\n");
}
while(intSelection > 0);
} //End of main-method
private int ShowMenu()throws IOException {
int intChoice = -1, intInput;
String strChoice = "";
DataInputStream stdin= new DataInputStream(System.in);
System.out.println("Main Menu\n");
System.out.println("Enter < 1 > to view the random numbers\n");
System.out.println("Enter < 2 > to find the largest number of the random numbers\n");
System.out.println("Enter < 3 > to find the smallest number of the random numbers\n");
System.out.println("Enter < 4 > to find the sum of the random numbers\n");
System.out.println("Enter < 5 > to find the average of the random numbers\n");
System.out.println("Enter < 6 > to find the median of the random numbers\n");
System.out.println("Enter < 0 > to end the program\n");
strChoice= stdin.readLine();
if(strChoice=="") //If strChoice holds no value
return 0; //Return the number 0 to the method that calls it
intChoice= Integer.parseInt(strChoice);
return intChoice; //Return the data to that method that calls it (If the if condition doesn't apply)
}
private void Random(int count, int high, int low){
intList = new int[count];
for (int i= 0; i < count; i++) { //Loops the number of times Inputted
int generatedValue = (int)((Math.random()*(high-low)+1)+low); //Assigns random numbers into the array
intList[i] = generatedValue;
} //End of for-loop
}
private int Amount()throws IOException { //Method Amount, which purpose is to find the amount of numbers to create and return the value to the main method
RandomNumberProject rAm= new RandomNumberProject();
DataInputStream stdin= new DataInputStream(System.in);
int intAmount;
System.out.println("Please enter the amount of numbers to calculate.\n");
do { //Do-loop, the following code will be processed if the conditions are true
intAmount= rAm.Error(); //Assigns intAmount to the return value of method Error
if (intAmount < 1) { //If intAmount is less than 1
System.out.println("This number must be larger than 0");
System.out.println("Please enter the amount of numbers to calculate.");
} //End of if statement
} //End of do-loop
while(intAmount < 1); //While intAmount is less than 1
return intAmount; //Returns the integer value of strNum to the main method
} //End of method Amount
private int LowNumber()throws IOException { //Method Low, which purpose is to declare the lowest number that can be created in the array and return the value to the main method
RandomNumberProject rAm= new RandomNumberProject();
DataInputStream stdin= new DataInputStream(System.in);
int intRangeLow;
System.out.println("Please enter the lowest number you would like to calculate.\n");
//do { //Do-loop, the following code will be processed if the conditions are true
intRangeLow= rAm.Error();
//if (intRangeLow < 1) {
//System.out.println("This number must be larger than 0");
//System.out.println("Please enter the lowest number you would like to calculate.\n");
//} //End of if statement
//} //End of do-loop
//while(intRangeLow < 1); //While intRangeLow is less than 1
return intRangeLow; //Returns the integer value of strRangeLow to the main method
} //End of method Low
public int HighNumber(int intRangeLow)throws IOException { //Method High, which purpose is to declare the highest number that can be created in the array and return the value to the main method
RandomNumberProject rAm= new RandomNumberProject();
DataInputStream stdin= new DataInputStream(System.in);
int intRangeHigh;
System.out.println("Please enter the highest number you would like to calculate.\n");
do { //Do-loop, the following code will be processed if the conditions are true
intRangeHigh= rAm.Error();
if (intRangeHigh < intRangeLow) {
System.out.println("The highest number can't be lower than the lowest number.");
System.out.println("Please enter the highest number you would like to calculate.\n");
} //End of if statement
} //End of do-loop
while(intRangeHigh < intRangeLow); //While intRangeHigh is less than intRangeLow
return intRangeHigh; //Returns the integer value of strRangeHigh to the main method
} //End of method High
private void PerformMenu(int intChoice)throws IOException { //Method Menu, which purpose is to output a menu in which the user can choose which actions to perform
DecimalFormat oneDecimal= new DecimalFormat("0.0");
switch (intChoice) { //Switch statement
case 1: for (int i= 0; i < intList.length; i++) //Loop the length of the array amount of times
System.out.println(intList[i]); //Displays the array if the number 1 is entered
break;
case 2: System.out.println("The largest number is "+HighNum()); break; //Displays the largest number if the number 2 is entered
case 3: System.out.println("The smallest number is "+LowNum()); break; //Displays the largest number if the number 2 is entered
case 4: System.out.println("The sum of all the numbers is "+getSum()); break; //Displays the largest number if the number 2 is entered
case 5: double dblAverage= (( double ) getSum()) / (( double ) getCount()); //Converts intTotalNum and intAmount into doubles and finds the average
System.out.println("The average of all the numbers is "+oneDecimal.format(dblAverage)); break;
case 6:findMedian(); break;
case 0: System.out.println("Program ended at user's request"); break;
default: System.out.println("Invalid Input, please enter a number between 1 and 6, or 0"); break; //If a wrong number is input, a message informs the user
}
} //End of method Menu
private int HighNum(){ //Create method HighNum
int intValue = Integer.MIN_VALUE; //intValue is given the minimum value that an integer can hold
for( int i = 0 ; i<intList.length; i++){ //Loop the amount of times of the length of the array
if(intList[i]>intValue){ //If the random number is higher than intValue
intValue = intList[i];
} //End of if statement
} //End of for-loop
return intValue; //Return the data to the method that calls it
} //End of method HighNum
private int LowNum(){ //Create method LowNum
int intValue = Integer.MAX_VALUE; //intValue is given the maximum value that an integer can hold
for( int i = 0 ; i<intList.length; i++){ //Loop the amount of times of the length of the array
if(intList[i]<intValue){ //If the random number is lower than intValue
intValue = intList[i];
} //End of if statement
} //End of for-loop
return intValue; //Return the data to the method that calls it
} //End of method HighNum
private int getCount(){ //Create method getCount
if(intList==null) //If intList has no value
return 0; //Return the value 0 to the method that calls it
else //If the if condition doesn't apply
return intList.length; //Return the length of the array to the method that calls it
} //End of method getCount
private int getSum() { //Create method getSum
int intTotal = 0;
for( int i = 0 ; i<intList.length; i++){ //Loop the array as many times as the length of the array is
intTotal+=intList[i];
} //End of the for-loop
return intTotal; //Return the data to the method that calls it
} //End of method getSum
private void BubbleSort() { //Create void method BubbleSort
int intOut, intIn, intTemp;
for(intOut=intList.length-1; intOut > 1; intOut--) //Outer loop, loops the length of the array, in descending order
for(intIn=0; intIn < intOut; intIn++) //Inner loop, loops the length of the array in ascending order
if( intList[intIn] > intList[intIn+1] ) //If statement, sorts the array into final ascending order
{
intTemp = intList[intIn];
intList[intIn] = intList[intIn+1];
intList[intIn+1] = intTemp;
}
} //End of method BubbleSort
private void findMode() {
}
private void findMedian() { //Method findMedian, which purpose is to find the median of all the numbers in the array and output it
BubbleSort(); //Call method BubbleSort
int intDivide = 0;
int intMedian = 0;
if (intList.length%2 == 0) { //If the length of the array mod 2 is equal to 0
intDivide = intList.length/2;
intMedian = (intList[intDivide] + intList[intDivide-1])/2;
} //End of if statement
else {
intDivide = intList.length/2;
intMedian = intList[intDivide];
} //If the if condition doesn't apply
System.out.println("The median of all random numbers is " + intMedian);
}
public int Error() throws IOException { //Method Error, which purpose is to provide "bulletproof" code to stop run-time errors
DataInputStream stdin=new DataInputStream(System.in);
int intInput;
while(true) { //While the terms are true (Terms determined in other method) then
try { //Assign intInput to be the integer variable of the input
intInput=Integer.parseInt(stdin.readLine());break;
} //End of try-statement
catch(NumberFormatException e) { //If the input cannot be assigned to intInput
System.out.println("Invalid Input: Not a number, please try again"); //A message appears telling the user
} //End of catch-statemt
} //End of while-loop
return intInput; //Return the bullet-proof integer variable
} //End of method Error
} //End of the public class Random_AmountLast edited by Java|Tera; Nov 29th, 2005 at 12:03 PM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|