You are on page 1of 15

Visual Programming

Week No. 02
1. Arrays

A console application, in the context of C#, is an application that takes input and displays
output at a command line console with access to three basic data streams: standard input,
standard output and standard error.

A console application facilitates the reading and writing of characters from a console -
either individually or as an entire line. It is the simplest form of a C# program and is
typically invoked from the Windows command prompt. A console application usually
exists in the form of a stand-alone executable file with minimal or no graphical user
interface (GUI).
Software required for completing this tutorial successfully

In order to successfully complete this tutorial, Microsoft Visual Studio 2010 is


required to be installed on your PCs
In today’s session, we will
 Understand the use of Arrays in Visual C#

 Types of Arrays In C#
2. Arrays in C#

What are Arrays?

Arrays are vital for most programming languages. An array is a number of object of a

specific type than can be referenced with a common name and the individual objects are

referenced by that name and an index. 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.

Figure 1 An array of 5 Elements

In above figure, 2.1, an array of 5 elements is shown

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.

Declaring and Memory Allocation to an Array

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.

int[] stdMarks;
When we declare an array type variable, it is a reference, which does not have a value (it points to null). This is

because the memory for the elements is not allocated yet.

Creating Array using “new” Operator

In C# we create an array with the help of the keyword new, which is used to allocate memory:

int[] stdMarks = new int[6];

In the above example, after the allocation of memory for the array the variable myArray points to an address in the
dynamic memory, where the values are. In C#, the elements of an array are always stored in the dynamic memory
(called also heap).

Figure 2 Memory Allocation to Array Elements


Now, let we see how to program array in Visual Studio

Open Visual Studio, Start New Console Application as we did in Week-01

Figure 3 Opening Microsoft Visual Studio


1. Next, Choose File  New  Project as shown

Figure 4 Starting New Project in Visual Studio


2. Choose visual C# language in the Left hand pane of the open Window as shown in the

next figure

Figure 5 Selecting Visual C# Language


3. Next, Choose Console Application from the Right Hand Pane in the Window shown

above. Enter the name of the Application as Week02 as shown below and press the OK

Button

Figure 6 Choosing Console Application and Renaming Application to Week02


In the Source Code window as shown below, type the code to declare an Array of Size 10 and
input values and output the values. Code is shown in the next figure:

Figure 7 Array Read and Output Source Code

Following output will be shown when you compile and run the application.
Figure 8 Output of Array Read and Output Source Code

In the next example Array elements are initialized at the time of declaration.

Figure 9 Array Initialization Source Code

When you run the application, the output is given in next figure.
Figure 10 Output of Array Initialization Source Code

Next Source Code shows the use of array property “Length”. This property is used to find
the size of the array. Here is the source code

Figure 11 Use of Length Property of Array to find the capacity of the array
Multidimensional Array in C#
The one-dimensional arrays are known also as vectors in mathematics. Often we need arrays with more than one
dimension. For example we can easily represent the standard chess board as a two-dimensional array with size 8 by
8 (8 cells in a horizontal direction and 8 cells in a vertical direction).

Declaring Multidimensional Array:

We declare a one-dimensional array of integer numbers using int[], and we declare a two-dimensional with int[,].
This example shows that:

int[,] stdSubjectWiseMarks;

Those arrays we will call two-dimensional, because they have two dimensions. They are also known as matrices (it
is mathematical term). In general arrays with more than one dimension we will call multidimensional.

This way we can declare three-dimensional arrays as we add one more dimension:

int[,,] stdSubjectWiseMarks;

In theory there is no limit for an array dimensions, but in practice we do not use much arrays with more than two
dimensions therefore we will focus on two-dimensional arrays.

Creating or Memory Allocation for Two Dimensional Array:

We are allocating memory for multidimensional arrays by using the keyword new and for each dimension we set
a length in the brackets as shown:

int[,] stdSubjectWiseMarks;

stdSubjectWiseMarks = new int[3, 3];

Or the same can be done using single line syntax

int[,] stdSubjectWiseMarks=new int[3, 3];

This will create a two dimensional array with 3 Rows and 3 Columns.
Initializing Two Dimensional Array:

int[,] stdSubjectWiseMarks = {
{65, 55, 50},
{70, 68, 45}
{50, 60, 70}

};

This will create a 3 Rows and 3 Columns Matrix as shown below

0 1 2

0 65 55 50

1 70 68 45

2 50 60 70

Congratulations! We Have successfully Completed today’s Tutorial


Visual Hands on are prepared by
Saif Ur Rehman, Assistant Professor
UIIT, PMAS Arid Agriculture University, Rawalpindi

You might also like