One Dimensional Array
One Dimensional Array
is a variable that can store multiple values with the same data type.
this values are called elements
array’s elements in C# are numbered with 0, 1, 2, … N-1 (N represent the size/length of
array) ; those numbers are called indices.
Arrays can be in different dimensions, but the most used is the one dimensional array
For example,
Where:
To illustrate:
Ten memory locations are allocated for array num. num can only accept integer values. num has a size
of 10 with indices from 0 to 9.
Another example,
Where:
If array declared with initialization, the number of elements determine the size of the array
Each value is assigned to each location. Assignment is done by position which means, 1 is assigned to
index 0, 2 to index 1, 3 to index 0 and so on.
To Illustrate:
The above example is similar to the previous one. 1 is assigned to index 0, 2 to index 1 and 3 to index 2.
To Illustrate:
Each string is assigned to each location. Assignment is done by position which means, “Monday” is
assigned to index 0, “Tuesday” to index 1 and so on.
To Illustrate:
Access to the Elements of an Array
Each element can be accessed through the name of the array and the element’s index
(consecutive number) placed in the brackets.
int x;
values[0] = 3;
values[1] = 2;
values[2] = 4;
values[3] = 12;
values[4] = 20;
To Illustrate:
Boundaries of an Array
Arrays are by default zero-based, which means the enumeration of the elements starts from 0.
The first element has the index 0, the second – 1,etc.
In an array of N elements, the last element has the index N-1.
NOTE: Array will throw an exception if accessing, creating, assigning elements of array using
indices greater than N-1.
Here is how to display the elements of the array: use the for loop, starting from index 0 to size-1 and
display using Console.WriteLine.
For example,
//output elements of array
Console.WriteLine("Display elements of array:");
for (int i = 0; i < num.Length; i++ )
{
Console.WriteLine(num[i] + "\t");
}
num.Length
Syntax:
foreach (datatype item in collection) {
}
Console.WriteLine("Display elements of array:");
foreach(int x in num){
Console.WriteLine(x);
}
Take note that when accessing elements of an array using a loop, the index is always the counter
variable.
Traverse an Array
To traverse an array, use the for or foreach loop, starting from index 0 to size-1 and do
the task asked.
Console.WriteLine(num[i]);
}
//another way to output elements of array
Console.WriteLine("Display elements of array using foreach loop:");
foreach(int x in num){
Console.WriteLine(x);
}
// compute sum
int sum=0;
for (int i = 0; i < num.Length; i++)
{
sum = sum + num[i];
}
}
}
}
Output:
Practice Exercises
1. Write a program, which creates an array of 15 elements of type integer and initializes each of
the elements with a value equals to the index of the element multiplied by 3. Print the elements
of the array.
2. Write a program, which creates an array of N elements of type character and determine how
many elements are vowels. Print the elements of the array and the number of vowels in the
array.
Exercises (Graded)
A. Ask an integer input from the user and search the value if it is an element of the array (set
A) and display the index of the integer.
Example, integer input = 15
15 is an element of the array, located at index 2.
B. Compute the average of the elements in the array (set A).
C. Copy the elements of the first array (set A) to the second array (set B).
Example:
set B = {3,8,15,20}
D. Display the reverse of the second array (set B).
Example:
set B = {20, 15, 8, 3}