Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 28th, 2005, 11:48 AM   #21
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Quote:
Originally Posted by groovicus
MODE:
To find the mode, all you need is a simple array (filled with zeroes) that is the same size as the largest possible random number that can be generated . Then you loop through your array of 1000 randomly generated numbers. If the first number is a one, then you put the number one in index one in your counting array. If your second number is a one, then you increment the count at one again, so then you have two. Whatever the random number is, increment the count in your array by one. Once you are done, then go through your counting array, see which cell has the largest number, and that is your mode.
That's hideously inefficient. You just need three integers and one integer array: the current modal numbers (array), the modal number count, the current number and the current number count.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 28th, 2005, 12:58 PM   #22
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 5 groovicus is on a distinguished road
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.
groovicus is offline   Reply With Quote
Old Nov 28th, 2005, 1:27 PM   #23
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Lemme write some code for ya - check back in a bit.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 28th, 2005, 2:16 PM   #24
JDStud6
Programmer
 
Join Date: Jan 2005
Location: Charleston, SC www.wareonearth.com
Posts: 57
Rep Power: 4 JDStud6 is on a distinguished road
Send a message via AIM to JDStud6
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
JDStud6 is offline   Reply With Quote
Old Nov 28th, 2005, 2:27 PM   #25
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 5 groovicus is on a distinguished road
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.
groovicus is offline   Reply With Quote
Old Nov 28th, 2005, 3:11 PM   #26
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
But what if there's more than one mode?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 28th, 2005, 3:35 PM   #27
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 5 groovicus is on a distinguished road
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
groovicus is offline   Reply With Quote
Old Nov 29th, 2005, 11:50 AM   #28
Java|Tera
Newbie
 
Join Date: Nov 2005
Posts: 14
Rep Power: 0 Java|Tera is on a distinguished road
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_Amount

Last edited by Java|Tera; Nov 29th, 2005 at 12:03 PM.
Java|Tera is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:37 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC