Results 1 to 6 of 6
- 05-19-2012, 11:05 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 31
- Rep Power
- 0
Issue with filling array full of random values
I feel like a silly, as I have done this before and it worked fine, but I am trying to fill an array with 10 random values so that I can implement a selection sort on it. I have all my code as I think it should be, but by placing various test lines in my code, it appears that it is not actually putting the values in the array. Am I missing something obvious, or am I just plain doing it wrong?
I am attempting to use a for loop to count through the array and place a value between 1 and 50 in it, however a S.O.P command in the for loop is not outputting anything. Furthermore, when I run the code, it only outputs "test" 10 times to console, implying that it is successfully calling the selection sort method, while not successfully calling the output for loop directly below the call to the selection sort.
Java Code:import java.util.Random; public class J2_Lab_7 { static int[] num = new int[10]; static Random generator = new Random(); public static void main (String[] args) { for (int i=0;i>num.length;i++) { num[i]=generator.nextInt(50)+1; System.out.println(num[i]); //this should be outputing the values of num[] but it is not?? } SelectionSort(num); for (int j=0; j>num.length; j++) { System.out.println (num[j]); System.out.println("test2");//this is not being called successfully } } public static void SelectionSort(int[] number) { int i, j, first, temp; for (i = number.length-1; i>0; i--) { first = 0; //initialize to subscript of first element for(j = 1; j<=i; j++) //locate smallest element between positions 1 and i. { if( number[j] < number[first] ) first = j; } temp = number[first]; //swap smallest found with element in position i. number[first] = number[i]; number[i] = temp; System.out.println("test");//this is being called successfully } } }
- 05-19-2012, 11:14 PM #2
Re: Issue with filling array full of random values
If the for loops are not being executed, there must be something wrong with the way they are coded. Check that they are coded correctly.S.O.P command in the for loop is not outputting anythingIf you don't understand my response, don't ignore it, ask a question.
- 05-19-2012, 11:21 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 31
- Rep Power
- 0
Re: Issue with filling array full of random values
As I understand for loops it is correct, I have my implementation, my test and my modifier, and they are calling an array that does exist. Furthermore, It is not getting any errors
Also just to be sure, I put a test S.O.P line in one of the for loops in the SelectionSort method, and it worked fine, and it is structured identically to the for loop that appears to not be working.
- 05-19-2012, 11:49 PM #4
Re: Issue with filling array full of random values
Print out the value of the expression that is used to control the execution of the for loop. It needs to be true for the loop to execute.
If you don't understand my response, don't ignore it, ask a question.
- 05-20-2012, 12:43 AM #5
Member
- Join Date
- Feb 2012
- Posts
- 31
- Rep Power
- 0
Re: Issue with filling array full of random values
I am not sure what you mean, it is just a for loop that starts with i at zero and goes to the length of the array num[] to start with, the array is set to ten values long, and with no data in it when it is instantiated, and it is meant to fill the array with values, it is just not working.
- 05-20-2012, 01:04 AM #6
Similar Threads
-
Issue with setting values in an array
By pyat77 in forum New To JavaReplies: 4Last Post: 11-04-2011, 02:01 AM -
Algorithm for filling random polygons
By theraccoon in forum Java 2DReplies: 3Last Post: 05-18-2011, 06:35 PM -
problem filling list with values from CSV feed
By shy_ted in forum New To JavaReplies: 7Last Post: 10-12-2010, 10:19 AM -
enter array values to random access file and search for maximum value
By sandraW in forum New To JavaReplies: 6Last Post: 08-26-2010, 02:26 PM -
Filling 2D Array
By Nakira in forum New To JavaReplies: 3Last Post: 11-12-2008, 12:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks