Difference between revisions of "Arrays - 2017"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
An array is a list consisting of one and only one data type, such as string or int. Each element in an array is assigned an integer subscript as an order to the list for when each element has been added.
 
An array is a list consisting of one and only one data type, such as string or int. Each element in an array is assigned an integer subscript as an order to the list for when each element has been added.
 +
Therefore, if you want a specific element to be declared, you can declare what is in the ''n''th position with
 +
<syntaxhighlight lang="csharp" line> Console.WriteLine(array[n]) </syntaxhighlight> assuming ''n'' is less than or equal to the number of elements in the array.
 +
 +
By default, an array is one dimensional. More dimensions can be added by declaring the array with a comma within the square brackets, e.g.
 +
<syntaxhighlight lang="csharp" line> array[,] -> array[x, y]
 +
array[,,] -> array[x, y, z]</syntaxhighlight> for two-dimensional and three-dimensional arrays respectively, with additional commas per additional dimension.
 +
 +
An example of arrays used in the Skeleton Program is as follows under the variable "Rabbit";
  
 
<syntaxhighlight lang="csharp" line>  class Warren
 
<syntaxhighlight lang="csharp" line>  class Warren
 
   {
 
   {
 
     private const int MaxRabbitsInWarren = 99;
 
     private const int MaxRabbitsInWarren = 99;
     private Rabbit[] Rabbits;
+
 
 +
     //private Rabbit[] Rabbits;
 +
 
 
     private int RabbitCount = 0;
 
     private int RabbitCount = 0;
 
     private int PeriodsRun = 0;
 
     private int PeriodsRun = 0;
Line 14: Line 24:
 
     {
 
     {
 
       this.Variability = Variability;
 
       this.Variability = Variability;
       Rabbits = new Rabbit[MaxRabbitsInWarren];
+
 
 +
       //Rabbits = new Rabbit[MaxRabbitsInWarren];
 +
 
 
       RabbitCount = (int)(CalculateRandomValue((int)(MaxRabbitsInWarren / 4), this.Variability));
 
       RabbitCount = (int)(CalculateRandomValue((int)(MaxRabbitsInWarren / 4), this.Variability));
 
       for (int r = 0; r < RabbitCount; r++)
 
       for (int r = 0; r < RabbitCount; r++)
 
       {
 
       {
         Rabbits[r] = new Rabbit(Variability);
+
 
 +
         //Rabbits[r] = new Rabbit(Variability);
 +
 
 
       }
 
       }
 
     }
 
     }
Line 26: Line 40:
 
       this.Variability = Variability;
 
       this.Variability = Variability;
 
       this.RabbitCount = rabbitCount;
 
       this.RabbitCount = rabbitCount;
       Rabbits = new Rabbit[MaxRabbitsInWarren];
+
 
 +
       //Rabbits = new Rabbit[MaxRabbitsInWarren];
 +
 
 
       for (int r = 0; r < RabbitCount; r++)
 
       for (int r = 0; r < RabbitCount; r++)
 
       {
 
       {
         Rabbits[r] = new Rabbit(Variability);
+
 
 +
         //Rabbits[r] = new Rabbit(Variability);
 +
 
 
       }
 
       }
 
     }
 
     }
 
   }
 
   }
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
In this case, ''n'' is declared as '''r'''.

Latest revision as of 14:24, 6 February 2017

An array is a list consisting of one and only one data type, such as string or int. Each element in an array is assigned an integer subscript as an order to the list for when each element has been added. Therefore, if you want a specific element to be declared, you can declare what is in the nth position with

1  Console.WriteLine(array[n])

assuming n is less than or equal to the number of elements in the array.

By default, an array is one dimensional. More dimensions can be added by declaring the array with a comma within the square brackets, e.g.

1  array[,] -> array[x, y]
2 array[,,] -> array[x, y, z]

for two-dimensional and three-dimensional arrays respectively, with additional commas per additional dimension.

An example of arrays used in the Skeleton Program is as follows under the variable "Rabbit";

 1   class Warren
 2   {
 3     private const int MaxRabbitsInWarren = 99;
 4 
 5     //private Rabbit[] Rabbits;
 6 
 7     private int RabbitCount = 0;
 8     private int PeriodsRun = 0;
 9     private bool AlreadySpread = false;
10     private int Variability;
11     private static Random Rnd = new Random();
12 
13     public Warren(int Variability)
14     {
15       this.Variability = Variability;
16 
17       //Rabbits = new Rabbit[MaxRabbitsInWarren];
18 
19       RabbitCount = (int)(CalculateRandomValue((int)(MaxRabbitsInWarren / 4), this.Variability));
20       for (int r = 0; r < RabbitCount; r++)
21       {
22 
23         //Rabbits[r] = new Rabbit(Variability);
24 
25       }
26     }
27 
28     public Warren(int Variability, int rabbitCount)
29     {
30       this.Variability = Variability;
31       this.RabbitCount = rabbitCount;
32 
33       //Rabbits = new Rabbit[MaxRabbitsInWarren];
34 
35       for (int r = 0; r < RabbitCount; r++)
36       {
37 
38         //Rabbits[r] = new Rabbit(Variability);
39 
40       }
41     }
42   }

In this case, n is declared as r.