I would like to apply the values of 2 int variables, entered by the user, to a 2 dimensional array. My problem is in understanding how to cause the 2 ints to create the array dynamically, if the user enters 3 for int A, and 5 for int B, then the array will consist of 3 rows, and 5 columns.
I would then create a class to generate random numbers that will fill the array with a minValue and a maxValue. I'm not sure how to begin with the algorithm, and how to establish the relationship between the array and the random number generation. I believe the code below will create a random array, but not dynamic, using user input?
Thanks!
public static void GenRanArray(int nbOfRows,
int nbOfCols,
int [,] array)
{
const int MIN_NUMBER = 1;
const int MAX_NUMBER = 99;
// ---------------------------------------------
// Declare the randomNumber object: randomNumber
Random randomNumber = new Random();
// -----------------------------------
int row;
int col;
for (row = 0; row < nbOfRows; row++)
for (col = 0; col < nbOfCols; col++)
array[row, col] = randomNumber.Next(MIN_NUMBER,
MAX_NUMBER);
} // method GenRanArray
// ===============================
// ============================================
public static void OutputArray(int nbOfRows,
int nbOfCols,
int [,] array)
{
int row;
int col;
for (row = 0; row < nbOfRows; row++)
for (col = 0; col < nbOfCols; col++)
Console.WriteLine("array[" + row + ", " + col + "] = "
+ array[row, col]);
Console.WriteLine();
} // method GenerateRandomArray
// ===============================
} // class MyMethods
} // namespace Arrays2D
This post has been edited by PsychoCoder: 15 October 2007 - 10:22 AM

New Topic/Question
Reply



MultiQuote






|