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[0]);
Console.WriteLine(name[1]);
Console.ReadLine();
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();
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;
}
}
}
}