Results 1 to 17 of 17
- 03-05-2011, 05:02 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
Random Numbers - finding least value
Very, very new to Java and :confused:..Have code which generates random array of 100 numbers 0-9. It displays random numbers and also count of each random numbers. Last method which I am having major problem "should find least frequent value" and display it based on System.out.print. Been working on this over a week and now when I look at the code nothing make sense anymore. Can some one please help and make sense of what I am majorly missing..thank you in advance:
public class Program6c
{
public static void main(String[] args)
{
//int[]min =leastFrequent;
//DisplayLeast(min);
//declaring array and creating array
int[]numbers = createArray();
//array display
System.out.println("The 100 random numbers are: ");
System.out.println("-----------------------------");
displayArray(numbers);
//count the values of numbers
int []counts = countNumbers(numbers);
//display counts
System.out.println( " ");
System.out.println( "The occurence of each value is:");
System.out.println("-----------------------------");
displayCounts(counts);
}
//creating array of numbers
public static int[] createArray()
{
//declaring and creating an array of 100 random numbers
int[] numbers = new int[100];
//creating random numbers from 1 to 10 and assigning them to the array
for (int i = 0; i < numbers.length; i++)
numbers[i] = (int)(Math.random() * 10);
// returning the array
return numbers;
}
//display array of numbers
public static void displayArray(int[] numbers)
{
// displaying numbers per line
for (int i = 0; i < numbers.length; i++)
{
System.out.println("Random Number ["+ (i+1)+ "]: " + numbers[i]);
}
}
// counting the occurrences/total of each random number
public static int []countNumbers(int[]numbers)
{
// declaring and creating an array of 10 int
int[] counts = new int[10];
//for every number in the array count it
for (int i = 0; i < numbers.length; i++)
{
counts [numbers [i]]++;
}
return counts;
}
// display counts
public static void displayCounts(int[] counts)
{
for (int i = 0; i <= 9; i++)
{
System.out.println( i + " | " + counts[i]);
}
}
//method to find least frequent value
public static int []leastFrequent(int []value)
{
int min = 0;
for(i=1;i <counts.length;i++)
{
if (counts[i]<counts[min])
min=i;
}
return min;
System.out.print("Least frequent random value was" + min +
"which occured" + counts[min] + "times");
}
}
- 03-05-2011, 05:23 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Does this compile? If not, and you can't understand the compiler's message, post the entire message.
Think about and describe precisely what the leastFrequent() method is supposed to do. Does the method find something and if so what value is returned (the least frequent value or its index). Or does it display something (in which case it should have a different name). No, it shouldn't do both as that would be (and is) confusing. And what array is the method passed - an array of values or an array of frequencies? It will need to be passed everything it needs to do whatever it is that you want it to do.
----------------------------
When you post code use the code tags: put [code] at the start of the code and [/code] at the end that way the indenting will be preserved and the result is readable.Last edited by pbrockway2; 03-05-2011 at 05:25 AM.
- 03-05-2011, 07:03 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
Ok, I think I understand what you are saying..leastFrequent is suppose to return a value occurence and how many times. In example below the #8 is the least amount 5x it came up so I need these 2 values. Then the next method would tell me what indexs these 5 '8's showed up. (i.e. 8, 33, 52....)
The occurrence of each value is:
-----------------------------
0 | 13
1 | 11
2 | 8
3 | 14
4 | 8
5 | 10
6 | 8
7 | 14
8 | 5 ...so value 8 occurred 5x radomly
9 | 9
I guess this is where the problem is: not sure how to break these 2 requirements.
- 03-05-2011, 09:25 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
What are the compiler messages?
(Basically I think your approach of finding the index of the least value in the counts array is correct, but for it to become runnable code you will have to deal with those messages and that might involve changing other things in the program.)
- 03-05-2011, 11:00 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
Least value of random intergers
Ok, I have changed the 3rd part to find the index with the lowest counts value which I will store (use for the next method) and print info. Then create a new method which tells me what index on the numbers array the count (8's)located. Based on the code I have below when compiled getting "unreachable statement & missing return statement" errors.
Java Code:// declaring and creating an array int[] counts = new int[10]; //for every number in the array count it for (int i = 0; i < numbers.length; i++) { counts [numbers [i]]++; } return counts; } // display counts public static void displayCounts(int[] counts) { for (int i = 0; i <= 9; i++) { System.out.println( i + " | " + counts[i]); } } //method to find least frequent value public static int leastValue(int [] counts, int []min) { int lowestIndex = 0; for(int i =1;i <counts.length;i++) { if (counts[i] <counts[lowestIndex]) lowestIndex=i; } return lowestIndex; System.out.print("Lowest number: " + counts[lowestIndex] + " is at " + lowestIndex);
- 03-05-2011, 11:17 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
It's a good idea to copy and post the exact and entire compiler message.
-------------------------
The leastValue() method is getting the counts array (rather than the values array) and that's a very Good Thing.
(1) The "unreachable statement" message is getting at the fact the System.out.println() stuff is trying to happen after the return statement. And clearly you can't do that: once you've returned, you've returned. Put this message aside for just a moment.
(2) The "missing return statement" occurs - I'm guessing - when you take the return line out in an effort to get rid of the "unreachable statement" problem. But think about it: if you intend this method to print out the results of its findings, need it return anything at all?
If it is a method for displaying, try this:
* Rename the method to display to displayLeastFrequent() so that it accurately reflects what it does.
* Get rid of the min argument. It isn't used, so it shouldn't be there.
* Declare the method as returning void because it is a displaying method and...
* remove the return statement.
This method will then be properly declared to do what you intend it ultimately to do. There will be other problems that the compiler alerts you to. If you can't see your way through them remember to post the exact and entire message along with the code that it refers to.
- 03-05-2011, 11:21 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
By the way, I think you can do everything you want within this method. Ie you don't need another. The index that you find is the least frequent number, and counts[] at that index is the frequency of that number.
- 03-06-2011, 12:31 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
I'm sorry but I am totally confused now. I guess I have worked on this too long. This is what the code looks like now. I have compiled it and I get no compile errors, but all I get are the list of 100 random #'s and occurences of each value. I need a method to find the least count and method to display the locations of of those values in the array of 100.
[code]
public class RanNumbers
{
public static void main(String[] args)
{
//declaring array and creating array
int[]numbers = createArray();
//array display
System.out.println("The 100 random numbers are: ");
System.out.println("-----------------------------");
displayArray(numbers);
//count the values of numbers
int []counts = countNumbers(numbers);
//display counts
System.out.println( " ");
System.out.println( "The occurence of each value is:");
System.out.println("-----------------------------");
displayCounts(counts);
//int[] lowestIndex = leastValue(min);
}
//creating array of numbers
public static int[] createArray()
{
//declaring and creating an array of 100 random numbers
int[] numbers = new int[100];
//creating random numbers from 1 to 10 and assigning them to the array
for (int i = 0; i < numbers.length; i++)
numbers[i] = (int)(Math.random() * 10);
// returning the array
return numbers;
}
//display array of numbers
public static void displayArray(int[] numbers)
{
// displaying numbers per line
for (int i = 0; i < numbers.length; i++)
{
System.out.println("Random Number ["+ (i+1)+ "]: " + numbers[i]);
}
}
// counting the occurrences/total of each random number
public static int []countNumbers(int[]numbers)
{
// declaring and creating an array
int[] counts = new int[10];
//for every number in the array count it
for (int i = 0; i < numbers.length; i++)
{
counts [numbers [i]]++;
}
return counts;
}
// display counts
public static void displayCounts(int[] counts)
{
for (int i = 0; i <= 9; i++)
{
System.out.println( i + " | " + counts[i]);
}
}
//method to find least frequent value
public static void leastFrequent(int[]counts)
{
int lowestIndex = 0;
for(int i =1;i <counts.length;i++)
{
if (counts[i] <counts[lowestIndex])
lowestIndex=i;
}
System.out.print("Lowest number: " + counts[lowestIndex]
+ " is at " + lowestIndex);
}
}
- 03-06-2011, 12:31 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
Java Code:public class Program6a { public static void main(String[] args) { //declaring array and creating array int[]numbers = createArray(); //array display System.out.println("The 100 random numbers are: "); System.out.println("-----------------------------"); displayArray(numbers); //count the values of numbers int []counts = countNumbers(numbers); //display counts System.out.println( " "); System.out.println( "The occurence of each value is:"); System.out.println("-----------------------------"); displayCounts(counts); // int[] lowestIndex = leastValue(min); } //creating array of numbers public static int[] createArray() { //declaring and creating an array of 100 random numbers int[] numbers = new int[100]; //creating random numbers from 1 to 10 and assigning them to the array for (int i = 0; i < numbers.length; i++) numbers[i] = (int)(Math.random() * 10); // returning the array return numbers; } //display array of numbers public static void displayArray(int[] numbers) { // displaying numbers per line for (int i = 0; i < numbers.length; i++) { System.out.println("Random Number ["+ (i+1)+ "]: " + numbers[i]); } } // counting the occurrences/total of each random number public static int []countNumbers(int[]numbers) { // declaring and creating an array int[] counts = new int[10]; //for every number in the array count it for (int i = 0; i < numbers.length; i++) { counts [numbers [i]]++; } return counts; } // display counts public static void displayCounts(int[] counts) { for (int i = 0; i <= 9; i++) { System.out.println( i + " | " + counts[i]); } } //method to find least frequent value public static void leastFrequent(int[]counts) { int lowestIndex = 0; for(int i =1;i <counts.length;i++) { if (counts[i] <counts[lowestIndex]) lowestIndex=i; } System.out.print("Lowest number: " + counts[lowestIndex] + " is at " + lowestIndex); } }
- 03-06-2011, 12:44 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Are these the same program?
Anyway you may have been working on it too long. What you have now works for me ... once I actually call the leastFrequent() method!
Java Code:public class RanNumbers { public static void main(String[] args) { // declaring array and creating array int[] numbers = createArray(); // array display System.out.println("The 100 random numbers are: "); System.out.println("-----------------------------"); displayArray(numbers); // count the values of numbers int[] counts = countNumbers(numbers); // display counts System.out.println(" "); System.out.println("The occurence of each value is:"); System.out.println("-----------------------------"); displayCounts(counts); // int[] lowestIndex = leastValue(min); [b]leastFrequent(counts);[/b] } // creating array of numbers public static int[] createArray() { // declaring and creating an array of 100 random numbers int[] numbers = new int[100]; // creating random numbers from 1 to 10 and assigning them to the array for (int i = 0; i < numbers.length; i++) numbers[i] = (int) (Math.random() * 10); // returning the array return numbers; } // display array of numbers public static void displayArray(int[] numbers) { // displaying numbers per line for (int i = 0; i < numbers.length; i++) { System.out .println("Random Number [" + (i + 1) + "]: " + numbers[i]); } } // counting the occurrences/total of each random number public static int[] countNumbers(int[] numbers) { // declaring and creating an array int[] counts = new int[10]; // for every number in the array count it for (int i = 0; i < numbers.length; i++) { counts[numbers[i]]++; } return counts; } // display counts public static void displayCounts(int[] counts) { for (int i = 0; i <= 9; i++) { System.out.println(i + " | " + counts[i]); } } // method to find least frequent value public static void leastFrequent(int[] counts) { int lowestIndex = 0; for (int i = 1; i < counts.length; i++) { if (counts[i] < counts[lowestIndex]) lowestIndex = i; } System.out.print("Lowest number: " + counts[lowestIndex] + " is at " + lowestIndex); } }
The output is
Java Code:The 100 random numbers are: ----------------------------- Random Number [1]: 4 Random Number [2]: 7 Random Number [3]: 8 Random Number [4]: 8 Random Number [5]: 1 ... etc (snipped) Random Number [97]: 9 Random Number [98]: 8 Random Number [99]: 9 Random Number [100]: 4 The occurence of each value is: ----------------------------- 0 | 9 1 | 13 2 | 12 3 | 7 4 | 11 5 | 10 6 | 11 7 | 9 8 | 10 9 | 8 Lowest number: 7 is at 3
You will notice that the message at the end is "wrong", but it has all the right bits. It should say something like "The least frequent value is 3 with a frequency of 7"
- 03-06-2011, 02:21 AM #11
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
I am so close to completing this and @18-20 hrs (maybe more) on this is too much. I added the last method which needs to display all the indexes from the numbers array that the lowest value is stored. I am receiving 2 errors 'illegal start of expression & reached end of file while parsing" (bottom of code is error as noted in compiler). I Googled the meanings and where it could be in my code and I am not finding why I am receiving these 2 errors.
Incidentally - pbrockway2 your help & explanations have been the most help I have received including googling. Thank you soo much...
Java Code:public class Program6a { public static void main(String[] args) { //declaring array and creating array int[]numbers = createArray(); //array display System.out.println("The 100 random numbers are: "); System.out.println("-----------------------------"); displayArray(numbers); //count the values of numbers int []counts = countNumbers(numbers); //display counts System.out.println( " "); System.out.println( "The occurence of each value is:"); System.out.println("-----------------------------"); displayCounts(counts); leastFrequent(counts); displayLocation(numbers); } //creating array of numbers public static int[] createArray() { //declaring and creating an array of 100 random numbers int[] numbers = new int[100]; //creating random numbers from 1 to 10 and assigning them to the array for (int i = 0; i < numbers.length; i++) numbers[i] = (int)(Math.random() * 10); // returning the array return numbers; } //display array of numbers public static void displayArray(int[] numbers) { // displaying numbers per line for (int i = 0; i < numbers.length; i++) { System.out.println("Random Number ["+ (i+1)+ "]: " + numbers[i]); } } // counting the occurrences/total of each random number public static int []countNumbers(int[]numbers) { // declaring and creating an array int[] counts = new int[10]; //for every number in the array count it for (int i = 0; i < numbers.length; i++) { counts [numbers [i]]++; } return counts; } // display counts public static void displayCounts(int[] counts) { for (int i = 0; i <= 9; i++) { System.out.println( i + " | " + counts[i]); } } //method to find least frequent value public static void leastFrequent(int[]counts) { int lowestIndex = 0; for(int i =1;i <counts.length;i++) { if (counts[i] <counts[lowestIndex]) lowestIndex=i; } System.out.println(); System.out.print("The least frequent count is " + counts[lowestIndex] + " for the value of " + lowestIndex); } //method to display what index least frequent values are located in array public static void displayLocation (int[] numbers) { for(int i=0; i < numbers.length; i++) { if (numbers[i]==lowestIndex) } ////////getting a "illegal start of expression line 103" error System.out.print(); System.out.println(i); } } /////receiving a "reached end of file while parsing line 107" error
- 03-06-2011, 03:17 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
The braces are all over the place towards the end. They should come in matching pairs:
---------Java Code:public static void displayLocation (int[] numbers) [color=red][b]{[/b][/color] for(int i=0; i < numbers.length; i++) [color=green][b]{[/b][/color] if (numbers[i]==lowestIndex) [color=blue][b]{[/b][/color] System.out.print(); System.out.println(i); [color=blue][b]}[/b][/color] [color=green][b]}[/b][/color] [color=red][b]}[/b][/color]
I said before that you didn't need to return anything from the method that found the lowest frequency. However I didn't realise that you were going to go on and find all locations of this least-frequent-number.
If you get the brackets right you will notice that the compiler grumbles that it doesn't know about leastIndex. In fact you will have to have the leastFrequent() method return this value - to main() - and then passit to the displayLocation() method as an argument.
- 03-06-2011, 03:18 AM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Also, what is
Java Code:System.out.print();
supposed to do? You should be able to figure out how to do that properly.
- 03-06-2011, 04:42 AM #14
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
I'm so sorry, I did not communicate clearly what I was trying to do. systems.out.print() was more of just adding an extra line which is not important. I have re-written the code with return value for 2nd to the last method and also last method. I am getting 3 errors when compile:
leastFrequent(int[],int[]) in Program6a cannot be applied to (int[]) -line 28
displayLocation(int[],int) in Program6a cannot be applied to (int[],int[]) -line 29
incompatible types-92
Not sure if I forgot something, did not add/delete something anymore..too many versions. As you can tell I am beginner of Java big time.
Java Code:public class Program6a { public static void main(String[] args) { //declaring array and creating array int[]numbers = createArray(); //array display System.out.println("The 100 random numbers are: "); System.out.println("-----------------------------"); displayArray(numbers); //count the values of numbers int []counts = countNumbers(numbers); //display counts System.out.println( " "); System.out.println( "The occurence of each value is:"); System.out.println("-----------------------------"); displayCounts(counts); int []min = leastFrequent(counts); displayLocation(numbers, min); } //creating array of numbers public static int[] createArray() { //declaring and creating an array of 100 random numbers int[] numbers = new int[100]; //creating random numbers from 1 to 10 and assigning them to the array for (int i = 0; i < numbers.length; i++) numbers[i] = (int)(Math.random() * 10); // returning the array return numbers; } //display array of numbers public static void displayArray(int[] numbers) { // displaying numbers per line for (int i = 0; i < numbers.length; i++) { System.out.println("Random Number ["+ (i+1)+ "]: " + numbers[i]); } } // counting the occurrences/total of each random number public static int []countNumbers(int[]numbers) { // declaring and creating an array int[] counts = new int[10]; //for every number in the array count it for (int i = 0; i < numbers.length; i++) { counts [numbers [i]]++; } return counts; } // display counts public static void displayCounts(int[] counts) { for (int i = 0; i <= 9; i++) { System.out.println( i + " | " + counts[i]); } } //method to find least frequent value public static int []leastFrequent(int[]counts,int[]min ) { int lowestIndex = 0; for(int i =1;i <counts.length;i++) { if (counts[i] <counts[lowestIndex]) lowestIndex=i; } return lowestIndex; { System.out.println(); System.out.print("The least frequent count is " + counts[lowestIndex] + " for the value of " + lowestIndex); } } //method to display what index least frequent values are located in array public static void displayLocation (int[] numbers,int min) { for(int i=0; i < numbers.length; i++) { if (numbers[i]==min) { System.out.println(i); } } } }
- 03-06-2011, 05:26 AM #15
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Take your time with the compiler messages: they are intelligible and they are helpful. (Although you may not be completely convinced of either.)
leastFrequent(int[],int[]) in Program6a cannot be applied to (int[]) -line 28
In main() you are saying "leastFrequent(counts);" but the leastFrequent() method is declared as having two array arguments.
displayLocation(int[],int) in Program6a cannot be applied to (int[],int[]) -line 29
Again you send displayLocation() two arrays as arguments, but it is declared to accept an array and an int.
incompatible types-92
Your leastFrequent() method is returning an int (lowestIndex) but it is declared to return an array.
---------------------------
In every case there is a mismatch between how you declare a method and how you use it. Make sure you understand this point. A method must be called with the same number and same same type of arguments as it declares. It must return exactly the type of thing it is declared to return.
In each case you have a choice: either change how the method is declared, or change how you use it. Don't imagine that you can guess which to do. Or that you can apply a stochastic process of changing the type of things at random until it wroks: there are just too many combinations and that will be random walk to nowhere!
Instead think:
* What information does leastFrequent need to determine the least frequent value? The arguments should be the things it needs. Currently you declare it to need counts and min but you are only sending it counts.
* What information should leastFrequent return? The declared return type should reflect this. Currently it is declared to return an array, but it actually returning just an int (lowestIndex). So which is it: are you going to later want a whole array? or is the single int lowestIndex enough for you to later determine the positions?
* What information does displayLocation() require? It needs the numbers array, but does it need the single int min to go looking for, or does it need an entire array? You are sending it an array, but it is only declared to need an int.
---------------------------------
Declarations and usage must match up.
Java Code:// in main int[] counts = countNumbers(numbers); // etc [color=green][b]int[] min[/b][/color] = leastFrequent([color=red][b]counts[/b][/color]); displayLocation(numbers, [color=green][b]min[/b][/color]); } // later... public static [color=green][b]int[][/b][/color] leastFrequent([color=red][b]int[]counts,int[]min[/b][/color]) { [color=green][b]int[/b][/color] lowestIndex = 0; // etc return lowestIndex; } public static void displayLocation (int[] numbers,[color=green][b]int min[/b][/color]) { // etc }
- 03-06-2011, 10:17 PM #16
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
I finally got it based on you noted. Had to walk away and think about what I was doing, but it looks great. Thank you for all your help.... :)
The 100 random numbers are:
-----------------------------
Random Number [1]: 6
Random Number [2]: 5
Random Number [3]: 1
Random Number [4]: 7
Random Number [5]: 6
Random Number [6]: 5
Random Number [7]: 6
Random Number [8]: 7
Random Number [9]: 1
Random Number [10]: 0
Random Number [11]: 5
Random Number [12]: 4
Random Number [13]: 7
Random Number [14]: 4
Random Number [15]: 3
Random Number [16]: 8
Random Number [17]: 0
Random Number [18]: 2
Random Number [19]: 5
Random Number [20]: 6
Random Number [21]: 5
Random Number [22]: 5
...etc
Random Number [98]: 4
Random Number [99]: 6
Random Number [100]: 3
The occurence of each value is:
-----------------------------
0 | 12
1 | 9
2 | 5
3 | 10
4 | 9
5 | 12
6 | 15
7 | 11
8 | 10
9 | 7
The least frequent count is 5 for the value of 2
Index locations:
17
37
47
56
86
Process completed.
- 03-08-2011, 12:34 AM #17
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Similar Threads
-
Problem getting numbers from user and finding smallest two numbers
By radhi16 in forum New To JavaReplies: 11Last Post: 01-14-2011, 06:36 PM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
finding the largest k numbers in an array without sorting the whole list?
By jmmjm in forum Advanced JavaReplies: 5Last Post: 02-07-2009, 07:48 AM -
Random numbers
By jithan in forum Advanced JavaReplies: 3Last Post: 06-14-2008, 02:04 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks