Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
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
Old Nov 27th, 2005, 3:04 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
Please don't criticise my code, I am new to it, as I mentioned earlier
Do you mean, not at all, or do it gently, or exactly what? What if something in it is wrong? Is that off limits?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 27th, 2005, 3:13 PM   #3
Java|Tera
Newbie
 
Join Date: Nov 2005
Posts: 14
Rep Power: 0 Java|Tera is on a distinguished road
No no, I mean, don't tell me I'm stupid for doing something wrong..

If something is wrong, please go ahead and tell me.
Java|Tera is offline   Reply With Quote
Old Nov 27th, 2005, 4:02 PM   #4
B3TA_SCR1PT3R
Hobbyist Programmer
 
B3TA_SCR1PT3R's Avatar
 
Join Date: Jul 2005
Location: Dallas, Texas
Posts: 101
Rep Power: 0 B3TA_SCR1PT3R is an unknown quantity at this point
Send a message via AIM to B3TA_SCR1PT3R
can i make a suggestion?

replace:
System.out.println("Enter 7 to find the mode of the random numbers");
System.out.println();

with:
System.out.println("Enter 7 to find the mode of the random numbers\n");
__________________
Hoes telling me to calm down but I'm like fuck that shit!
B3TA_SCR1PT3R is offline   Reply With Quote
Old Nov 27th, 2005, 5:05 PM   #5
Java|Tera
Newbie
 
Join Date: Nov 2005
Posts: 14
Rep Power: 0 Java|Tera is on a distinguished road
Quote:
Originally Posted by B3TA_SCR1PT3R
can i make a suggestion?

replace:
System.out.println("Enter 7 to find the mode of the random numbers");
System.out.println();

with:
System.out.println("Enter 7 to find the mode of the random numbers\n");
What does that do though?
Java|Tera is offline   Reply With Quote
Old Nov 27th, 2005, 5:17 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
"\n" is a newline character. I'm not up on Java, so the suggestion may or may not be valid. If the output buffer isn't guaranteed to be flushed until a new println appears, then you'd want to leave it as you have it, since the last println would guarantee that the previous message is output. My guess is that your advisor is unaware of the possible implications, also.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 27th, 2005, 5:21 PM   #7
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
The same thing, pretty much.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Nov 27th, 2005, 5:22 PM   #8
Java|Tera
Newbie
 
Join Date: Nov 2005
Posts: 14
Rep Power: 0 Java|Tera is on a distinguished road
Ok, but I still don't know how to do mode :\
Java|Tera is offline   Reply With Quote
Old Nov 27th, 2005, 5:29 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I'll look at your code in detail a little later. You know what mode is, right?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 27th, 2005, 6:01 PM   #10
Java|Tera
Newbie
 
Join Date: Nov 2005
Posts: 14
Rep Power: 0 Java|Tera is on a distinguished road
Yes.

Unfortunately it is due in approximately 16 hours.
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 12:36 AM.

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