c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I832 materiais10.248 seguidores
Pré-visualização50 páginas
Str, and also it gives the no opinion in this case.
Comment
When you in Visual Studio creates a new console application it automatically creates a class with a 
Main() method. It is a static method and it must be, as it must be called by the runtime system without 
having an object. In most testing programs the class with Main() had only static methods, for example.
static void Test2()
{
Console.WriteLine(Str.FillRight("abc", 10, 'x'));
Console.WriteLine(Str.FillLeft("abc", 10, 'x'));
Console.WriteLine(Str.FillCenter("abc", 10, 'x'));
}
and the method is usually called from Main(). When you in such situations has no object, the methods 
must be static, and that is why the methods in the Main() class has always been static.
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
137 
More about arrays
16 More about arrays
I have previously defined an array as a number of elements of a particular type that can be referenced 
via a common name. The picture of an array is a structure like the following
0 1 2 3 4 5 6 7 8 9
where each box has room for an element of the arrays type. The individual elements can be referenced 
using the array name and an index that always starts from 0. The type can be anything, and in the class 
Cup it was a Dice, while in the class Purse it was an IBankNote. The only thing to note is that if the type 
is a value type, the boxes directly contains the value that is attached to the individual indices, but if the 
type is a reference type, the boxes contains only references to the objects that are linked to the individual 
objects. It is rare that it means so much in practice, but you should be aware that if you write something 
like the following
Dice[] t = new Dice[5];
then there is created an array, but there it is not yet filled with Dice objects. The array is empty 
corresponding to each position contains null. There is not yet created any Dice objects.
How to retain your 
top staff
FIND OUT NOW FOR FREE
Get your free trial
Because happy staff get more done
What your staff really want?
The top issues troubling them?
How to make staff assessments 
work for you & them, painlessly?
DO YOU WANT TO KNOW:
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
138 
More about arrays
An array as above is a 1-dimensional array. One can also work with arrays of multiple dimensions. For 
example is a 2-dimensional array a structure organized into rows and columns, and each element can 
be referenced using the array name and an index pair. For example you can define a two-dimensional 
array of elements of the type int having 4 rows and five columns in the following manner:
int[,] t = new int[4, 5];
t[2, 3] = 43;
This can be illustrated in the figure below. Please note that as with a 1-dimensional array indices start 
with 0 \u2013 for both rows and columns.
43
0
1
2
3
1 2 3 40
It is seldom you are using arrays with more than two dimensions, but there is no upper limit to the 
number of dimensions, but for us humans it is difficult to give the array a geometric interpretation. For 
example you can define a 3-dimensional array as:
int[,,] t = new int[3, 4, 2];
This can be illustrated as a cube (or more 2-dimensional arrays, lying behind each other):
0
1
2
1 30 2
0
1
The individual elements referenced by three indices, for example.
t[1, 2, 1] = 53;
Then it is easy to guess the syntax of how to define arrays of dimension greater than 3.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
139 
More about arrays
Exam35
Multi-dimensional arrays
In this example I will show some examples of multidimensional arrays.
How to
The examples are as follows:
static void Test1()
{
char[,] t = new char[5, 4];
char ch = 'A';
for (int i = 0; i < t.GetLength(0); ++i)
for (int j = 0; j < t.GetLength(1); ++j) t[i, j] = ch++;
Print(t);
}
static void Test2()
{
char[,,] t = new char[3, 5, 4];
char ch = 'A';
for (int i = 0; i < t.GetLength(0); ++i)
for (int j = 0; j < t.GetLength(1); ++j)
for (int k = 0; k < t.GetLength(2); ++k) t[i, j, k] = ch++;
Print(t);
}
static void Test3()
{
int[,] t = { { 2, 3, 5, 7 }, { 11, 13, 17, 19 }, { 23, 29, 31, 37 } };
Print(t);
}
static void Test4()
{
char[][] t = new char[4][];
t[0] = new char[3];
t[1] = new char[5];
t[2] = new char[2];
t[3] = new char[7];
char ch = 'A';
for (int i = 0; i < t.Length; ++i)
for (int j = 0; j < t[i].Length; ++j) t[i][j] = ch++;
Print(t);
}
static void Test5()
{
int[] t = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
foreach (int n in t) Console.WriteLine(n);
}
static void Test6()
{
int[] t = { 23, 7, 5, 11, 3, 17, 29, 2, 19, 13 };
Array.Sort(t);
foreach (int n in t) Console.WriteLine(n);
}
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
140 
More about arrays
static void Print(char[,] t)
{
for (int i = 0; i < t.GetLength(0); ++i)
{
for (int j = 0; j < t.GetLength(1); ++j) Console.Write(&quot;{0} &quot;, t[i, j]);
Console.WriteLine();
}
}
static void Print(char[,,] t)
{
for (int i = 0; i < t.GetLength(0); ++i)
{
for (int j = 0; j < t.GetLength(1); ++j)
{
for (int k = 0; k < t.GetLength(2); ++k) Console.Write(&quot;{0} &quot;, t[i, j, k]);
Console.WriteLine();
}
Console.WriteLine();
}
}
static void Print(int[,] t)
{
for (int i = 0; i < t.GetLength(0); ++i)
{
for (int j = 0; j < t.GetLength(1); ++j) Console.Write(&quot;{0, 3:D} &quot;, t[i, j]);
Console.WriteLine();
}
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
141 
More about arrays
static void Print(char[][] t)
{
for (int i = 0; i < t.Length; ++i)
{
for (int j = 0; j < t[i].Length; ++j) Console.Write(&quot;{0} &quot;, t[i][j]);
Console.WriteLine();
}
}
Explanation
The first example creates a 2-dimensional array of the type char and prints it. You should particularly 
note how one refers to the number of elements in each dimension with the method GetLength(), where 
the parameter indicates the dimension that you refer.
The next example is similar, but here is instead talking about a 3-dimensional array. You should be 
especially aware of how to create a 3-dimensional array \u2013 here with 5 rows, 4 columns and 3 layers. Note 
also how to refer to an element using three indices.
It is also possible to initialize a multidimensional array in the declaration using a list. The third example 
shows how to create a 2-dimensional array consisting of 3 rows and 4 columns.
The type of an array can be anything and thus specially also another array. This makes it possible to 
define the arrays, where each row has a different number of elements. The fourth example creates an 
array of four rows where the numbers of elements per array are respectively 3, 5, 2 and 7:
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
142 
More about arrays
As a further remark concerning arrays, let me mention another loop construction. Indeed, it has not 
specifically to do with arrays, but it may be useful in the context of arrays. The fifth example defines a 
generally 1-dimensional array of 10 elements. The array is printed on the screen pass it with a loop, but 
this time using a foreach loop. The syntax is simply to define a variable of the same type as the array, and 
this variable will then run through all array elements. The advantage of foreach rather than a simple for 
statement is simply that it may be more readable.
As a last remark concerning arrays, I will mention the class Array, which