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.
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
}
}
}
Re: Issue with filling array full of random values
Quote:
S.O.P command in the for loop is not outputting anything
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.
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.
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.
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.
Re: Issue with filling array full of random values
Quote:
it is just not working.
Print out the value of the expression in the for loop that controls when the loop executes. You called it:
my implementation, my test and my modifier