this is my code:
public void RandomNumberGenerator(int lower, int upper){
Random rnd = new Random(); int[,] arrdegree = new int[21, 6]; for (int i = 1; i < 21; i++){
for (int j = 1; j < 6; j++){
arrdegree[i, j] = rnd.Next(10, 99);
Console.WriteLine("Student" + "" + i + " , Subject" + j + "= " + arrdegree[i, j]);}
}
}
i need to sort the values of the random generator in the myarray.................please help

Ahmed SolimanPosted Feb 11, 2008, 6:21 PM
Ryan AlfordPosted Feb 11, 2008, 3:25 PM
AlanPosted Feb 11, 2008, 11:56 AM
Hi Ahmed,
It depends really on what basis, and in which order, you want to sort the elements of the array but I've modified your method to sort in the simplest possible way for each student, namely by marks for each subject in ascending order but without keeping information on which mark relates to which subject, which may or may not be important to what you're doing.
Before coding this, I made the following modifications to your method:
1. As array indexing in C# always starts at element 0 (rather than 1), I've changed the for loops accordingly. Otherwise the elements at index 0 would be given the default value of 0 for ints.
2. The Random.Next(a, b) method actually produces a random number greater than or equal to 'a' but less than 'b'. So, if you want a number between 10 and 99 inclusive you want to use Random.Next(10, 100).
3. You haven't actually used your parameters 'lower' and 'upper' so I've assumed these refer to the minimum and maximum marks.
public void RandomNumberGenerator(int lower, int upper)
{
Random rnd = new Random();
int[,] arrdegree = new int[21, 6];
int[] temp = new int[6];
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 5; j++)
{
arrdegree[i, j] = rnd.Next(lower, upper + 1);
temp[j] = arrdegree[i,j];
}
Array.Sort(temp);
for (int j = 0; j < 5; j++)
{
arrdegree[i, j] = temp[j];
Console.WriteLine("Student" + "" + (i + 1) + " , Subject" + (j + 1)
+ "= " + arrdegree[i, j]);
}
}
}