# Visual C# 2005 GuideOne-Dimensional Arrays

An array is an ordered set of values of a single type. Each element in the array shares the same name but a different index or subscript. This is a number which indicates the position of the element in the array. In C~, the first lement of an array has the subscript, 0.

Copy and compile the following program. This program declares and intialises an array of names.

```string[] name = {"Pinky", "Perky", "Bob", "Bill"}; Console.WriteLine(name); Console.WriteLine(name); Console.ReadLine();```

## Iterating Through An Array

The fact that array elements have the same name and a different index allows us to use loops to process each element of an array.

For example,

```string[] name = {"Pinky", "Perky", "Bob", "Bill"}; for (int i = 0; i < name.Length; i++) {    Console.WriteLine(name[i]); } Console.ReadLine();```

You can also use the foreach statement to do this,

```string[] name = {"Pinky", "Perky", "Bob", "Bill"}; foreach (string i in name) {    Console.WriteLine(i); } Console.ReadLine();```

## BitArrays

A BitArray is a compact array of bits where each element is represented by a boolean value, where true indicates an on bit and false an off bit.

We can use this structure to implement an ancient techinique for finding prime numbers called the Sieve of Eratosthenes.

The principle is that we start with a super huge array of bits. We want the bit to be set to true if the number is prime, false if not. The program starts by declaring the BitArray with all of its elements set to true. The first value is read and, if true, all multiples of that value in the array are set to false, since they cannot be prime. The next number is then read and the process completed until this has been done with all numbers. Anything still set to true is prime.

The program has been written using procedures and functions to keep it tidy. A wee menu type interface is provided for the user. Apart from crashes when incorrect data types are entered, this program runs in a continual loop.

```const int maxNumber = 8000000; static BitArray primeStorage = new BitArray(maxNumber, true); static void Main(string[] args) {    Sieve();    int numCheck;    while (GetMenuChoice() == 1)    {       numCheck = ReadNumber();       CheckNumber(numCheck);    } } static int GetMenuChoice() {    Console.Clear();    Console.WriteLine("Prime Number Menu");    Console.WriteLine();    Console.WriteLine("1. Test A Number");    Console.WriteLine("2. Quit The Program");    Console.WriteLine();    Console.Write("Enter your choice: ");    int choice = System.Convert.ToInt32(Console.ReadLine());    return choice; } static int ReadNumber() {    Console.Clear();    Console.Write("Enter your number: ");    int number1 = System.Convert.ToInt32(Console.ReadLine());    return number1; } static void CheckNumber(int numToCheck) {    if (primeStorage[numToCheck] == true)    {       Console.WriteLine("Number {0} is prime", numToCheck);    }    else    {       Console.WriteLine("Number {0} is not prime", numToCheck);    }    Console.WriteLine();    Console.WriteLine("Press Enter");    Console.ReadLine(); } static void Sieve() {    int index = 1;    while (index < (maxNumber - 1))    {       index += 1;       if (primeStorage[index] == true)       {          for (int counter = index * 2; counter < maxNumber; counter += index)          {             primeStorage[counter] = false;          }       }    } }```

## Having A Go

1. Initialise an array of 10 names. Output the array to the screen in reverse order.
2. Declare an array of 10 integers. Allow the user to input the values they want. Output the sum, largest, smallest and mean values from their list.
3. Declare an array of 7 integers. Initialise each element to 0. Use a loop and random numbers to simulate the rolling of a single die. If a 1 is rolled, increase the value of element 1 in the array by 1, if a 2 is rolled, increase the value of element 2 by 1. When all 200 rolls have been simulated, output the tally to the console.
4. Write a program to choose 100 random numbers between 1 and 100. Count and display how many numbers are in the range 1-10, 11-20, 21-30 etc.