c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I826 materiais10.227 seguidores
Pré-visualização50 páginas
if your line is a palindrome:
let i = 0
let j = index of the last character
as long as i < j repeat
{
if both i and j refers to a character who must be ignored then
{
increment i by 1
decrement j by 1
}
else if i reference to a character to be ignored then
increment i by 1
else if j reference to a character to be ignored then
decrement j by 1
else if character number i is different from character number j then
it is not a palidrome
ellers
{
increment i by 1
decrement j by 1
}
}
it is a palidrome
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 
60 
Strings
This algorithm can be written in C# as follows:
static bool IsPalindrom(string text)
{
for (int i = 0, j = text.Length - 1; i < j; )
{
if (!CharOk(text[i]) && !CharOk(text[j]))
{
++i;
--j;
}
else if (!CharOk(text[i]))
++i;
else if (!CharOk(text[j]))
--j;
else if (text[i] != text[j])
return false;
else
{
++i;
--j;
}
}
return true;
} 
static bool CharOk(char ch)
{
return (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == 'æ' ||
ch == 'ø' || ch == 'å';
}
Then you can write the Main() method:
static void Main()
{
while (true)
{
string line = Enter();
if (IsPalindrom(line.ToLower())) Console.WriteLine(line + &quot; er et palindrom&quot;); 
else Console.WriteLine(line + &quot; er ikke et palindrom&quot;);
}
}
Back is the input function:
static string Enter()
{
Console.Write(&quot;? &quot;);
string text = Console.ReadLine();
if (text.Length == 0) Environment.Exit(0);
return text;
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
61 
Strings
Explanation
First, notice the condition in the while loop in Main(). It is always true, and it is thus an infinite loop. This 
means that the loop and thus the program must stop in some other way. It occurs in the input method. 
If the user simply keys enter and thus do not enter anything, the statement is executed:
Environment.Exit(0);
which stops the program. One can discuss whether it is the way to do it, and it is also primarily included 
here to demonstrate that it is possible.
The check for palindrome is performed by the method IsPalindrom(). The idea is to have an index to 
both the start and the end of the string. The characters are compared in pairs, and an auxiliary method 
CharOk() is used to test whether it is a character that must be included.
In the case of conditions, there are three major operators, which as arguments have one or two conditions:
\u2022	 && takes two arguments of type bool and is a mathematical conjunction. This means that it 
is true if both its arguments are true.
\u2022	 | | takes two arguments of type bool and is a mathematical disjunction. This means that it is 
true, if at least one of its arguments is true.
\u2022	 ! has an argument of type bool and is a mathematical negation. That is, it has the opposite 
truth value of the argument.
The method CharOk() is used to test whether its parameter is a character to be counted: Whether it 
is a letter or a digit. The characters are arranged in natural order, but the three Danish letters are for 
themselves, which complicates the control a bit. You will notice how the complex expression is built with 
the operators && and | |. && has higher precedence than | |, so strictly speaking, the parentheses can be 
omitted, but they increase readability. The check may be expressed in text something like:
if (ch is at least a and ch is lower than z) or 
(ch at least 0 and ch is lower than 9) or
 ch is æ or ch is ø or ch is å then ...
The method IsPalindrom() use the ! operator to test the opposite value of the CharOk(), for example:
if not CharOk(text[i]) then ...
Note that when the method IsPalindrom() is called in the Main() method the text (the string entered) 
are first converted to lowercase with the method ToLower(). It is therefore, it is sufficient to test for the 
small letters in the method CharOk().
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
62 
Arrays
7 Arrays
An array is a number of objects of a particular type that can be referenced with a common name and 
the individual objects are referenced by that name and an index. The first element always has index 0, 
and you can have the following picture of an array with 10 objects
0 1 2 3 4 5 6 7 8 9
where each box has space for an object of that type. In C# you can create an array with space for 10 int 
objects as follows:
int[] number = new int[10];
It is the brackets [] which says that number is an array. Notice how an array is created with new. It is 
necessary and explains that an array is a reference type. If, for example you will place numbers in the 
first four places, it could be done as follows:
number[0] = 2;
number[1] = 3;
number[2] = 5;
number[3] = 7;
The following is also a legal statement:
int sum = number[1] + number[3];
and the variable sum will have the value 10. A single element like number[2] can simply be used anywhere 
where one can use an int variable.
Exam18
Two arrays of the type int
As an example, the following program creates two arrays with elements of the type int and prints them 
on the screen:
static void Main(string[] args)
{
int[] v1 = new int[10];
int[] v2 = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
for (int i = 0; i < v1.Length; ++i) v1[i] = i + 1;
Print(v1);
Print(v2);
}
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
63 
Arrays
static void Print(int[] t)
{
for (int i = 0; i < t.Length; ++i) Console.Write(&quot;{0} &quot;, t[i]);
Console.WriteLine();
}
If you run the program, you get the result:
Explanation
You must note how the array v2 is defined by a list of numbers. It simply means that the compiler can 
create the array and directly initialize it with the list elements. Also, note how the array v1 is initialized 
in a for loop. Note here especially how Length can be used to refer to the number of elements. Note, 
finally, how to transfer an array as a parameter to a method, and specifically how you in the method 
Print() write that t is an array.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
64 
Arrays
Exam19
Array of strings
In the above example, the type of the arrays is int, but the type of an array can generally be anything. 
Below is a program that creates an array with 5 objects of the type string:
static void Main(string[] args)
{
string[] names = new string[5];
names[0] = &quot;Svend&quot;;
names[1] = &quot;Knud&quot;;
names[2] = &quot;Valdemar&quot;;
Console.WriteLine(&quot;---------------------------&quot;);
for (int i = 0; i < names.Length; ++i) Console.WriteLine(names[i]);
Console.WriteLine(&quot;---------------------------&quot;);
}
Note that only the first three elements are initialized. The latter two are not initialized and has the value 
null, equivalent to that string is a reference type. If the method is executed, one gets the result:
Note the two blank lines corresponding to the printing of null.
Exam20
Yatzy
There should be written a program to simulate where you throw 5 cubes until they all show the same 
value \u2013 until you get yatzy.
How to
The program can be written as follows:
class Program
{
static Random rand = new Random();
static void Main(string[] args)
{
int[] beaker = new int[5];
int count = 0;
do
{
Download free eBooks at bookboon.com
C# 1 Introduction to