2 dimensional array??
I have this 2-d array here and I need to get my program to add rows(which would be tests. I would like to be able to enter the amount of tests that i would like to output using the code i have(so if i only wanted test 1 i would enter 1 and so on) Does anyone know a good website that illistrates something like this or any ideas on how i could do this. Thanks.
public class Grades
{
static public void Main ()
{
DateTime now = DateTime.Now;
Random rand = new Random ((int) now.Millisecond);
int [,] Grades = new int [5,10];
for (int x = 0; x < Grades.GetLength (0); ++x)
{
for (int y = 0; y < Grades.GetLength(1); ++y)
{
Grades [x, y] = 70 + rand.Next () % 31;
}
}
int [] Average = new int [10];
Console.WriteLine ("Grade summary:\r\n");
Console.WriteLine ("Student 1 2 3 4 5 6 7 8 9 10");
Console.WriteLine (" ------------");
for (int x = 0; x < Grades.GetLength (0); ++x)
{
Console.Write ("Test " + (x + 1) + " ");
for (int y = 0; y < Grades.GetLength(1); ++y)
{
Average[y] += Grades[x,y];
Console.Write ("{0,4:D}", Grades[x,y]);
}
Console.WriteLine ();
}
Console.Write ("\r\n Avg. ");
foreach (int Avg in Average)
{
Console.Write ("{0,4:D}", Avg / Grades.GetLength(0));
}
Console.WriteLine ();
}
}
[1883 byte] By [
ryno3185] at [2007-11-20 11:35:39]

# 1 Re: 2 dimensional array??
If you know the dimensions before you enter that function you can just pass the dimensions are variables.
Instead of:
int [,] Grades = new int [5,10];
Use:
int [,] Grades = new int [Width, Height];
If you need to dynamically resize the table, just use lists of lists:
List<List<int>> Grades = new List<List<int>>();
Each time you want to create a new now then it would be:
Grades.Add(new List<int>());
You can still reference the set as an array like:
Value = Grade[X][Y];
Just remember to add a new grade you need to use list.add() unless you pre-pad the list with empty entries.
So to add a new grade, 80 to row 0, it would be:
Grade[0].Add(80);
DeepT at 2007-11-9 11:36:48 >
