0% found this document useful (0 votes)
2 views32 pages

Lect8

Uploaded by

6m9rt8dg8j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views32 pages

Lect8

Uploaded by

6m9rt8dg8j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Visual Programming in Visual Studio.

NET

Microsoft C#.NET

Qadeem Khan
IIT, KUST
qadeem_kust@yahoo.com
What Is an "Array"?
 Arrays are vital for most programming languages. They are
collections of variables, which we call elements:
 An array’s elements in C# are numbered with 0, 1, 2, … N-1.
Those numbers are called indices. The total number of
elements in a given array we call length of an array.
 All elements of a given array are of the same type, no matter
whether they are primitive or reference types. This allows
us to represent a group of similar elements as an
ordered sequence and work on them as a whole.
 Arrays can be in different dimensions, but the most used are
the one-dimensional and the two-dimensional arrays.
One-dimensional arrays are also called vectors and two-
dimensional are also known as matrices.
Declaration and Allocation of
Memory for Arrays
 In C# the arrays have fixed length, which is
set at the time of their instantiation and
determines the total number of elements.
Once the length of an array is set we cannot
change it anymore.
Declaring an Array
 We declare an array in C# in the following
way:
 int[] myArray;
Creation of an Array – the
Operator "new"
 In C# we create an array with the help of
the keyword new, which is used to
allocate memory:

 int[] myArray = new int[6];


Array Initialization and Default
Values
 Before we can use an element of a given array,
it has to be initialized or to have a default
value. In some programming languages
there are no default values and then if we
try to access an element, which is not
initialized, this may cause an error. In C#
all variables, including the elements of
arrays have a default initial value. This
value is either 0 for the numeral types or
its equivalent for the non-primitive types
(for example null for a reference type and
false for the bool type).
 Of course we can set initial values explicitly.
We can do this in different ways. Here is one
of them:

 int[] myArray = { 1, 2, 3, 4, 5, 6 };
Declaration and Initialization of
an Array – Example
 Here is one more example how to declare
and initialize an array:
 string[] daysOfWeek = { "Monday",

"Tuesday", "Wednesday","Thursday",
"Friday", "Saturday", "Sunday" };
Access to the Elements of an
Array
 We access the array elements directly using
their indices. Each element can be
accessed through the name of the
array and the element’s index
(consecutive number) placed in the
brackets. We can access given
elements of the array both for reading
and for writing, which means we can
treat elements as variables.

 myArray[index] = 100;
 int[] myArray = new int[6];
 myArray[1] = 1;
 myArray[5] = 5;
iterate through the array using
a loop statement
Going Out of Bounds of the
Array
 The .NET Framework does an automatic
check on each element access attempt, whether
the index is valid or it is out of the range of the
array. When we try to access an invalid (not
existing) element in an array,
aSystem.IndexOutOfRangeException is
thrown. The automatic check really helps the
developers find errors while working with arrays.
Of course, checking for exceptions has its price.
Checks affect the performance, but that’s
nothing compared to avoiding errors like "out of
range", "access to unallocated memory", etc.
Reversing an Array – Example
 In the next example we will access
elements and change them using their
indices. The task is to print the elements
in reversed order. We will reverse the
elements of the array using a second,
auxiliary array, where we will keep the
elements of the first one, but in a reversed
order. Note that the length of both arrays is
the same and it stays unchanged after the
first allocation:
Searching into an array
Array Class in C#.NET
 The Array class is the base class for all the
arrays in C#. It is defined in the System
namespace. The Array class provides
various properties and methods to work
with arrays.
Properties of the Array Class
Methods of the Array Class
 Guess the output??????
Reading an Array from the
Console
 We will use a for-loop and the .NET
Framework tools for reading from the console.
 Initially we read a line from the console
using Console.ReadLine(), and then we
parse that line to an integer number using
int.Parse() and we set it to the variable n.
We then use the number n as length of the
array.

 int n = int.Parse(Console.ReadLine());
 int[] array = new int[n];
 Again we use a loop to iterate through the
array. At each iteration we set the current
element to what we have read from the
console. The loop will continue n times,
which means it will iterate through the
array and so we will read a value for
each element of the array:
 for (int i = 0; i < n; i++)
 {
 array[i] = int.Parse(Console.ReadLine());
 }
 string []answer = new string[5]
 for(int i = 0;i<answer.length;i++)

{ answer[i]= Console.ReadLine();
}
 double[] array = new double[6];
 Console.WriteLine("Please Enter 6 Floating numbers");
 for (int i = 0; i < 6; i++)
 {
 array[i] = Convert.ToDouble(Console.ReadLine());
 }

 double sum = 0;

 foreach (double d in array)


 {
 sum += d;
 }
 double average = sum / 6;
 Console.WriteLine("===============================================");
 Console.WriteLine("The Values you've entered are");
 Console.WriteLine("{0}{1,8}", "index", "value");
 for (int counter = 0; counter < 6; counter++)
 Console.WriteLine("{0,5}{1,8}", counter, array[counter]);
 Console.WriteLine("===============================================");
 Console.WriteLine("The average is ;");
 Console.WriteLine(average);
 Console.WriteLine("===============================================");
 Console.WriteLine("would you like to search for a certain elemnt ? (enter yes or no)");
 string answer = Console.ReadLine();
 switch (answer)
 {
 case "yes":
 Console.WriteLine("===============================================");
 Console.WriteLine("please enter the array index you wish to get the value of it");
 int index = Convert.ToInt32(Console.ReadLine());
 Console.WriteLine("===============================================");
 Console.WriteLine("The Value of the selected index is:");
 Console.WriteLine(array[index]);
 break;

 case "no":
 Console.WriteLine("===============================================");
 Console.WriteLine("HAVE A NICE DAY SIR");
 break;
 }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy