happens. Declaring a Method that Returns a Result Lets see again
how to declare a method. static () Earlier we said that at the place of
we will always put void. Now we will extend this definition, as we will
see, that void is not the only choice. Instead
converts a decimal number to binary one. 5. Write a program that
converts a binary number to decimal one. 6. Write a program that
converts a decimal number to hexadecimal one. 7. Write a program
that converts a hexadecimal number to decimal one. 8. Write
95031 as an example, which can be represented in the decimal
numeral system as: 95031 = (9104 ) + (5103 ) + (0102 ) + (3101 )
+ (1100 ) Represented that way, the number 95031 is presented in a
natural way for humans because the principles of the decimal
n
If it 346 Fundamentals of Computer Programming with C# cannot be
given a good name, this may indicate that the method solves more than
one task and, hence, it must be separated into sub-methods. - Method
names should describe an action, so they should con
the elements of the array and places in each position 1. The steps of
the algorithm we perform in an infinite loop, from which we are going
to escape in an appropriate moment by ending the execution of the
methods via the operator return. The way we imple
usage could considerably simplify the programming code and its
readability. Example of Recursion Lets consider the Fibonacci
numbers. These are the elements of the following sequence: 1, 1, 2, 3,
5, 8, 13, 21, 34, 55, 89, 144, Each element of the sequence
we must note that the type of returned value of a method is only part of
its declaration, not of its signature. What identifies a method is its
signature. The return type is not part of the method signature. The
reason is that if two methods differ only b
bestSum = long.MinValue; int bestRow = 0; int bestCol = 0; for (int
row = 0; row < matrix.GetLength(0) - 1; row+) cfw_ for (int col = 0; col
< matrix.GetLength(1) - 1; col+) cfw_ 252 Fundamentals of Computer
Programming with C# long sum = matrix[row, col]
constructed easily and leads to simple and efficient calculations. An
example of linear computational process is the calculation of factorial.
In it we calculate the elements of the sequence in which every next
element depends only on the previous ones. W
1) + Fib(n - 2); If we set the value of n = 100, the calculations
would take so much time that no one would wait to see the result. The
reason is that similar implementation is extremely inefficient. Each
recursive call leads to two more calls and each
of the result can be regarded as ordered sequence of N numbers. The
first one represents the current value of the counter of the loop, the
second one of the second loop, etc. On each position we can have
value between 1 and K. The solution of our task boi
Method to Display a Company Logo Example To make the
mentioned above more clear, we will change the example that shows
the logo of "Microsoft": static void PrintLogo(string logo)
cfw_ Console.WriteLine(logo); Now, executing our method, we can
display the
method signature, mentioned above. To clarify, lets discuss the
following example: we have a method PrintNameAndAge(), in which
method declaration is a parameters list, with parameters of types
string and int, ordered as shown below: Person.cs class Perso
explained in the chapter Exception Handling, but for now you
should remember that returning an incorrect result or having a side
effect are bad practices. If a method cannot do its job, Chapter 9.
Methods 347 it should inform its caller about this by thro
the end of the methods parameters list. So, if we try to put the
declaration of the var-args parameter x, shown in the last example,
not at the last place, like so: static void DoSomething(params int[] x,
string strParam) cfw_ The compiler will return th
loop. 7. There are two solutions: Chapter 9. Methods 349 First
solution: Let the number is num. So while num 0 we print its last
digit (num % 10) and then divide num by 10. Second solution: Convert
the number into a string string and print it in a reverse
Programming with C# "N" or "n" Equivalent to "F" but represents
also the corresponding separator for thousands, millions, etc. (for
example, in the English language often the number "1000" is
represented as "1,000" with comma between the number 1 and the
element from their signature parameter list, is different, due to
difference of their second element type: static void DoSomething(int
p1, float p2) cfw_ static void DoSomething(int param1, int param2) cfw_
Chapter 9. Methods 323 In this case even if we
algorithm. First we create a two-dimensional array, which contains
integer numbers. We declare our auxiliary variables bestSum,
bestRow, bestCol and we initialize bestSum with the minimal value of
type long (so any other value is greater than this one). N
0, 3, 5, 8, 10, 12 Best Practices when Using Methods In the chapter
"High-Quality Programming Code" we will explain in details about
the good practices for writing methods. None the less, we will look at
some of them right now, so we can start applying th
the console. If we execute the example for n=3 and m=75, we will
obtain the following result: n = 3 m = 75 3 5 7 11 13 17 19 23 29 31 37
41 43 47 53 59 61 67 71 73 Lucky Numbers Example Lets consider
another example through which we will show that we can
their count is not known in advance, we have to think of an alternative
approach. This is the case with the following task. Write a program
that simulates the execution of N nested loops from 1 to K, where N
and K are entered by the user. The result of th
many times certain number can be found in a given array. Write a
program to test that the method works correctly. 5. Write a method that
checks whether an element, from a certain position in an array is
greater than its two neighbors. Test whether the met
noticed that our iterative algorithm, which imitates nested loops, works
in a completely different principle. Try to implement the same without
recursion and you will see it is not easy. Ordinarily each recursion
could boil down to iteration by using a st
array, while the unsorted is in the right. For each step of the
algorithm, the sorted part expands to the right with one element and
the unsorted shrinks with one element from its left part. Lets take a
look at an example. So assume we have the following
that the name (DoSomething) is the same for both methods. We still
have different signatures for both methods. Overloaded Methods
Invocation Since we have declared methods with the same name and
different signatures, we can invoke each of them as any othe
positive integer called precision, which has a different meaning for the
different specifiers. When it affects the number of decimal places after
the decimal point, the result is rounded. The following table describes
specifiers and their precision meanin
However, it seems that we could optimize it more, and instead of the
argument to be an integer array, we can declare it in such way that it
could accept a variable count of integer parameters. This will save us
the need to initialize the array in advance
position, i.e. the related parameter to be at the end of the parameters
list. Chapter 9. Methods 319 Specifics of Empty Parameter List After
we got familiar with the declaration and invocation of methods with
variable number of arguments, one more questio