0% found this document useful (0 votes)
87 views

One Dimensional Array

The document provides information on arrays in C#, including: - Arrays can store multiple values of the same data type and are indexed starting from 0. - One-dimensional arrays are declared with syntax like "datatype[] arrayName = new datatype[size];". - Elements can be accessed and assigned using the array name and index. - Loops like for and foreach are used to traverse arrays. - Arrays have a .Length property that returns the number of elements. - Examples are given to input, output, sum, and count elements in arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

One Dimensional Array

The document provides information on arrays in C#, including: - Arrays can store multiple values of the same data type and are indexed starting from 0. - One-dimensional arrays are declared with syntax like "datatype[] arrayName = new datatype[size];". - Elements can be accessed and assigned using the array name and index. - Loops like for and foreach are used to traverse arrays. - Arrays have a .Length property that returns the number of elements. - Examples are given to input, output, sum, and count elements in arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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

One Dimensional Array

How to declare and create an array?

In general, to declare an array

datatype [ ] arrayName = new dataype[arraySize];

For example,

int[ ] num = new int[10];

Where:

num is the name of the array,

10 is the maximum number of integers that can be stored

int is the data type of those values.

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,

char[ ] name = new char[30];

Where:

name is an array of thirty characters.


You can declare an array and initialize.

 If array declared with initialization, the number of elements determine the size of the array

Here are few examples,

int[ ] num= {1,2,3,4,5,6,7,8,9,10};

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:

int[ ] num = {1,2,3};

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:

string[] daysOfWeek = { "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday", "Sunday"


};

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.

Here some examples,

int[ ] num = new int[10];

num[0] // first element of num array

num[9] //last element of num array

string[] daysOfWeek = { "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday", "Sunday"


};

daysOfWeek[5] // 6th element of daysOfWeek array

Assign an element of an Array to a variable


string week;

week = daysOfWeek[3] // 4th element of daysOfWeek array is assigned to week variable

Now, week has a value which is “Thursday”

int x;

x = num[7] // 8th element of num array is assigned to variable x

Now, x has a value of 8.

Assign a value to an Array


int[] values = new int[5];

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.

Input and Output an Array


Here is how to input elements to the entire array: use the for loop, starting from index 0 to
size-1 and input using Console.Readline() if it is an array of numbers.
For example, //this will determine the number of elements (size) in the array
int size = Convert.ToInt32(Console.ReadLine());
int[] num = new int[size];

Console.WriteLine("Input elements of array:");

for (int i = 0; i < size;i++ )


{
//assign input as an element of array from 0 to size-1
num[i] = Convert.ToInt32(Console.ReadLine());
}

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

 .Length is a function in C# that will return the number of elements(size of array)


Here is how to display the elements of the array: use the foreach loop (this a loop structure that is use
for collections like array), starting from index 0 to size-1

Syntax:
foreach (datatype item in collection) {

// Process the value here

}
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.

For example: Compute the sum of all elements in the array


int sum=0;
for (int i = 0; i < num.Length; i++)
{
sum = sum + num[i];
}

Console.WriteLine("The sum of the elements in the array is:" + sum);

Another example: Determine how many even numbers in the array


int EvenNo = 0;
foreach(int x in num){
if (x % 2 == 0) {
EvenNo++;
}
}
Console.WriteLine("Even numbers in array:" + EvenNo);
Here is an example code for the array:
namespace ArrayExample
{
class Program
{
static void Main(string[] args)
{
//this will determine the number of elements (size) in the array
Console.WriteLine("Input no. of elements in the array");
int size = Convert.ToInt32(Console.ReadLine());
int[] num = new int[size];

//input values as element of array


Console.WriteLine("Input elements of array:");
for (int i = 0; i < size;i++ )
{
//assign input as an element of array from 0 to size-1
num[i] = Convert.ToInt32(Console.ReadLine());
}

//output elements of array


Console.WriteLine("Display elements of array using for loop:");
for (int i = 0; i < num.Length; i++ )
{

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];
}

Console.WriteLine("The sum of the elements in the array is:" + sum);

//count even numbers


int EvenNo = 0;
foreach(int x in num){
if (x % 2 == 0) {
EvenNo++;
}
}
Console.WriteLine("Even numbers in array:" + EvenNo);
Console.ReadKey();

}
}
}
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)

1. Create two sets of integers using array with N number of elements.

Example, given N=4 (four elements of array to input)

set A = {3, 8, 15, 20} // 3,8,15,20 are user inputs

set B = {0, 0, 0, 0} // initialize all elements of this array to 0

After creating the array, solve the following:

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}

You might also like